Patchwork color: sync text attributes and buffered text output on Windows (issue5508)

login
register
mail settings
Submitter Matt Harbison
Date March 19, 2017, 6:06 p.m.
Message ID <b1bebed54e9371708e1c.1489946793@Envy>
Download mbox | patch
Permalink /patch/19449/
State Accepted
Headers show

Comments

Matt Harbison - March 19, 2017, 6:06 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1489941885 14400
#      Sun Mar 19 12:44:45 2017 -0400
# Node ID b1bebed54e9371708e1cd1eba63f0cedadfdf035
# Parent  da7d19324b1e3d2ce0636bf73e76bf8f3d68ed27
color: sync text attributes and buffered text output on Windows (issue5508)

I originally noticed that log output wasn't being colored after 3a4c0905f357,
but there were other complications too.  With a bunch of untracked files, only
the first 1K of characters were colored pink, and the rest were normal white.  A
single modified file at the top would also be colored pink.

Line buffering and full buffering are treated as the same thing in Windows [1],
meaning the stream is either buffered or not.  I can't find any explicit
documentation to say stdout is unbuffered by default when attached to a console
(but some internet postings indicated that is the case[2]).  Therefore, it seems
that explicit flushes are better than just not reopening stdout.

NB: pager is now on by default, and needs to be disabled to see any color on
Windows.

[1] https://msdn.microsoft.com/en-us/library/86cebhfs(v=vs.140).aspx
[2] https://sourceforge.net/p/mingw/mailman/message/27121137/
Yuya Nishihara - March 20, 2017, 4:22 a.m.
On Sun, 19 Mar 2017 14:06:33 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1489941885 14400
> #      Sun Mar 19 12:44:45 2017 -0400
> # Node ID b1bebed54e9371708e1cd1eba63f0cedadfdf035
> # Parent  da7d19324b1e3d2ce0636bf73e76bf8f3d68ed27
> color: sync text attributes and buffered text output on Windows (issue5508)

Looks good. Queued, thanks.

I'll make some cleanup on top of this patch. The way win32print() interacts with
file handles seems unclear.

Patch

diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -463,9 +463,11 @@ 
                 for sattr in m.group(1).split(';'):
                     if sattr:
                         attr = mapcolor(int(sattr), attr)
+                ui.flush()
                 _kernel32.SetConsoleTextAttribute(stdout, attr)
                 writefunc(m.group(2), **opts)
                 m = re.match(ansire, m.group(3))
         finally:
             # Explicitly reset original attributes
+            ui.flush()
             _kernel32.SetConsoleTextAttribute(stdout, origattr)