Mercurial > hg
comparison hgext/color.py @ 24028:a78888d98606
color: be more conservative about setting ANSI mode on Windows (BC)
The current color mode detection on Windows assumes the presence of the
TERM environment variable assumes ANSI is supported. However, this isn't
always true. In MSYS (commonly found as part of MinGW), TERM is set to
"cygwin" and the auto resolved color mode of "ansi" results in escape
sequences getting printed literally to the terminal. The output is
very difficult to read and results in a bad user experience. A
workaround is to activate the pager and have it attend all commands (GNU
less in MSYS can render ANSI terminal sequences properly).
In Cygwin, TERM is set to "xterm." Furthermore, Cygwin supports
displaying these terminal sequences properly (unlike MSYS).
This patch changes the mode auto-detection logic on Windows to be more
conservative about selecting the "ansi" mode. We now only select the
"ansi" mode if TERM is set and it contains the string "xterm" or if
we were unable to talk to win32 APIs to determine the settings. There is
a chance this may take away "ansi" from a terminal that actually
supports it. The recourse for this would be to patch the detection to
act appropriately and to override color.mode until that patch has
landed. However, the author believes this regression is tolerable, since
it means MSYS users won't have gibberish printed by default.
Since MSYS's common pager (less) supports display of ANSI sequences,
there is room to patch the color extensions so it can select the ANSI
color mode if a pager is activated.
Mozilla (being an active user of MSYS) would really appreciate this
being part of the stable branch. However, since I believe it is BC, I
haven't explicitly requested application to stable since I figure that
request will be denied.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 03 Feb 2015 16:24:32 -0800 |
parents | e563e0cfe08c |
children | 4e02418b4236 |
comparison
equal
deleted
inserted
replaced
24027:f2a631c7387a | 24028:a78888d98606 |
---|---|
213 formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted()) | 213 formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted()) |
214 | 214 |
215 mode = ui.config('color', 'mode', 'auto') | 215 mode = ui.config('color', 'mode', 'auto') |
216 realmode = mode | 216 realmode = mode |
217 if mode == 'auto': | 217 if mode == 'auto': |
218 if os.name == 'nt' and 'TERM' not in os.environ: | 218 if os.name == 'nt': |
219 # looks line a cmd.exe console, use win32 API or nothing | 219 term = os.environ.get('TERM') |
220 realmode = 'win32' | 220 # TERM won't be defined in a vanilla cmd.exe environment. |
221 if not term: | |
222 realmode = 'win32' | |
223 | |
224 # UNIX-like environments on Windows such as Cygwin and MSYS will | |
225 # set TERM. They appear to make a best effort attempt at setting it | |
226 # to something appropriate. However, not all environments with TERM | |
227 # defined support ANSI. Since "ansi" could result in terminal | |
228 # gibberish, we error on the side of selecting "win32". However, if | |
229 # w32effects is not defined, we almost certainly don't support | |
230 # "win32", so don't even try. | |
231 if 'xterm' in term or not w32effects: | |
232 realmode = 'ansi' | |
233 else: | |
234 realmode = 'win32' | |
221 else: | 235 else: |
222 realmode = 'ansi' | 236 realmode = 'ansi' |
223 | 237 |
224 if realmode == 'win32': | 238 if realmode == 'win32': |
225 _terminfo_params = {} | 239 _terminfo_params = {} |