[PATCH 3 of 3] color: enable debug option to show labels

Jordi Gutiérrez Hermoso jordigh at octave.org
Sun Aug 24 17:38:34 CDT 2014


# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh at octave.org>
# Date 1408916427 14400
#      Sun Aug 24 17:40:27 2014 -0400
# Node ID 172abbcbcf6ffcd1d17d8ca8100230adb3db27b7
# Parent  8469498daf77ee1426ee9dabdaa68e337382dca7
color: enable debug option to show labels

This is a debug option for showing labels. This can be helpful for
knowing which labels are available for colouring or to see the output
when defining your own templates. A test is included.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -22,7 +22,10 @@ function (aka ANSI escape codes).
 Text receives color effects depending on the labels that it has. Many
 default Mercurial commands emit labelled text. You can also define
 your own labels in templates using the label function, see `hg help
-templates`.
+templates`. Labels are normally invisible. In order to see these
+labels and their position in the text, use the global --color=debug
+option. A single portion of text may have more than one label. In this
+case, the multiple labels will be enclosed by brackets.
 
 The following are the default effects for some default labels. Default
 effects may be overridden from your configuration file::
@@ -179,6 +182,9 @@ def _terminfosetup(ui, mode):
 def _modesetup(ui, coloropt):
     global _terminfo_params
 
+    if coloropt == 'debug':
+        return 'debug'
+
     auto = (coloropt == 'auto')
     always = not auto and util.parsebool(coloropt)
     if not always and not auto:
@@ -393,10 +399,24 @@ class colorui(uimod.ui):
             return super(colorui, self).write_err(
                 *[self.label(str(a), label) for a in args], **opts)
 
+    def showlabel(self, msg, label):
+        if ' ' in label:
+            label = '[' + label + ']'
+        if label:
+            if msg and msg[-1] == '\n':
+                return "%s(%s)\n" % (label, msg[:-1])
+            else:
+                return "%s(%s)" % (label, msg)
+        else:
+            return msg
+
     def label(self, msg, label):
         if self._colormode is None:
             return super(colorui, self).label(msg, label)
 
+        if self._colormode == 'debug':
+            return self.showlabel(msg, label)
+
         effects = []
         for l in label.split():
             s = _styles.get(l, '')
@@ -442,7 +462,7 @@ def uisetup(ui):
     def colorcmd(orig, ui_, opts, cmd, cmdfunc):
         mode = _modesetup(ui_, opts['color'])
         colorui._colormode = mode
-        if mode:
+        if mode and mode != 'debug':
             extstyles()
             configstyles(ui_)
         return orig(ui_, opts, cmd, cmdfunc)
@@ -452,9 +472,9 @@ def uisetup(ui):
 def extsetup(ui):
     commands.globalopts.append(
         ('', 'color', 'auto',
-         # i18n: 'always', 'auto', and 'never' are keywords and should
-         # not be translated
-         _("when to colorize (boolean, always, auto, or never)"),
+         # i18n: 'always', 'auto', 'never', and 'debug' are keywords
+         # and should not be translated
+         _("when to colorize (boolean, always, auto, never, or debug)"),
          _('TYPE')))
 
 @command('debugcolor', [], 'hg debugcolor')
diff --git a/tests/test-status-color.t b/tests/test-status-color.t
--- a/tests/test-status-color.t
+++ b/tests/test-status-color.t
@@ -20,6 +20,14 @@ hg status in repo root:
   \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4mb/in_b\x1b[0m (esc)
   \x1b[0;35;1;4m? \x1b[0m\x1b[0;35;1;4min_root\x1b[0m (esc)
 
+  $ hg status --color=debug
+  status.unknown(? )status.unknown(a/1/in_a_1)
+  status.unknown(? )status.unknown(a/in_a)
+  status.unknown(? )status.unknown(b/1/in_b_1)
+  status.unknown(? )status.unknown(b/2/in_b_2)
+  status.unknown(? )status.unknown(b/in_b)
+  status.unknown(? )status.unknown(in_root)
+
 hg status . in repo root:
 
   $ hg status --color=always .
@@ -137,6 +145,13 @@ Make sure ui.formatted=False works
   adding deleted
   adding modified
   adding removed
+  $ hg log --color=debug
+  [log.changeset changeset.draft](changeset:   0:389aef86a55e)
+  log.tag(tag:         tip)
+  log.user(user:        test)
+  log.date(date:        Thu Jan 01 00:00:00 1970 +0000)
+  log.summary(summary:     initial checkin)
+  
   $ touch modified added unknown ignored
   $ hg add added
   $ hg remove removed


More information about the Mercurial-devel mailing list