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