Show More
@@ -1,96 +1,94 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': |
|
|||
35 | return '\xE2\x95\xB3' # U+2573 ╳ |
|
|||
36 | if edge == '/': |
|
34 | if edge == '/': | |
37 | return '\xE2\x95\xB1' # U+2571 ╱ |
|
35 | return '\xE2\x95\xB1' # U+2571 ╱ | |
38 | if edge == '-': |
|
36 | if edge == '-': | |
39 | return '\xE2\x94\x80' # U+2500 ─ |
|
37 | return '\xE2\x94\x80' # U+2500 ─ | |
40 | if edge == '|': |
|
38 | if edge == '|': | |
41 | return '\xE2\x94\x82' # U+2502 │ |
|
39 | return '\xE2\x94\x82' # U+2502 │ | |
42 | if edge == ':': |
|
40 | if edge == ':': | |
43 | return '\xE2\x94\x86' # U+2506 ┆ |
|
41 | return '\xE2\x94\x86' # U+2506 ┆ | |
44 | if edge == '\\': |
|
42 | if edge == '\\': | |
45 | return '\xE2\x95\xB2' # U+2572 ╲ |
|
43 | return '\xE2\x95\xB2' # U+2572 ╲ | |
46 | if edge == '+': |
|
44 | if edge == '+': | |
47 | if before == ' ' and not after == ' ': |
|
45 | if before == ' ' and not after == ' ': | |
48 | return '\xE2\x94\x9C' # U+251C ├ |
|
46 | return '\xE2\x94\x9C' # U+251C ├ | |
49 | if after == ' ' and not before == ' ': |
|
47 | if after == ' ' and not before == ' ': | |
50 | return '\xE2\x94\xA4' # U+2524 ┤ |
|
48 | return '\xE2\x94\xA4' # U+2524 ┤ | |
51 | return '\xE2\x94\xBC' # U+253C ┼ |
|
49 | return '\xE2\x94\xBC' # U+253C ┼ | |
52 | return edge |
|
50 | return edge | |
53 |
|
51 | |||
54 | def convertedges(line): |
|
52 | def convertedges(line): | |
55 | line = ' %s ' % line |
|
53 | line = ' %s ' % line | |
56 | pretty = [] |
|
54 | pretty = [] | |
57 | for idx in pycompat.xrange(len(line) - 2): |
|
55 | for idx in pycompat.xrange(len(line) - 2): | |
58 | pretty.append(prettyedge(line[idx:idx + 1], |
|
56 | pretty.append(prettyedge(line[idx:idx + 1], | |
59 | line[idx + 1:idx + 2], |
|
57 | line[idx + 1:idx + 2], | |
60 | line[idx + 2:idx + 3])) |
|
58 | line[idx + 2:idx + 3])) | |
61 | return ''.join(pretty) |
|
59 | return ''.join(pretty) | |
62 |
|
60 | |||
63 | def getprettygraphnode(orig, *args, **kwargs): |
|
61 | def getprettygraphnode(orig, *args, **kwargs): | |
64 | node = orig(*args, **kwargs) |
|
62 | node = orig(*args, **kwargs) | |
65 | if node == 'o': |
|
63 | if node == 'o': | |
66 | return '\xE2\x97\x8B' # U+25CB ○ |
|
64 | return '\xE2\x97\x8B' # U+25CB ○ | |
67 | if node == '@': |
|
65 | if node == '@': | |
68 | return '\xE2\x97\x8D' # U+25CD ◍ |
|
66 | return '\xE2\x97\x8D' # U+25CD ◍ | |
69 | if node == '*': |
|
67 | if node == '*': | |
70 | return '\xE2\x88\x97' # U+2217 ∗ |
|
68 | return '\xE2\x88\x97' # U+2217 ∗ | |
71 | if node == 'x': |
|
69 | if node == 'x': | |
72 | return '\xE2\x97\x8C' # U+25CC ◌ |
|
70 | return '\xE2\x97\x8C' # U+25CC ◌ | |
73 | if node == '_': |
|
71 | if node == '_': | |
74 | return '\xE2\x95\xA4' # U+2564 ╤ |
|
72 | return '\xE2\x95\xA4' # U+2564 ╤ | |
75 | return node |
|
73 | return node | |
76 |
|
74 | |||
77 | def outputprettygraph(orig, ui, graph, *args, **kwargs): |
|
75 | def outputprettygraph(orig, ui, graph, *args, **kwargs): | |
78 | (edges, text) = zip(*graph) |
|
76 | (edges, text) = zip(*graph) | |
79 | graph = zip([convertedges(e) for e in edges], text) |
|
77 | graph = zip([convertedges(e) for e in edges], text) | |
80 | return orig(ui, graph, *args, **kwargs) |
|
78 | return orig(ui, graph, *args, **kwargs) | |
81 |
|
79 | |||
82 | def extsetup(ui): |
|
80 | def extsetup(ui): | |
83 | if ui.plain('graph'): |
|
81 | if ui.plain('graph'): | |
84 | return |
|
82 | return | |
85 |
|
83 | |||
86 | if encoding.encoding != 'UTF-8': |
|
84 | if encoding.encoding != 'UTF-8': | |
87 | ui.warn(_('beautifygraph: unsupported encoding, UTF-8 required\n')) |
|
85 | ui.warn(_('beautifygraph: unsupported encoding, UTF-8 required\n')) | |
88 | return |
|
86 | return | |
89 |
|
87 | |||
90 | if r'A' in encoding._wide: |
|
88 | if r'A' in encoding._wide: | |
91 | ui.warn(_('beautifygraph: unsupported terminal settings, ' |
|
89 | ui.warn(_('beautifygraph: unsupported terminal settings, ' | |
92 | 'monospace narrow text required\n')) |
|
90 | 'monospace narrow text required\n')) | |
93 | return |
|
91 | return | |
94 |
|
92 | |||
95 | extensions.wrapfunction(graphmod, 'outputgraph', outputprettygraph) |
|
93 | extensions.wrapfunction(graphmod, 'outputgraph', outputprettygraph) | |
96 | extensions.wrapfunction(templatekw, 'getgraphnode', getprettygraphnode) |
|
94 | extensions.wrapfunction(templatekw, 'getgraphnode', getprettygraphnode) |
General Comments 0
You need to be logged in to leave comments.
Login now