diff --git a/hgext/beautifygraph.py b/hgext/beautifygraph.py new file mode 100644 --- /dev/null +++ b/hgext/beautifygraph.py @@ -0,0 +1,59 @@ +# -*- coding: UTF-8 -*- +# beautifygraph.py - improve graph output by using Unicode characters + +'''This extension beautifies log -G output by using Unicode characters. + + A terminal with UTF-8 support and a monospace Unicode font are required. +''' + +from __future__ import absolute_import + +import sys +from mercurial import extensions +from mercurial import templatekw +from mercurial import graphmod + +# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for +# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should +# be specifying the version(s) of Mercurial they are tested with, or +# leave the attribute unspecified. +testedwith = 'ships-with-hg-core' + +def extsetup(ui): + def convertedges(line): + def prettyedge(before, edge, after): + if edge == '~': return '╧' + if edge == 'X': return '╳' + if edge == '/': return '╱' + if edge == '-': return '─' + if edge == '|': return '│' + if edge == ':': return '┆' + if edge == '\\': return '╲' + if edge == '+': + if before == ' ' and not after == ' ': return '├' + if after == ' ' and not before == ' ': return '┤' + return '┼' + return edge + line = ' %s ' % line + pretty = []; + for idx in xrange(len(line) - 2): + pretty.append(prettyedge(line[idx], line[idx+1], line[idx+2])) + return ''.join(pretty) + + def getprettygraphnode(orig, *args, **kwargs): + node = orig(*args, **kwargs) + if node == 'o': return '◯' + if node == '@': return '⌾' + if node == '*': return '∗' + if node == 'x': return '◌' + if node == '_': return '╤' + return node + + def outputprettygraph(orig, ui, graph, *args, **kwargs): + if not ui.plain('graph') and sys.stdout.encoding == "UTF-8": + (edges, text) = zip(*graph) + graph = zip([convertedges(e) for e in edges], text) + return orig(ui, graph, *args, **kwargs) + + extensions.wrapfunction(graphmod, 'outputgraph', outputprettygraph) + extensions.wrapfunction(templatekw, 'getgraphnode', getprettygraphnode)