Commits

Josh VanderLinden  committed 482e0a2

Enhanced the output of the todo command a bit

  • Participants
  • Parent commits 77fba70

Comments (0)

Files changed (1)

 
 GREP = 'grep'
 
+def format(todo_dict):
+    file_count = len(todo_dict)
+    todo_count = sum(len(todos) for todos in todo_dict.values())
+
+    output = 'Found %i TODOs in %i files\n\n' % (todo_count, file_count)
+
+    for file, todos in todo_dict.items():
+        output += "File:\t%s\n" % file
+
+        for line, todo in todos:
+            output += "\tLine: %s\t%s\n" % (line, todo)
+
+        output += "\n"
+
+    return output
+
 def todo_finder(ui, repo, *pats, **opts):
     """Searches for TODO items in your repository"""
 
         def capture(self, text, eol):
             self.matches.append(text + eol)
 
-    format = lambda matches: ['File:\t%s\nLine:\t%s\n%s\n\n' % (m[0], m[2], m[3]) for m in todos]
-
     captor = WriteCaptor()
 
     orig_write = ui.write
     ui.write = orig_write
 
     todos = TODO_RE.findall('\n'.join(captor.matches))
+
+    # group the TODOs by file
+    todo_dict = {}
+    for file, rev, line, todo in todos:
+        if not todo_dict.has_key(file):
+            todo_dict[file] = []
+
+        todo_dict[file].append((line, todo))
+
+    formatted = format(todo_dict)
+
     if persist:
-        open(persist, 'w').writelines(format(todos))
+        open(persist, 'w').write(formatted)
     else:
-        ui.write('%s' % ''.join(format(todos)))
+        ui.write(formatted)
 
 cmdtable = {
     '^todo|tg': (todo_finder, def_prop(GREP, 1) + [