Patchwork [V3] color: consolidate cut-and-paste code

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date March 11, 2015, 1:51 p.m.
Message ID <b988792457a86c797e14.1426081910@Iris>
Download mbox | patch
Permalink /patch/8003/
State Accepted
Commit e98f2078e4675e1cff89158e13b4b8ac94c41902
Headers show

Comments

Jordi Gutiérrez Hermoso - March 11, 2015, 1:51 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1426080862 14400
#      Wed Mar 11 09:34:22 2015 -0400
# Node ID b988792457a86c797e1475dcb1ae1cb4d1d24a22
# Parent  60c279ab7bd3bb08779cd6c74230d6739a63ebea
color: consolidate cut-and-paste code

This fixes a mild case of cut-and-paste code regarding failing to set
terminal modes. This is evident in the win32 comment that is misplaced
for the terminfo mode since cset ad6ad51cc0dd.

Instead, we refactor this C&P into a small local function.
Augie Fackler - March 11, 2015, 4:50 p.m.
On Wed, Mar 11, 2015 at 09:51:50AM -0400, Jordi Gutiérrez Hermoso wrote:
> # HG changeset patch
> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> # Date 1426080862 14400
> #      Wed Mar 11 09:34:22 2015 -0400
> # Node ID b988792457a86c797e1475dcb1ae1cb4d1d24a22
> # Parent  60c279ab7bd3bb08779cd6c74230d6739a63ebea
> color: consolidate cut-and-paste code

Queued, thanks

>
> This fixes a mild case of cut-and-paste code regarding failing to set
> terminal modes. This is evident in the win32 comment that is misplaced
> for the terminfo mode since cset ad6ad51cc0dd.
>
> Instead, we refactor this C&P into a small local function.
>
> diff --git a/hgext/color.py b/hgext/color.py
> --- a/hgext/color.py
> +++ b/hgext/color.py
> @@ -251,12 +251,16 @@ def _modesetup(ui, coloropt):
>          else:
>              realmode = 'ansi'
>
> +    def modewarn():
> +        # only warn if color.mode was explicitly set and we're in
> +        # an interactive terminal
> +        if mode == realmode and ui.interactive():
> +            ui.warn(_('warning: failed to set color mode to %s\n') % mode)
> +
>      if realmode == 'win32':
>          _terminfo_params = {}
>          if not w32effects:
> -            if mode == 'win32' and ui.interactive():
> -                # only warn if color.mode is explicitly set to win32
> -                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
> +            modewarn()
>              return None
>          _effects.update(w32effects)
>      elif realmode == 'ansi':
> @@ -264,10 +268,8 @@ def _modesetup(ui, coloropt):
>      elif realmode == 'terminfo':
>          _terminfosetup(ui, mode)
>          if not _terminfo_params:
> -            if mode == 'terminfo' and ui.interactive():
> -                ## FIXME Shouldn't we return None in this case too?
> -                # only warn if color.mode is explicitly set to win32
> -                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
> +            ## FIXME Shouldn't we return None in this case too?
> +            modewarn()
>              realmode = 'ansi'
>      else:
>          return None
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -251,12 +251,16 @@  def _modesetup(ui, coloropt):
         else:
             realmode = 'ansi'
 
+    def modewarn():
+        # only warn if color.mode was explicitly set and we're in
+        # an interactive terminal
+        if mode == realmode and ui.interactive():
+            ui.warn(_('warning: failed to set color mode to %s\n') % mode)
+
     if realmode == 'win32':
         _terminfo_params = {}
         if not w32effects:
-            if mode == 'win32' and ui.interactive():
-                # only warn if color.mode is explicitly set to win32
-                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
+            modewarn()
             return None
         _effects.update(w32effects)
     elif realmode == 'ansi':
@@ -264,10 +268,8 @@  def _modesetup(ui, coloropt):
     elif realmode == 'terminfo':
         _terminfosetup(ui, mode)
         if not _terminfo_params:
-            if mode == 'terminfo' and ui.interactive():
-                ## FIXME Shouldn't we return None in this case too?
-                # only warn if color.mode is explicitly set to win32
-                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
+            ## FIXME Shouldn't we return None in this case too?
+            modewarn()
             realmode = 'ansi'
     else:
         return None