hgext/beautifygraph.py
author Raphaël Gomès <rgomes@octobus.net>
Tue, 29 Mar 2022 18:21:40 +0200
changeset 49126 e7b74bb602a4
parent 48875 6000f5b25c9b
child 49284 d44e3c45f0e4
permissions -rw-r--r--
rust-dirstatemap: add unit tests These were missing and have already proven valuable since they have found two bugs (fixed in previous patches). There may be other behavior to test, but this gives us a decent coverage. Differential Revision: https://phab.mercurial-scm.org/D12524
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    15
from mercurial.i18n import _
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    16
from mercurial import (
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    17
    encoding,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    18
    extensions,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    19
    graphmod,
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38340
diff changeset
    20
    pycompat,
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    21
    templatekw,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    22
)
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
# 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
    25
# 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
    26
# 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
    27
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
testedwith = b'ships-with-hg-core'
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    29
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    30
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    31
def prettyedge(before, edge, after):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
    if edge == b'~':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
        return b'\xE2\x95\xA7'  # U+2567 ╧
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
    if edge == b'/':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
        return b'\xE2\x95\xB1'  # U+2571 ╱
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
    if edge == b'-':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
        return b'\xE2\x94\x80'  # U+2500 ─
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    38
    if edge == b'|':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
        return b'\xE2\x94\x82'  # U+2502 │
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
    if edge == b':':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
        return b'\xE2\x94\x86'  # U+2506 ┆
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    42
    if edge == b'\\':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    43
        return b'\xE2\x95\xB2'  # U+2572 ╲
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
    if edge == b'+':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
        if before == b' ' and not after == b' ':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
            return b'\xE2\x94\x9C'  # U+251C ├
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    47
        if after == b' ' and not before == b' ':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
            return b'\xE2\x94\xA4'  # U+2524 ┤
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    49
        return b'\xE2\x94\xBC'  # U+253C ┼
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    50
    return edge
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    51
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    52
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    53
def convertedges(line):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
    line = b' %s ' % line
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    55
    pretty = []
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38340
diff changeset
    56
    for idx in pycompat.xrange(len(line) - 2):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    57
        pretty.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    58
            prettyedge(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    59
                line[idx : idx + 1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    60
                line[idx + 1 : idx + 2],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    61
                line[idx + 2 : idx + 3],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    62
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    63
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
    return b''.join(pretty)
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    65
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    66
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    67
def getprettygraphnode(orig, *args, **kwargs):
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    68
    node = orig(*args, **kwargs)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
    if node == b'o':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
        return b'\xE2\x97\x8B'  # U+25CB ○
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
    if node == b'@':
46204
36c05ab02232 beautifygraph: change the current commit symbol
msuozzo@google.com
parents: 44345
diff changeset
    72
        return b'\xE2\x97\x89'  # U+25C9 ◉
44345
14d0e89520a2 graphlog: use '%' for other context in merge conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    73
    if node == b'%':
14d0e89520a2 graphlog: use '%' for other context in merge conflict
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    74
        return b'\xE2\x97\x8D'  # U+25CE ◎
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
    if node == b'*':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    76
        return b'\xE2\x88\x97'  # U+2217 ∗
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    77
    if node == b'x':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
        return b'\xE2\x97\x8C'  # U+25CC ◌
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
    if node == b'_':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    80
        return b'\xE2\x95\xA4'  # U+2564 ╤
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    81
    return node
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    82
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    83
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    84
def outputprettygraph(orig, ui, graph, *args, **kwargs):
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    85
    (edges, text) = zip(*graph)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    86
    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
    87
    return orig(ui, graph, *args, **kwargs)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    89
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    90
def extsetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    91
    if ui.plain(b'graph'):
39198
362cb82385ea beautifygraph: don't warn about busted terminal if HGPLAIN is set
Augie Fackler <augie@google.com>
parents: 38340
diff changeset
    92
        return
362cb82385ea beautifygraph: don't warn about busted terminal if HGPLAIN is set
Augie Fackler <augie@google.com>
parents: 38340
diff changeset
    93
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    94
    if encoding.encoding != b'UTF-8':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
        ui.warn(_(b'beautifygraph: unsupported encoding, UTF-8 required\n'))
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    96
        return
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    97
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    98
    if 'A' in encoding._wide:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    99
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
   100
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
                b'beautifygraph: unsupported terminal settings, '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
                b'monospace narrow text required\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
   103
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
   104
        )
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
   105
        return
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
   106
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    extensions.wrapfunction(graphmod, b'outputgraph', outputprettygraph)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   108
    extensions.wrapfunction(templatekw, b'getgraphnode', getprettygraphnode)