comparison hgext/beautifygraph.py @ 38340:9abe91a503da

graph: 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. Differential Revision: https://phab.mercurial-scm.org/D3665
author John Stiles <johnstiles@gmail.com>
date Fri, 15 Jun 2018 16:32:31 -0700
parents
children e7aa113b14f7 362cb82385ea
comparison
equal deleted inserted replaced
38339:03aa222ca28e 38340:9abe91a503da
1 # -*- coding: UTF-8 -*-
2 # beautifygraph.py - improve graph output by using Unicode characters
3 #
4 # Copyright 2018 John Stiles <johnstiles@gmail.com>
5 #
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
8
9 '''beautify log -G output by using Unicode characters (EXPERIMENTAL)
10
11 A terminal with UTF-8 support and monospace narrow text are required.
12 '''
13
14 from __future__ import absolute_import
15
16 from mercurial.i18n import _
17 from mercurial import (
18 encoding,
19 extensions,
20 graphmod,
21 templatekw,
22 )
23
24 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
26 # be specifying the version(s) of Mercurial they are tested with, or
27 # leave the attribute unspecified.
28 testedwith = 'ships-with-hg-core'
29
30 def prettyedge(before, edge, after):
31 if edge == '~':
32 return '\xE2\x95\xA7' # U+2567 ╧
33 if edge == 'X':
34 return '\xE2\x95\xB3' # U+2573 ╳
35 if edge == '/':
36 return '\xE2\x95\xB1' # U+2571 ╱
37 if edge == '-':
38 return '\xE2\x94\x80' # U+2500 ─
39 if edge == '|':
40 return '\xE2\x94\x82' # U+2502 │
41 if edge == ':':
42 return '\xE2\x94\x86' # U+2506 ┆
43 if edge == '\\':
44 return '\xE2\x95\xB2' # U+2572 ╲
45 if edge == '+':
46 if before == ' ' and not after == ' ':
47 return '\xE2\x94\x9C' # U+251C ├
48 if after == ' ' and not before == ' ':
49 return '\xE2\x94\xA4' # U+2524 ┤
50 return '\xE2\x94\xBC' # U+253C ┼
51 return edge
52
53 def convertedges(line):
54 line = ' %s ' % line
55 pretty = []
56 for idx in xrange(len(line) - 2):
57 pretty.append(prettyedge(line[idx], line[idx + 1], line[idx + 2]))
58 return ''.join(pretty)
59
60 def getprettygraphnode(orig, *args, **kwargs):
61 node = orig(*args, **kwargs)
62 if node == 'o':
63 return '\xE2\x97\x8B' # U+25CB ○
64 if node == '@':
65 return '\xE2\x97\x8D' # U+25CD ◍
66 if node == '*':
67 return '\xE2\x88\x97' # U+2217 ∗
68 if node == 'x':
69 return '\xE2\x97\x8C' # U+25CC ◌
70 if node == '_':
71 return '\xE2\x95\xA4' # U+2564 ╤
72 return node
73
74 def outputprettygraph(orig, ui, graph, *args, **kwargs):
75 (edges, text) = zip(*graph)
76 graph = zip([convertedges(e) for e in edges], text)
77 return orig(ui, graph, *args, **kwargs)
78
79 def extsetup(ui):
80 if encoding.encoding != 'UTF-8':
81 ui.warn(_('beautifygraph: unsupported encoding, UTF-8 required\n'))
82 return
83
84 if 'A' in encoding._wide:
85 ui.warn(_('beautifygraph: unsupported terminal settings, '
86 'monospace narrow text required\n'))
87 return
88
89 if ui.plain('graph'):
90 return
91
92 extensions.wrapfunction(graphmod, 'outputgraph', outputprettygraph)
93 extensions.wrapfunction(templatekw, 'getgraphnode', getprettygraphnode)