Patchwork D12158: color: don't infer vt status from TERM on Windows

login
register
mail settings
Submitter phabricator
Date Feb. 9, 2022, 6:16 p.m.
Message ID <differential-rev-PHID-DREV-76piacbzjndjpqxshrup-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50493/
State New
Headers show

Comments

phabricator - Feb. 9, 2022, 6:16 p.m.
mitchhentgesmozilla created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Previously, it was assumed that Windows environments with
  "xterm" in the TERM environment variable meant that either
  "virtual terminal mode" was already enabled, or that
  we are running in an environment that didn't need a "virtual
  terminal mode" (such as mintty, that interprets ANSI sequences
  itself).
  
  However, modern Cygwin and MSYS2 set TERM=xterm when using the
  Command Prompt as the terminal, which needs "virtual terminal
  mode" to be manually enabled. However, due to (issue6640),
  the vtmode wasn't being enabled.
  
  This patch ensures that we always try to enable vtmode on
  Windows regardless of the state of TERM, so that:
  
  - ANSI-based colors work in modern Cygwin/MSYS2 (with Command Prompt), and
  - The vtmode is unnecessarily set when running in a different terminal such as mintty, but it is simply redundant and doesn't appear to have ill effects.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12158

AFFECTED FILES
  mercurial/color.py

CHANGE DETAILS




To: mitchhentgesmozilla, #hg-reviewers
Cc: mercurial-patches, spectral, mercurial-devel

Patch

diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -248,28 +248,19 @@ 
     if pycompat.iswindows:
         from . import win32
 
-        term = encoding.environ.get(b'TERM')
-        # TERM won't be defined in a vanilla cmd.exe environment.
-
-        # 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.
-        ansienviron = term and b'xterm' in term
-
         if mode == b'auto':
             # 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.
             # w32effects is not populated when stdout is redirected, so checking
             # it first avoids win32 calls in a state known to error out.
-            if ansienviron or not w32effects or win32.enablevtmode():
+            if not w32effects or win32.enablevtmode():
                 realmode = b'ansi'
             else:
                 realmode = b'win32'
         # An empty w32effects is a clue that stdout is redirected, and thus
         # cannot enable VT mode.
-        elif mode == b'ansi' and w32effects and not ansienviron:
+        elif mode == b'ansi' and w32effects:
             win32.enablevtmode()
     elif mode == b'auto':
         realmode = b'ansi'