hgext3rd/topic/__init__.py
changeset 4303 78700a59192a
parent 4302 8e9940f1ae56
child 4320 b4bc185bcefb
--- a/hgext3rd/topic/__init__.py	Mon Dec 03 14:15:00 2018 +0800
+++ b/hgext3rd/topic/__init__.py	Mon Dec 03 14:41:03 2018 +0800
@@ -956,17 +956,21 @@
 
 def _listtopics(ui, repo, opts):
     fm = ui.formatter('topics', opts)
-    showlast = opts.get('age')
-    if showlast:
-        # we have a new function as plugging logic into existing function is
-        # pretty much difficult
-        return _showlasttouched(repo, fm, opts)
     activetopic = repo.currenttopic
     namemask = '%s'
     if repo.topics:
         maxwidth = max(len(t) for t in repo.topics)
         namemask = '%%-%is' % maxwidth
-    for topic in sorted(repo.topics):
+    if opts.get('age'):
+        # here we sort by age and topic name
+        topicsdata = sorted(_getlasttouched(repo, repo.topics))
+    else:
+        # here we sort by topic name only
+        topicsdata = (
+            (None, topic, None, None)
+            for topic in sorted(repo.topics)
+        )
+    for age, topic, date, user in topicsdata:
         fm.startitem()
         marker = ' '
         label = 'topic'
@@ -983,8 +987,18 @@
         if ui.quiet:
             fm.plain('\n')
             continue
+        fm.plain(' (')
+        if date:
+            if age == -1:
+                timestr = 'empty and active'
+            else:
+                timestr = templatefilters.age(date)
+            fm.write('lasttouched', '%s', timestr, label='topic.list.time')
+        if user:
+            fm.write('usertouched', ' by %s', user, label='topic.list.user')
+        if date:
+            fm.plain(', ')
         data = stack.stack(repo, topic=topic)
-        fm.plain(' (')
         if ui.verbose:
             fm.write('branches+', 'on branch: %s',
                      '+'.join(data.branches), # XXX use list directly after 4.0 is released
@@ -1024,36 +1038,6 @@
         fm.plain(')\n')
     fm.end()
 
-def _showlasttouched(repo, fm, opts):
-    topics = repo.topics
-    if topics:
-        maxwidth = max(len(t) for t in topics)
-        namemask = '%%-%is' % maxwidth
-    activetopic = repo.currenttopic
-    topicsdata = sorted(_getlasttouched(repo, topics))
-    for age, topic, date, user in topicsdata:
-        fm.startitem()
-        marker = ' '
-        label = 'topic'
-        active = (topic == activetopic)
-        if active:
-            marker = '*'
-            label = 'topic.active'
-        fm.plain(' %s ' % marker, label=label)
-        fm.write('topic', namemask, topic, label=label)
-        fm.data(active=active)
-        fm.plain(' (')
-        if age == -1:
-            timestr = 'empty and active'
-        else:
-            timestr = templatefilters.age(date)
-        fm.write('lasttouched', '%s', timestr, label='topic.list.time')
-        if user:
-            fm.write('usertouched', ' by %s', user, label='topic.list.user')
-        fm.plain(')')
-        fm.plain('\n')
-    fm.end()
-
 def _getlasttouched(repo, topics):
     """
     Calculates the last time a topic was used. Returns a generator of 4-tuples: