[PATCH] Enable the coloring of branches command

Jeremy Whitlock jcscoobyrs at gmail.com
Thu Jul 29 00:05:50 CDT 2010


# HG changeset patch
# User Jeremy Whitlock <jcscoobyrs at gmail.com>
# Date 1280379903 21600
# Node ID 6e8c4a29dd57b78f8f9f3ba4241ba25480dd08ea
# Parent  efcdf6a953a09efbf06129556a1d07c3ee04e0e0
Enable the coloring of branches command.

This commit updates the branch command to use ui.label for the branch names and
the changeset.  This implementation allows assigning colors to the four states
of a branch: current, active, inactive and closed.  While you can configure
color for the four states, only current and closed have default colors of green
and black bold respectively.

diff -r efcdf6a953a0 -r 6e8c4a29dd57 hgext/color.py
--- a/hgext/color.py	Tue Jul 27 16:04:00 2010 -0500
+++ b/hgext/color.py	Wed Jul 28 23:05:03 2010 -0600
@@ -26,7 +26,7 @@
 
 Other effects in addition to color, like bold and underlined text, are
 also available. Effects are rendered with the ECMA-48 SGR control
-function (aka ANSI escape codes). This module also provides the
+function (aka ANSI escape codesb). This module also provides the
 render_text function, which can be used to add effects to any text.
 
 Default effects may be overridden from the .hgrc file::
@@ -62,6 +62,9 @@
 
   bookmarks.current = green
 
+  branches.closed = black bold
+  branches.current = green
+
 The color extension will try to detect whether to use ANSI codes or
 Win32 console APIs, unless it is made explicit::
 
@@ -87,6 +90,8 @@
             'cyan_background': 46, 'white_background': 47}
 
 _styles = {'grep.match': 'red bold',
+           'branches.closed': 'black bold',
+           'branches.current': 'green',
            'diff.changed': 'white',
            'diff.deleted': 'red',
            'diff.diffline': 'bold',
diff -r efcdf6a953a0 -r 6e8c4a29dd57 mercurial/commands.py
--- a/mercurial/commands.py	Tue Jul 27 16:04:00 2010 -0500
+++ b/mercurial/commands.py	Wed Jul 28 23:05:03 2010 -0600
@@ -512,16 +512,22 @@
             else:
                 hn = repo.lookup(node)
                 if isactive:
+                    label = 'branches.active'
                     notice = ''
                 elif hn not in repo.branchheads(tag, closed=False):
                     if not closed:
                         continue
+                    label = 'branches.closed'
                     notice = _(' (closed)')
                 else:
+                    label = 'branches.inactive'
                     notice = _(' (inactive)')
+                if tag == repo.dirstate.branch():
+                    label = 'branches.current'
                 rev = str(node).rjust(31 - encoding.colwidth(encodedtag))
-                data = encodedtag, rev, hexfunc(hn), notice
-                ui.write("%s %s:%s%s\n" % data)
+                rev = ui.label('%s:%s' % (rev, hexfunc(hn)), 'log.changeset')
+                encodedtag = ui.label(encodedtag, label)
+                ui.write("%s %s%s\n" % (encodedtag, rev, notice))
 
 def bundle(ui, repo, fname, dest=None, **opts):
     """create a changegroup file
diff -r efcdf6a953a0 -r 6e8c4a29dd57 tests/test-branches
--- a/tests/test-branches	Tue Jul 27 16:04:00 2010 -0500
+++ b/tests/test-branches	Wed Jul 28 23:05:03 2010 -0600
@@ -1,5 +1,8 @@
 #!/bin/sh
 
+echo "[extensions]" >> $HGRCPATH
+echo "color =" >> $HGRCPATH
+
 hg init a
 cd a
 echo 'root' >root
@@ -45,9 +48,9 @@
 hg branch 'a branch name much longer than the default justification used by branches'
 hg commit -d '6 0' -m "Adding d branch"
 
-hg branches
+hg branches --color=never
 echo '-------'
-hg branches -a
+hg branches --color=never -a
 
 echo "--- Branch a"
 hg log -b a
@@ -56,7 +59,7 @@
 hg log -b b
 
 echo "---- going to test branch closing"
-hg branches
+hg branches --color=never
 hg up -C b
 echo 'xxx1' >> b
 hg commit -d '7 0' -m 'adding cset to branch b'
@@ -65,23 +68,23 @@
 hg commit -d '8 0' -m 'adding head to branch b'
 echo 'xxx3' >> b
 hg commit -d '9 0' -m 'adding another cset to branch b'
-hg branches
+hg branches --color=never
 hg heads --closed
 hg heads
 hg commit -d '9 0' --close-branch -m 'prune bad branch'
-hg branches -a
+hg branches --color=never -a
 hg up -C b
 hg commit -d '9 0' --close-branch -m 'close this part branch too'
 echo '--- b branch should be inactive'
-hg branches
-hg branches -c
-hg branches -a
+hg branches --color=never
+hg branches --color=never -c
+hg branches --color=never -a
 hg heads b
 hg heads --closed b
 echo 'xxx4' >> b
 hg commit -d '9 0' -m 'reopen branch with a change'
 echo '--- branch b is back in action'
-hg branches -a
+hg branches --color=never -a
 echo '---- test heads listings'
 hg heads
 echo '% branch default'
@@ -92,3 +95,25 @@
 echo '% branch b'
 hg heads b
 hg heads --closed b
+
+hg up -C c
+hg commit -d '9 0' --close-branch -m 'reclosing this branch'
+hg up -C b
+echo '-------'
+hg branches --color=always
+echo '-------'
+hg branches --color=always --closed
+
+echo "[extensions]" >> $HGRCPATH
+echo "color =" >> $HGRCPATH
+echo "[color]" >> $HGRCPATH
+echo "branches.active = green" >> $HGRCPATH
+echo "branches.closed = blue" >> $HGRCPATH
+echo "branches.current = red" >> $HGRCPATH
+echo "branches.inactive = magenta" >> $HGRCPATH
+echo "log.changeset = cyan" >> $HGRCPATH
+
+echo '-------'
+hg branches --color=always
+echo '-------'
+hg branches --color=always --closed
diff -r efcdf6a953a0 -r 6e8c4a29dd57 tests/test-branches.out
--- a/tests/test-branches.out	Tue Jul 27 16:04:00 2010 -0500
+++ b/tests/test-branches.out	Wed Jul 28 23:05:03 2010 -0600
@@ -242,3 +242,27 @@
 date:        Thu Jan 01 00:00:09 1970 +0000
 summary:     prune bad branch
 
+3 files updated, 0 files merged, 2 files removed, 0 files unresolved
+2 files updated, 0 files merged, 3 files removed, 0 files unresolved
+-------
+b                             13:6ac12926b8c3
+a branch name much longer than the default justification used by branches 7:10ff5895aa57
+a                              5:d8cbc61dbaa6 (inactive)
+default                        0:19709c5a4e75 (inactive)
+-------
+b                             13:6ac12926b8c3
+a branch name much longer than the default justification used by branches 7:10ff5895aa57
+c                             14:717d2e6fabe1 (closed)
+a                              5:d8cbc61dbaa6 (inactive)
+default                        0:19709c5a4e75 (inactive)
+-------
+b                             13:6ac12926b8c3
+a branch name much longer than the default justification used by branches 7:10ff5895aa57
+a                              5:d8cbc61dbaa6 (inactive)
+default                        0:19709c5a4e75 (inactive)
+-------
+b                             13:6ac12926b8c3
+a branch name much longer than the default justification used by branches 7:10ff5895aa57
+c                             14:717d2e6fabe1 (closed)
+a                              5:d8cbc61dbaa6 (inactive)
+default                        0:19709c5a4e75 (inactive)


More information about the Mercurial-devel mailing list