graph: add outputgraph() function, called by ascii() to print
authorJohn Stiles <johnstiles@gmail.com>
Thu, 24 May 2018 23:05:12 -0700
changeset 38150 24e517600b29
parent 38149 d1690a64268e
child 38151 0dfa27e53c57
graph: add outputgraph() function, called by ascii() to print the graph to the ui. This allows a cleaner entrypoint for extensions to tweak the graph output without needing to rewrite all of ascii(), or needing to manually guess where the graph nodes/edges end and the rev note portion begins. This patch does not affect graph output or behavior in any way. Differential Revision: https://phab.mercurial-scm.org/D3655
mercurial/graphmod.py
--- a/mercurial/graphmod.py	Wed Feb 28 03:07:48 2018 +0530
+++ b/mercurial/graphmod.py	Thu May 24 23:05:12 2018 -0700
@@ -341,6 +341,22 @@
         'graphshorten': False,
     }
 
+def outputgraph(ui, graph):
+    """outputs an ASCII graph of a DAG
+
+    this is a helper function for 'ascii' below.
+
+    takes the following arguments:
+
+    - ui to write to
+    - graph data: list of { graph nodes/edges, text }
+
+    this function can be monkey-patched by extensions to alter graph display
+    without needing to mimic all of the edge-fixup logic in ascii()
+    """
+    for (ln, logstr) in graph:
+        ui.write((ln + logstr).rstrip() + "\n")
+
 def ascii(ui, state, type, char, text, coldata):
     """prints an ASCII graph of the DAG
 
@@ -469,9 +485,8 @@
 
     # print lines
     indentation_level = max(ncols, ncols + coldiff)
-    for (line, logstr) in zip(lines, text):
-        ln = "%-*s %s" % (2 * indentation_level, "".join(line), logstr)
-        ui.write(ln.rstrip() + '\n')
+    lines = ["%-*s " % (2 * indentation_level, "".join(line)) for line in lines]
+    outputgraph(ui, zip(lines, text))
 
     # ... and start over
     state['lastcoldiff'] = coldiff