topic: simplify _showlasttouched and _getlasttouched (and its data structure)
authorAnton Shestakov <av6@dwimlabs.net>
Mon, 03 Dec 2018 14:15:00 +0800
changeset 4302 8e9940f1ae56
parent 4301 5cbaf5d25443
child 4303 78700a59192a
topic: simplify _showlasttouched and _getlasttouched (and its data structure)
hgext3rd/topic/__init__.py
--- a/hgext3rd/topic/__init__.py	Thu Dec 13 18:57:49 2018 +0100
+++ b/hgext3rd/topic/__init__.py	Mon Dec 03 14:15:00 2018 +0800
@@ -1026,50 +1026,45 @@
 
 def _showlasttouched(repo, fm, opts):
     topics = repo.topics
-    timedict = _getlasttouched(repo, topics)
-    times = timedict.keys()
-    times.sort()
     if topics:
         maxwidth = max(len(t) for t in topics)
         namemask = '%%-%is' % maxwidth
     activetopic = repo.currenttopic
-    for timevalue in times:
-        curtopics = sorted(timedict[timevalue][1])
-        for topic, user in curtopics:
-            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 timevalue == -1:
-                timestr = 'empty and active'
-            else:
-                timestr = templatefilters.age(timedict[timevalue][0])
-            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')
+    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 dictionary of seconds
-    passed from current time for a topic as keys and topic name as values.
+    Calculates the last time a topic was used. Returns a generator of 4-tuples:
+    (age in seconds, topic name, date, and user who last touched the topic).
     """
-    topicstime = {}
     curtime = time.time()
-    for t in topics:
-        secspassed = -1
+    for topic in topics:
+        age = -1
         user = None
         maxtime = (0, 0)
-        trevs = repo.revs("topic(%s)", t)
+        trevs = repo.revs("topic(%s)", topic)
         # Need to check for the time of all changesets in the topic, whether
         # they are obsolete of non-heads
         # XXX: can we just rely on the max rev number for this
@@ -1090,16 +1085,10 @@
                     maxtime = rt
 
         username = stack.parseusername(user)
-        topicuser = (t, username)
+        if trevs:
+            age = curtime - maxtime[0]
 
-        if trevs:
-            secspassed = (curtime - maxtime[0])
-        try:
-            topicstime[secspassed][1].append(topicuser)
-        except KeyError:
-            topicstime[secspassed] = (maxtime, [topicuser])
-
-    return topicstime
+        yield (age, topic, maxtime, username)
 
 def summaryhook(ui, repo):
     t = getattr(repo, 'currenttopic', '')