##// END OF EJS Templates
color: fix --color=always when output is piped...
Peter Arrenbrecht -
r14495:ad6ad51c default
parent child Browse files
Show More
@@ -114,7 +114,7 b' from mercurial.i18n import _'
114 'blue_background': 44, 'purple_background': 45,
114 'blue_background': 44, 'purple_background': 45,
115 'cyan_background': 46, 'white_background': 47}
115 'cyan_background': 46, 'white_background': 47}
116
116
117 def _terminfosetup(ui):
117 def _terminfosetup(ui, mode):
118 '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
118 '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
119
119
120 global _terminfo_params
120 global _terminfo_params
@@ -122,7 +122,6 b' def _terminfosetup(ui):'
122 if not _terminfo_params:
122 if not _terminfo_params:
123 return
123 return
124 # Otherwise, see what the config file says.
124 # Otherwise, see what the config file says.
125 mode = ui.config('color', 'mode', 'auto')
126 if mode not in ('auto', 'terminfo'):
125 if mode not in ('auto', 'terminfo'):
127 return
126 return
128
127
@@ -149,6 +148,51 b' def _terminfosetup(ui):'
149 "ECMA-48 color\n"))
148 "ECMA-48 color\n"))
150 _terminfo_params = {}
149 _terminfo_params = {}
151
150
151 def _modesetup(ui, opts):
152 global _terminfo_params
153
154 coloropt = opts['color']
155 auto = coloropt == 'auto'
156 always = not auto and util.parsebool(coloropt)
157 if not always and not auto:
158 return None
159
160 formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted())
161
162 mode = ui.config('color', 'mode', 'auto')
163 realmode = mode
164 if mode == 'auto':
165 if os.name == 'nt' and 'TERM' not in os.environ:
166 # looks line a cmd.exe console, use win32 API or nothing
167 realmode = 'win32'
168 elif not formatted:
169 realmode = 'ansi'
170 else:
171 realmode = 'terminfo'
172
173 if realmode == 'win32':
174 if not w32effects and mode == 'win32':
175 # only warn if color.mode is explicitly set to win32
176 ui.warn(_('warning: failed to set color mode to %s\n') % mode)
177 return None
178 _effects.update(w32effects)
179 elif realmode == 'ansi':
180 _terminfo_params = {}
181 elif realmode == 'terminfo':
182 _terminfosetup(ui, mode)
183 if not _terminfo_params:
184 if mode == 'terminfo':
185 ## FIXME Shouldn't we return None in this case too?
186 # only warn if color.mode is explicitly set to win32
187 ui.warn(_('warning: failed to set color mode to %s\n') % mode)
188 realmode = 'ansi'
189 else:
190 return None
191
192 if always or (auto and formatted):
193 return realmode
194 return None
195
152 try:
196 try:
153 import curses
197 import curses
154 # Mapping from effect name to terminfo attribute name or color number.
198 # Mapping from effect name to terminfo attribute name or color number.
@@ -301,40 +345,9 b' def uisetup(ui):'
301 global _terminfo_params
345 global _terminfo_params
302 if ui.plain():
346 if ui.plain():
303 return
347 return
304
305 formatted = (os.environ.get('TERM') != 'dumb' and ui.formatted())
306 mode = ui.config('color', 'mode', 'auto')
307 if mode == 'auto':
308 if os.name == 'nt' and 'TERM' not in os.environ:
309 # looks line a cmd.exe console, use win32 API or nothing
310 mode = w32effects and 'win32' or 'none'
311 else:
312 if not formatted:
313 _terminfo_params = False
314 else:
315 _terminfosetup(ui)
316 if not _terminfo_params:
317 mode = 'ansi'
318 else:
319 mode = 'terminfo'
320 if mode == 'win32':
321 if w32effects is None:
322 # only warn if color.mode is explicitly set to win32
323 ui.warn(_('warning: failed to set color mode to %s\n') % mode)
324 return
325 _effects.update(w32effects)
326 elif mode == 'ansi':
327 _terminfo_params = {}
328 elif mode == 'terminfo':
329 _terminfosetup(ui)
330 else:
331 return
332 def colorcmd(orig, ui_, opts, cmd, cmdfunc):
348 def colorcmd(orig, ui_, opts, cmd, cmdfunc):
333 coloropt = opts['color']
349 mode = _modesetup(ui_, opts)
334 auto = coloropt == 'auto'
350 if mode:
335 always = util.parsebool(coloropt)
336 if (always or
337 (always is None and auto and formatted)):
338 colorui._colormode = mode
351 colorui._colormode = mode
339 colorui.__bases__ = (ui_.__class__,)
352 colorui.__bases__ = (ui_.__class__,)
340 ui_.__class__ = colorui
353 ui_.__class__ = colorui
General Comments 0
You need to be logged in to leave comments. Login now