Patchwork [4,of,6] color: move 'write' logic to the core ui class

login
register
mail settings
Submitter Pierre-Yves David
Date Feb. 22, 2017, 4:54 p.m.
Message ID <0f2be97ca31046bbf9c9.1487782475@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/18719/
State Superseded
Delegated to: Yuya Nishihara
Headers show

Comments

Pierre-Yves David - Feb. 22, 2017, 4:54 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1487590299 -3600
#      Mon Feb 20 12:31:39 2017 +0100
# Node ID 0f2be97ca31046bbf9c9f37fcdacf7b406e94054
# Parent  9a9e8b28b717e30ed43030cc64f115a4356193de
# EXP-Topic color
color: move 'write' logic to the core ui class

One more step, the support for writing color is not directly in core. No
behavior change for the default case ('_colormode' = None).
Yuya Nishihara - Feb. 23, 2017, 2:11 p.m.
On Wed, 22 Feb 2017 17:54:35 +0100, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
> # Date 1487590299 -3600
> #      Mon Feb 20 12:31:39 2017 +0100
> # Node ID 0f2be97ca31046bbf9c9f37fcdacf7b406e94054
> # Parent  9a9e8b28b717e30ed43030cc64f115a4356193de
> # EXP-Topic color
> color: move 'write' logic to the core ui class

> --- a/mercurial/ui.py	Mon Feb 20 12:31:24 2017 +0100
> +++ b/mercurial/ui.py	Mon Feb 20 12:31:39 2017 +0100
> @@ -779,28 +779,40 @@ class ui(object):
>      def write(self, *args, **opts):
>          '''write args to output
>  
> -        By default, this method simply writes to the buffer or stdout,
> -        but extensions or GUI tools may override this method,
> -        write_err(), popbuffer(), and label() to style output from
> -        various parts of hg.
> +        By default, this method simply writes to the buffer or stdout.
> +        Color mode can be set on the UI class to have the output decorated
> +        with color modifier before behing writed to stdout.
>  
> -        An optional keyword argument, "label", can be passed in.
> -        This should be a string containing label names separated by
> -        space. Label names take the form of "topic.type". For example,
> -        ui.debug() issues a label of "ui.debug".
> +        The color used is controled by an optional keyword argument, "label".
> +        This should be a string containing label names separated by space.
> +        Label names take the form of "topic.type". For example, ui.debug()
> +        issues a label of "ui.debug".
>  
>          When labeling output for a specific command, a label of
>          "cmdname.type" is recommended. For example, status issues
>          a label of "status.modified" for modified files.
>          '''
>          if self._buffers and not opts.get('prompt', False):
> -            self._buffers[-1].extend(a for a in args)
> +            if self._bufferapplylabels:
> +                label = opts.get('label', '')
> +                self._buffers[-1].extend(self.label(a, label) for a in args)
> +            else:
> +                self._buffers[-1].extend(args)
> +        elif self._colormode == 'win32':
> +            for a in args:
> +                # windows color printing is its own can of crab, defer to
> +                # the color module and that is it.
> +                color.win32print(a, self.fout.write, **opts)

Nit: perhaps win32print() should be counted as a stdio_blocked section.

>          else:
> +            msgs = args
> +            if self._colormode is not None:
> +                label = opts.get('label', '')
> +                msgs = [self.label(a, label) for a in args]
>              self._progclear()
>              # opencode timeblockedsection because this is a critical path
>              starttime = util.timer()
>              try:
> -                for a in args:
> +                for a in msgs:
>                      self.fout.write(a)
>              finally:
>                  self._blockedtimes['stdio_blocked'] += \

Patch

diff -r 9a9e8b28b717 -r 0f2be97ca310 hgext/color.py
--- a/hgext/color.py	Mon Feb 20 12:31:24 2017 +0100
+++ b/hgext/color.py	Mon Feb 20 12:31:39 2017 +0100
@@ -297,22 +297,6 @@  def _modesetup(ui, coloropt):
     return None
 
 class colorui(uimod.ui):
-    def write(self, *args, **opts):
-        if self._colormode is None:
-            return super(colorui, self).write(*args, **opts)
-
-        label = opts.get('label', '')
-        if self._buffers and not opts.get('prompt', False):
-            if self._bufferapplylabels:
-                self._buffers[-1].extend(self.label(a, label) for a in args)
-            else:
-                self._buffers[-1].extend(args)
-        elif self._colormode == 'win32':
-            for a in args:
-                color.win32print(a, super(colorui, self).write, **opts)
-        else:
-            return super(colorui, self).write(
-                *[self.label(a, label) for a in args], **opts)
 
     def write_err(self, *args, **opts):
         if self._colormode is None:
diff -r 9a9e8b28b717 -r 0f2be97ca310 mercurial/ui.py
--- a/mercurial/ui.py	Mon Feb 20 12:31:24 2017 +0100
+++ b/mercurial/ui.py	Mon Feb 20 12:31:39 2017 +0100
@@ -779,28 +779,40 @@  class ui(object):
     def write(self, *args, **opts):
         '''write args to output
 
-        By default, this method simply writes to the buffer or stdout,
-        but extensions or GUI tools may override this method,
-        write_err(), popbuffer(), and label() to style output from
-        various parts of hg.
+        By default, this method simply writes to the buffer or stdout.
+        Color mode can be set on the UI class to have the output decorated
+        with color modifier before behing writed to stdout.
 
-        An optional keyword argument, "label", can be passed in.
-        This should be a string containing label names separated by
-        space. Label names take the form of "topic.type". For example,
-        ui.debug() issues a label of "ui.debug".
+        The color used is controled by an optional keyword argument, "label".
+        This should be a string containing label names separated by space.
+        Label names take the form of "topic.type". For example, ui.debug()
+        issues a label of "ui.debug".
 
         When labeling output for a specific command, a label of
         "cmdname.type" is recommended. For example, status issues
         a label of "status.modified" for modified files.
         '''
         if self._buffers and not opts.get('prompt', False):
-            self._buffers[-1].extend(a for a in args)
+            if self._bufferapplylabels:
+                label = opts.get('label', '')
+                self._buffers[-1].extend(self.label(a, label) for a in args)
+            else:
+                self._buffers[-1].extend(args)
+        elif self._colormode == 'win32':
+            for a in args:
+                # windows color printing is its own can of crab, defer to
+                # the color module and that is it.
+                color.win32print(a, self.fout.write, **opts)
         else:
+            msgs = args
+            if self._colormode is not None:
+                label = opts.get('label', '')
+                msgs = [self.label(a, label) for a in args]
             self._progclear()
             # opencode timeblockedsection because this is a critical path
             starttime = util.timer()
             try:
-                for a in args:
+                for a in msgs:
                     self.fout.write(a)
             finally:
                 self._blockedtimes['stdio_blocked'] += \