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