diff hgext3rd/topic/__init__.py @ 1975:acbbf7f0751e

topic: add formatter support Using formatter to output the list of topic will add support for color and richer templater. We add some test for output control. We introduce a dedicated test to help with coming more complexe output.
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Sat, 13 Aug 2016 00:37:33 +0200
parents 20fb4195bfc4
children ebdc2a6a9a25
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Sat Aug 13 00:13:05 2016 +0200
+++ b/hgext3rd/topic/__init__.py	Sat Aug 13 00:37:33 2016 +0200
@@ -263,7 +263,7 @@
             with repo.vfs.open('topic', 'w') as f:
                 f.write(topic)
         return
-    _listtopics(ui, repo)
+    _listtopics(ui, repo, opts)
 
 @command('stack [TOPIC]', [] + commands.formatteropts)
 def cmdstack(ui, repo, topic='', **opts):
@@ -272,11 +272,24 @@
     List the current topic by default."""
     return stack.showstack(ui, repo, topic, opts)
 
-def _listtopics(ui, repo):
-    current = repo.currenttopic
-    for t in sorted(repo.topics):
-        marker = '*' if t == current else ' '
-        ui.write(' %s %s\n' % (marker, t))
+def _listtopics(ui, repo, opts):
+    fm = ui.formatter('bookmarks', opts)
+    activetopic = repo.currenttopic
+    for topic in sorted(repo.topics):
+        fm.startitem()
+        marker = ' '
+        label = 'topic'
+        active = (topic == activetopic)
+        if active:
+            marker = '*'
+            label = 'topic.active'
+        if not ui.quiet:
+            # registering the active data is made explicitly later
+            fm.plain(' %s ' % marker, label=label)
+        fm.write('topic', '%s', topic, label=label)
+        fm.data(active=active)
+        fm.plain('\n')
+    fm.end()
 
 def summaryhook(ui, repo):
     t = repo.currenttopic