Patchwork color: be more conservative about setting ANSI mode on Windows (BC)

login
register
mail settings
Submitter Gregory Szorc
Date Feb. 4, 2015, 12:25 a.m.
Message ID <045521b92170102ea14f.1423009545@gps-mbp.local>
Download mbox | patch
Permalink /patch/7643/
State Accepted
Commit a78888d98606cdb8c1a052a911636aae052bd4b5
Headers show

Comments

Gregory Szorc - Feb. 4, 2015, 12:25 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1423009472 28800
#      Tue Feb 03 16:24:32 2015 -0800
# Node ID 045521b92170102ea14f3ad326242b63e24c09d5
# Parent  a8dc5a3f4f4c2d7195a1ccf1712ef70e2080dc0e
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.
Matt Mackall - Feb. 4, 2015, 1:15 a.m.
On Tue, 2015-02-03 at 16:25 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1423009472 28800
> #      Tue Feb 03 16:24:32 2015 -0800
> # Node ID 045521b92170102ea14f3ad326242b63e24c09d5
> # Parent  a8dc5a3f4f4c2d7195a1ccf1712ef70e2080dc0e
> color: be more conservative about setting ANSI mode on Windows (BC)

Erm.. ok, queued. We probably have users who've acclimated to the
bogosity here, I will give them your email address.

Patch

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -214,11 +214,25 @@  def _modesetup(ui, coloropt):
 
     mode = ui.config('color', 'mode', 'auto')
     realmode = mode
     if mode == 'auto':
-        if os.name == 'nt' and 'TERM' not in os.environ:
-            # looks line a cmd.exe console, use win32 API or nothing
-            realmode = 'win32'
+        if os.name == 'nt':
+            term = os.environ.get('TERM')
+            # TERM won't be defined in a vanilla cmd.exe environment.
+            if not term:
+                realmode = 'win32'
+
+            # UNIX-like environments on Windows such as Cygwin and MSYS will
+            # set TERM. They appear to make a best effort attempt at setting it
+            # to something appropriate. However, not all environments with TERM
+            # defined support ANSI. Since "ansi" could result in terminal
+            # gibberish, we error on the side of selecting "win32". However, if
+            # w32effects is not defined, we almost certainly don't support
+            # "win32", so don't even try.
+            if 'xterm' in term or not w32effects:
+                realmode = 'ansi'
+            else:
+                realmode = 'win32'
         else:
             realmode = 'ansi'
 
     if realmode == 'win32':