comparison hgext/color.py @ 24068:4e02418b4236

color: support a different color mode when the pager is active MSYS on Windows has a terminal that supports the "win32" color mode (which "auto" properly detects for us). However, a popularily configured pager in that environment (GNU less) only supports the "ansi" color mode. This patch teaches color about a new config option: pagermode. It behaves like "mode" but is only consulted when the pager is active for the current command. MSYS users can now set "pagermode = ansi" and get a colorful experience that just works. Previously, MSYS users would have to live without color when using GNU less as the pager, would have to manually configure the pager to attend every command, or would have gibberish if "ansi" was used without the pager.
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 04 Feb 2015 14:11:45 -0800
parents a78888d98606
children 5058e6962fcc
comparison
equal deleted inserted replaced
24067:0baf41e02a4d 24068:4e02418b4236
138 will only display ECMA-48 color codes, and terminfo mode may sometimes 138 will only display ECMA-48 color codes, and terminfo mode may sometimes
139 emit codes that less doesn't understand. You can work around this by 139 emit codes that less doesn't understand. You can work around this by
140 either using ansi mode (or auto mode), or by using less -r (which will 140 either using ansi mode (or auto mode), or by using less -r (which will
141 pass through all terminal control codes, not just color control 141 pass through all terminal control codes, not just color control
142 codes). 142 codes).
143
144 On some systems (such as MSYS in Windows), the terminal may support
145 a different color mode than the pager (activated via the "pager"
146 extension). It is possible to define separate modes depending on whether
147 the pager is active::
148
149 [color]
150 mode = auto
151 pagermode = ansi
152
153 If ``pagermode`` is not defined, the ``mode`` will be used.
143 ''' 154 '''
144 155
145 import os 156 import os
146 157
147 from mercurial import cmdutil, commands, dispatch, extensions, subrepo, util 158 from mercurial import cmdutil, commands, dispatch, extensions, subrepo, util
211 return None 222 return None
212 223
213 formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted()) 224 formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted())
214 225
215 mode = ui.config('color', 'mode', 'auto') 226 mode = ui.config('color', 'mode', 'auto')
227
228 # If pager is active, color.pagermode overrides color.mode.
229 if getattr(ui, 'pageractive', False):
230 mode = ui.config('color', 'pagermode', mode)
231
216 realmode = mode 232 realmode = mode
217 if mode == 'auto': 233 if mode == 'auto':
218 if os.name == 'nt': 234 if os.name == 'nt':
219 term = os.environ.get('TERM') 235 term = os.environ.get('TERM')
220 # TERM won't be defined in a vanilla cmd.exe environment. 236 # TERM won't be defined in a vanilla cmd.exe environment.