topic: merge _showlasttouched logic into _listtopics
authorAnton Shestakov <av6@dwimlabs.net>
Mon, 03 Dec 2018 14:41:03 +0800
changeset 4303 78700a59192a
parent 4302 8e9940f1ae56
child 4310 980565468003
topic: merge _showlasttouched logic into _listtopics Makes --age work with --verbose.
CHANGELOG
hgext3rd/topic/__init__.py
tests/test-topic.t
--- a/CHANGELOG	Mon Dec 03 14:15:00 2018 +0800
+++ b/CHANGELOG	Mon Dec 03 14:41:03 2018 +0800
@@ -15,6 +15,7 @@
   * compat: drop compatibility with Mercurial 4.3
   * topics: improve the message around topic changing
   * stack: introduce a --children flag (see help for details)
+  * topic: make --age compatible with the usual other display for `hg topic`
 
 8.3.3 - in progress
 -------------------
--- 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:
--- a/tests/test-topic.t	Mon Dec 03 14:15:00 2018 +0800
+++ b/tests/test-topic.t	Mon Dec 03 14:41:03 2018 +0800
@@ -819,7 +819,7 @@
   s0^ Add file delta (base current)
 
   $ hg topics --age
-   * fran (1970-01-01 by test)
+   * fran (1970-01-01 by test, 1 changesets)
 
   $ cd ..
 
@@ -903,18 +903,23 @@
    * topic2010 (1 changesets)
 
   $ hg topics --age
-   * topic2010 (2010-01-01 by bar)
-     topic1990 (1990-01-01 by foo)
-     topic1970 (1970-01-01 by test)
+   * topic2010 (2010-01-01 by bar, 1 changesets)
+     topic1990 (1990-01-01 by foo, 1 changesets)
+     topic1970 (1970-01-01 by test, 1 changesets)
+
+  $ hg topics --age --verbose
+   * topic2010 (2010-01-01 by bar, on branch: default, 1 changesets)
+     topic1990 (1990-01-01 by foo, on branch: default, 1 changesets)
+     topic1970 (1970-01-01 by test, on branch: default, 1 changesets)
 
   $ hg up topic1970
   switching to topic topic1970
   0 files updated, 0 files merged, 2 files removed, 0 files unresolved
 
   $ hg topics --age
-     topic2010 (2010-01-01 by bar)
-     topic1990 (1990-01-01 by foo)
-   * topic1970 (1970-01-01 by test)
+     topic2010 (2010-01-01 by bar, 1 changesets)
+     topic1990 (1990-01-01 by foo, 1 changesets)
+   * topic1970 (1970-01-01 by test, 1 changesets)
 
   $ hg topics --age random
   abort: cannot use --age while setting a topic