Patchwork [8,of,9,bm-refactor] bookmarks: factor out bookmark printing from commands

login
register
mail settings
Submitter Sean Farley
Date June 21, 2017, 12:29 a.m.
Message ID <e9cbc8f652302c0f69c8.1498004971@1.0.0.127.in-addr.arpa>
Download mbox | patch
Permalink /patch/21563/
State Accepted
Headers show

Comments

Sean Farley - June 21, 2017, 12:29 a.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1498001785 25200
#      Tue Jun 20 16:36:25 2017 -0700
# Branch bm-refactor
# Node ID e9cbc8f652302c0f69c8b05dc7fa6ec2b7eadd8e
# Parent  c81183c6280a88dd02587b0c8106aa8632236003
bookmarks: factor out bookmark printing from commands

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index cc91ebc..b8d31fb 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -767,5 +767,33 @@  def addbookmarks(repo, names, rev=None, 
         if not inactive and cur == marks[newact] and not rev:
             activate(repo, newact)
         elif cur != tgt and newact == repo._activebookmark:
             deactivate(repo)
         marks.recordchange(tr)
+
+def printbookmarks(ui, repo, **opts):
+    """print bookmarks to a formatter
+
+    Provides a way for extensions to control how bookmarks are printed.
+    """
+    fm = ui.formatter('bookmarks', opts)
+    hexfn = fm.hexfunc
+    marks = repo._bookmarks
+    if len(marks) == 0 and fm.isplain():
+        ui.status(_("no bookmarks set\n"))
+    for bmark, n in sorted(marks.iteritems()):
+        active = repo._activebookmark
+        if bmark == active:
+            prefix, label = '*', activebookmarklabel
+        else:
+            prefix, label = ' ', ''
+
+        fm.startitem()
+        if not ui.quiet:
+            fm.plain(' %s ' % prefix, label=label)
+        fm.write('bookmark', '%s', bmark, label=label)
+        pad = " " * (25 - encoding.colwidth(bmark))
+        fm.condwrite(not ui.quiet, 'rev node', pad + ' %d:%s',
+                     repo.changelog.rev(n), hexfn(n), label=label)
+        fm.data(active=(bmark == active))
+        fm.plain('\n')
+    fm.end()
diff --git a/mercurial/commands.py b/mercurial/commands.py
index c3a3a64..9d5b9e2 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -977,32 +977,11 @@  def bookmark(ui, repo, *names, **opts):
             elif not repo._activebookmark:
                 ui.status(_("no active bookmark\n"))
             else:
                 bookmarks.deactivate(repo)
     else: # show bookmarks
-        fm = ui.formatter('bookmarks', opts)
-        hexfn = fm.hexfunc
-        marks = repo._bookmarks
-        if len(marks) == 0 and fm.isplain():
-            ui.status(_("no bookmarks set\n"))
-        for bmark, n in sorted(marks.iteritems()):
-            active = repo._activebookmark
-            if bmark == active:
-                prefix, label = '*', bookmarks.activebookmarklabel
-            else:
-                prefix, label = ' ', ''
-
-            fm.startitem()
-            if not ui.quiet:
-                fm.plain(' %s ' % prefix, label=label)
-            fm.write('bookmark', '%s', bmark, label=label)
-            pad = " " * (25 - encoding.colwidth(bmark))
-            fm.condwrite(not ui.quiet, 'rev node', pad + ' %d:%s',
-                         repo.changelog.rev(n), hexfn(n), label=label)
-            fm.data(active=(bmark == active))
-            fm.plain('\n')
-        fm.end()
+        bookmarks.printbookmarks(ui, repo, **opts)
 
 @command('branch',
     [('f', 'force', None,
      _('set branch name even if it shadows an existing branch')),
     ('C', 'clean', None, _('reset branch name to parent branch name'))],