Patchwork [6,of,7,bm-refactor,V3] bookmarks: factor out bookmark printing from commands

login
register
mail settings
Submitter Sean Farley
Date June 23, 2017, 6:33 p.m.
Message ID <5caa40d7fa1be3ff13a2.1498242802@1.0.0.127.in-addr.arpa>
Download mbox | patch
Permalink /patch/21642/
State Accepted
Headers show

Comments

Sean Farley - June 23, 2017, 6:33 p.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 5caa40d7fa1be3ff13a2e80a91234809cff5307a
# Parent  e038a46ceca85f7de4605506fda3959d43541826
bookmarks: factor out bookmark printing from commands

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index 99c23e2..518b362 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -764,5 +764,33 @@  def addbookmarks(repo, tr, names, rev=No
     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 3778095..5bee39c 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -978,32 +978,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'))],