annotate hgext/beautifygraph.py @ 39023:858fe9625dab

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