mercurial/crecord.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sun, 19 Apr 2020 14:16:24 -0700
branchstable
changeset 44745 6a6c15cea1fa
parent 44311 d3f776c4760e
child 44826 1bab6b61b62b
child 45022 d2227d4c9e6b
permissions -rw-r--r--
contrib: install PyOxidizer in Linux and Windows environments For Linux, this was trivial. For Windows, we need to teach the powershell script to install Rust as well. This was also pretty straightforward. Differential Revision: https://phab.mercurial-scm.org/D8468
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     1
# stuff related specifically to patch manipulation / parsing
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     2
#
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     3
# Copyright 2008 Mark Edgington <edgimar@gmail.com>
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     4
#
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     7
#
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     8
# This code is based on the Mark Edgington's crecord extension.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     9
# (Itself based on Bryan O'Sullivan's record extension.)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    10
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    11
from __future__ import absolute_import
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    12
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    13
import locale
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    14
import os
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    15
import re
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    16
import signal
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    17
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    18
from .i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    19
from .pycompat import (
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    20
    getattr,
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    21
    open,
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    22
)
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    23
from . import (
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    24
    encoding,
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26421
diff changeset
    25
    error,
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    26
    patch as patchmod,
44135
ae596fac8ba0 crecord: fix a concatenation of bytes and str on py3
Kyle Lippincott <spectral@google.com>
parents: 43894
diff changeset
    27
    pycompat,
30328
0911191dc4c9 crecord: use scmutil.termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 29961
diff changeset
    28
    scmutil,
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
    29
    util,
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    30
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    31
from .utils import stringutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    32
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
    33
stringio = util.stringio
24314
348492ba632a crecord: more import style
Matt Mackall <mpm@selenic.com>
parents: 24313
diff changeset
    34
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    35
# patch comments based on the git one
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    36
diffhelptext = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    37
    """# To remove '-' lines, make them ' ' lines (context).
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    38
# To remove '+' lines, delete them.
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    39
# Lines starting with # will be removed from the patch.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    40
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    41
)
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    42
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    43
hunkhelptext = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    44
    """#
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    45
# If the patch applies cleanly, the edited hunk will immediately be
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    46
# added to the record list. If it does not apply cleanly, a rejects file
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    47
# will be generated. You can use that when you try again. If all lines
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    48
# of the hunk are removed, then the edit is aborted and the hunk is left
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    49
# unchanged.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    50
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    51
)
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    53
patchhelptext = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    54
    """#
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    55
# If the patch applies cleanly, the edited patch will immediately
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    56
# be finalised. If it does not apply cleanly, rejects files will be
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    57
# generated. You can use those when you try again.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    58
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    59
)
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    60
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    61
try:
24909
d71492ca2fdd crecord: fix mixed imports warning
Matt Harbison <matt_harbison@yahoo.com>
parents: 24840
diff changeset
    62
    import curses
43425
be0f77fd274d py3: fix handling of ctrl keys in crecord (issue6213)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43424
diff changeset
    63
    import curses.ascii
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    64
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    65
    curses.error
44311
d3f776c4760e py3: catch AttributeError too with ImportError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 44135
diff changeset
    66
except (ImportError, AttributeError):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    67
    # I have no idea if wcurses works with crecord...
24423
01b39e821d00 crecord: conditionalize the imports that are not available on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 24351
diff changeset
    68
    try:
01b39e821d00 crecord: conditionalize the imports that are not available on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 24351
diff changeset
    69
        import wcurses as curses
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    70
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    71
        curses.error
44311
d3f776c4760e py3: catch AttributeError too with ImportError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 44135
diff changeset
    72
    except (ImportError, AttributeError):
27530
ba30ef5bba95 crecord: ensure that curses is False if not imported
Sean Farley <sean@farley.io>
parents: 27529
diff changeset
    73
        # wcurses is not shipped on Windows by default, or python is not
ba30ef5bba95 crecord: ensure that curses is False if not imported
Sean Farley <sean@farley.io>
parents: 27529
diff changeset
    74
        # compiled with curses
ba30ef5bba95 crecord: ensure that curses is False if not imported
Sean Farley <sean@farley.io>
parents: 27529
diff changeset
    75
        curses = False
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    77
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    78
class fallbackerror(error.Abort):
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    79
    """Error that indicates the client should try to fallback to text mode."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    80
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    81
    # Inherits from error.Abort so that existing behavior is preserved if the
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    82
    # calling code does not know how to fallback.
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    83
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    84
27529
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    85
def checkcurses(ui):
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    86
    """Return True if the user wants to use curses
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    87
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    88
    This method returns True if curses is found (and that python is built with
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    89
    it) and that the user has the correct flag for the ui.
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    90
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    91
    return curses and ui.interface(b"chunkselector") == b"curses"
27529
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    92
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
    93
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    94
class patchnode(object):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    95
    """abstract class for patch graph nodes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    96
    (i.e. patchroot, header, hunk, hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    97
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    98
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    99
    def firstchild(self):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
        raise NotImplementedError(b"method must be implemented by subclass")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   101
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   102
    def lastchild(self):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
        raise NotImplementedError(b"method must be implemented by subclass")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   104
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   105
    def allchildren(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   106
        """Return a list of all of the direct children of this node"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
        raise NotImplementedError(b"method must be implemented by subclass")
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
   108
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   109
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   110
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   111
        Return the closest next item of the same type where there are no items
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   112
        of different types between the current item and this closest item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   113
        If no such item exists, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   114
        """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   115
        raise NotImplementedError(b"method must be implemented by subclass")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   116
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   117
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   118
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   119
        Return the closest previous item of the same type where there are no
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   120
        items of different types between the current item and this closest item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   121
        If no such item exists, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   122
        """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   123
        raise NotImplementedError(b"method must be implemented by subclass")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   124
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   125
    def parentitem(self):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
        raise NotImplementedError(b"method must be implemented by subclass")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   127
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   128
    def nextitem(self, skipfolded=True):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   129
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   130
        Try to return the next item closest to this item, regardless of item's
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   131
        type (header, hunk, or hunkline).
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   132
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   133
        If skipfolded == True, and the current item is folded, then the child
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   134
        items that are hidden due to folding will be skipped when determining
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   135
        the next item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   136
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   137
        If it is not possible to get the next item, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   138
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   139
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   140
            itemfolded = self.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   141
        except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   142
            itemfolded = False
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   143
        if skipfolded and itemfolded:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   144
            nextitem = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   145
            if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   146
                try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   147
                    nextitem = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   148
                except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   149
                    nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   150
            return nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   151
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   152
            # try child
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   153
            item = self.firstchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   154
            if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   155
                return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   156
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   157
            # else try next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   158
            item = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   159
            if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   160
                return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   161
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   162
            try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   163
                # else try parent's next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   164
                item = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   165
                if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   166
                    return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   167
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   168
                # else return grandparent's next sibling (or None)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   169
                return self.parentitem().parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   170
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   171
            except AttributeError:  # parent and/or grandparent was None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   172
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   173
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   174
    def previtem(self):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   175
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   176
        Try to return the previous item closest to this item, regardless of
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   177
        item's type (header, hunk, or hunkline).
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   178
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   179
        If it is not possible to get the previous item, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   180
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   181
        # try previous sibling's last child's last child,
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   182
        # else try previous sibling's last child, else try previous sibling
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   183
        prevsibling = self.prevsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   184
        if prevsibling is not None:
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   185
            prevsiblinglastchild = prevsibling.lastchild()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   186
            if (prevsiblinglastchild is not None) and not prevsibling.folded:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   187
                prevsiblinglclc = prevsiblinglastchild.lastchild()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   188
                if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   189
                    prevsiblinglclc is not None
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   190
                ) and not prevsiblinglastchild.folded:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   191
                    return prevsiblinglclc
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   192
                else:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   193
                    return prevsiblinglastchild
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   194
            else:
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   195
                return prevsibling
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   196
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   197
        # try parent (or None)
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   198
        return self.parentitem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   199
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   200
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   201
class patch(patchnode, list):  # todo: rename patchroot
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   202
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   203
    list of header objects representing the patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   204
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   205
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   206
    def __init__(self, headerlist):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   207
        self.extend(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   208
        # add parent patch object reference to each header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   209
        for header in self:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   210
            header.patch = self
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   212
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   213
class uiheader(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   214
    """patch header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   215
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
   216
    xxx shouldn't we move this to mercurial/patch.py ?
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   217
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   218
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   219
    def __init__(self, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   220
        self.nonuiheader = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   221
        # flag to indicate whether to apply this chunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   222
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   223
        # flag which only affects the status display indicating if a node's
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   224
        # children are partially applied (i.e. some applied, some not).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   225
        self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   226
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   227
        # flag to indicate whether to display as folded/unfolded to user
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   228
        self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   229
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   230
        # list of all headers in patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   231
        self.patch = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   232
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   233
        # flag is False if this header was ever unfolded from initial state
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   234
        self.neverunfolded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   235
        self.hunks = [uihunk(h, self) for h in self.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   236
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   237
    def prettystr(self):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   238
        x = stringio()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   239
        self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   240
        return x.getvalue()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   241
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   242
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   243
        numheadersinpatch = len(self.patch)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   244
        indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   245
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   246
        if indexofthisheader < numheadersinpatch - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   247
            nextheader = self.patch[indexofthisheader + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   248
            return nextheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   249
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   250
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   251
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   252
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   253
        indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   254
        if indexofthisheader > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   255
            previousheader = self.patch[indexofthisheader - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   256
            return previousheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   257
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   258
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   259
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   260
    def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   261
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   262
        there is no 'real' parent item of a header that can be selected,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   263
        so return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   264
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   265
        return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   266
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   267
    def firstchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   268
        """return the first child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   269
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   270
        if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   271
            return self.hunks[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   272
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   273
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   274
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   275
    def lastchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   276
        """return the last child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   277
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   278
        if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   279
            return self.hunks[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   280
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   281
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   282
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   283
    def allchildren(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   284
        """return a list of all of the direct children of this node"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   285
        return self.hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   286
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   287
    def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   288
        return getattr(self.nonuiheader, name)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   289
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   290
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   291
class uihunkline(patchnode):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   292
    """represents a changed line in a hunk"""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   293
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   294
    def __init__(self, linetext, hunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   295
        self.linetext = linetext
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   296
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   297
        # the parent hunk to which this line belongs
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   298
        self.hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   299
        # folding lines currently is not used/needed, but this flag is needed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   300
        # in the previtem method.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   301
        self.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   302
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   303
    def prettystr(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   304
        return self.linetext
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   305
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   306
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   307
        numlinesinhunk = len(self.hunk.changedlines)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   308
        indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   309
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   310
        if indexofthisline < numlinesinhunk - 1:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   311
            nextline = self.hunk.changedlines[indexofthisline + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   312
            return nextline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   313
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   314
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   315
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   316
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   317
        indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   318
        if indexofthisline > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   319
            previousline = self.hunk.changedlines[indexofthisline - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   320
            return previousline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   321
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   322
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   323
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   324
    def parentitem(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   325
        """return the parent to the current item"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   326
        return self.hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   327
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   328
    def firstchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   329
        """return the first child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   330
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   331
        # hunk-lines don't have children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   332
        return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   333
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   334
    def lastchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   335
        """return the last child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   336
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   337
        # hunk-lines don't have children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   338
        return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   339
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   340
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   341
class uihunk(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   342
    """ui patch hunk, wraps a hunk and keep track of ui behavior """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   343
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   344
    maxcontext = 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   345
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   346
    def __init__(self, hunk, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   347
        self._hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   348
        self.changedlines = [uihunkline(line, self) for line in hunk.hunk]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   349
        self.header = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   350
        # used at end for detecting how many removed lines were un-applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   351
        self.originalremoved = self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   352
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   353
        # flag to indicate whether to display as folded/unfolded to user
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   354
        self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   355
        # flag to indicate whether to apply this chunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   356
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   357
        # flag which only affects the status display indicating if a node's
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   358
        # children are partially applied (i.e. some applied, some not).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   359
        self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   360
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   361
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   362
        numhunksinheader = len(self.header.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   363
        indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   364
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   365
        if indexofthishunk < numhunksinheader - 1:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   366
            nexthunk = self.header.hunks[indexofthishunk + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   367
            return nexthunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   368
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   369
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   370
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   371
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   372
        indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   373
        if indexofthishunk > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   374
            previoushunk = self.header.hunks[indexofthishunk - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   375
            return previoushunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   376
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   377
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   378
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   379
    def parentitem(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   380
        """return the parent to the current item"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   381
        return self.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   382
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   383
    def firstchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   384
        """return the first child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   385
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   386
        if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   387
            return self.changedlines[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   388
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   389
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   390
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   391
    def lastchild(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   392
        """return the last child of this item, if one exists.  otherwise
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   393
        None."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   394
        if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   395
            return self.changedlines[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   396
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   397
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   398
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   399
    def allchildren(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   400
        """return a list of all of the direct children of this node"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   401
        return self.changedlines
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
   402
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   403
    def countchanges(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   404
        """changedlines -> (n+,n-)"""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   405
        add = len(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   406
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   407
                l
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   408
                for l in self.changedlines
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   409
                if l.applied and l.prettystr().startswith(b'+')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   410
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   411
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   412
        rem = len(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   413
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   414
                l
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   415
                for l in self.changedlines
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   416
                if l.applied and l.prettystr().startswith(b'-')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   417
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   418
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   419
        return add, rem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   420
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   421
    def getfromtoline(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   422
        # calculate the number of removed lines converted to context lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   423
        removedconvertedtocontext = self.originalremoved - self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   424
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   425
        contextlen = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   426
            len(self.before) + len(self.after) + removedconvertedtocontext
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   427
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   428
        if self.after and self.after[-1] == b'\\ No newline at end of file\n':
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   429
            contextlen -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   430
        fromlen = contextlen + self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   431
        tolen = contextlen + self.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   432
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   433
        # diffutils manual, section "2.2.2.2 detailed description of unified
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   434
        # format": "an empty hunk is considered to end at the line that
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   435
        # precedes the hunk."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   436
        #
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   437
        # so, if either of hunks is empty, decrease its line start. --immerrr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   438
        # but only do this if fromline > 0, to avoid having, e.g fromline=-1.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   439
        fromline, toline = self.fromline, self.toline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   440
        if fromline != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   441
            if fromlen == 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   442
                fromline -= 1
37875
03350f5234a4 crecord: fix line number in hunk header (issue5917)
Jun Wu <quark@fb.com>
parents: 37087
diff changeset
   443
            if tolen == 0 and toline > 0:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   444
                toline -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   445
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   446
        fromtoline = b'@@ -%d,%d +%d,%d @@%s\n' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   447
            fromline,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   448
            fromlen,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   449
            toline,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   450
            tolen,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   451
            self.proc and (b' ' + self.proc),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   452
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   453
        return fromtoline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   454
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   455
    def write(self, fp):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   456
        # updated self.added/removed, which are used by getfromtoline()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   457
        self.added, self.removed = self.countchanges()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   458
        fp.write(self.getfromtoline())
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   459
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   460
        hunklinelist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   461
        # add the following to the list: (1) all applied lines, and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   462
        # (2) all unapplied removal lines (convert these to context lines)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   463
        for changedline in self.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   464
            changedlinestr = changedline.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   465
            if changedline.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   466
                hunklinelist.append(changedlinestr)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   467
            elif changedlinestr.startswith(b"-"):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   468
                hunklinelist.append(b" " + changedlinestr[1:])
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   469
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   470
        fp.write(b''.join(self.before + hunklinelist + self.after))
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   471
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   472
    pretty = write
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   473
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   474
    def prettystr(self):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   475
        x = stringio()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   476
        self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   477
        return x.getvalue()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   478
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   479
    def reversehunk(self):
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   480
        """return a recordhunk which is the reverse of the hunk
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   481
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   482
        Assuming the displayed patch is diff(A, B) result. The returned hunk is
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   483
        intended to be applied to B, instead of A.
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   484
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   485
        For example, when A is "0\n1\n2\n6\n" and B is "0\n3\n4\n5\n6\n", and
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   486
        the user made the following selection:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   487
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   488
                 0
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   489
            [x] -1           [x]: selected
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   490
            [ ] -2           [ ]: not selected
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   491
            [x] +3
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   492
            [ ] +4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   493
            [x] +5
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   494
                 6
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   495
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   496
        This function returns a hunk like:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   497
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   498
                 0
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   499
                -3
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   500
                -4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   501
                -5
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   502
                +1
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   503
                +4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   504
                 6
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   505
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   506
        Note "4" was first deleted then added. That's because "4" exists in B
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   507
        side and "-4" must exist between "-3" and "-5" to make the patch
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   508
        applicable to B.
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   509
        """
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   510
        dels = []
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   511
        adds = []
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   512
        for line in self.changedlines:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   513
            text = line.linetext
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   514
            if line.applied:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   515
                if text.startswith(b'+'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   516
                    dels.append(text[1:])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   517
                elif text.startswith(b'-'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   518
                    adds.append(text[1:])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   519
            elif text.startswith(b'+'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   520
                dels.append(text[1:])
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   521
                adds.append(text[1:])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   522
        hunk = [b'-%s' % l for l in dels] + [b'+%s' % l for l in adds]
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   523
        h = self._hunk
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   524
        return patchmod.recordhunk(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   525
            h.header, h.toline, h.fromline, h.proc, h.before, hunk, h.after
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   526
        )
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   527
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   528
    def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   529
        return getattr(self._hunk, name)
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
   530
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   531
    def __repr__(self):
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43425
diff changeset
   532
        return '<hunk %r@%d>' % (self.filename(), self.fromline)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   533
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   534
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   535
def filterpatch(ui, chunks, chunkselector, operation=None):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   536
    """interactively filter patch chunks into applied-only chunks"""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   537
    chunks = list(chunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   538
    # convert chunks list into structure suitable for displaying/modifying
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   539
    # with curses.  create a list of headers only.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   540
    headers = [c for c in chunks if isinstance(c, patchmod.header)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   541
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   542
    # if there are no changed files
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   543
    if len(headers) == 0:
27321
dcdf0a52ad36 crecord: add dictionary to default return value of filterpatch
Laurent Charignon <lcharignon@fb.com>
parents: 27156
diff changeset
   544
        return [], {}
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   545
    uiheaders = [uiheader(h) for h in headers]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   546
    # let user choose headers/hunks/lines, and mark their applied flags
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   547
    # accordingly
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   548
    ret = chunkselector(ui, uiheaders, operation=operation)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   549
    appliedhunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   550
    for hdr in uiheaders:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   551
        if hdr.applied and (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   552
            hdr.special() or len([h for h in hdr.hunks if h.applied]) > 0
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   553
        ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   554
            appliedhunklist.append(hdr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   555
            fixoffset = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   556
            for hnk in hdr.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   557
                if hnk.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   558
                    appliedhunklist.append(hnk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   559
                    # adjust the 'to'-line offset of the hunk to be correct
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   560
                    # after de-activating some of the other hunks for this file
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   561
                    if fixoffset:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   562
                        # hnk = copy.copy(hnk) # necessary??
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   563
                        hnk.toline += fixoffset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   564
                else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   565
                    fixoffset += hnk.removed - hnk.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   566
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   567
    return (appliedhunklist, ret)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   568
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   569
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   570
def chunkselector(ui, headerlist, operation=None):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   571
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   572
    curses interface to get selection of chunks, and mark the applied flags
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   573
    of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   574
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   575
    ui.write(_(b'starting interactive selection\n'))
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   576
    chunkselector = curseschunkselector(headerlist, ui, operation)
42650
701341f57ceb curses: do not setlocale() at import time (issue5261)
Yuya Nishihara <yuya@tcha.org>
parents: 42573
diff changeset
   577
    # This is required for ncurses to display non-ASCII characters in
701341f57ceb curses: do not setlocale() at import time (issue5261)
Yuya Nishihara <yuya@tcha.org>
parents: 42573
diff changeset
   578
    # default user locale encoding correctly.  --immerrr
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43425
diff changeset
   579
    locale.setlocale(locale.LC_ALL, '')
31933
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   580
    origsigtstp = sentinel = object()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   581
    if util.safehasattr(signal, b'SIGTSTP'):
31933
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   582
        origsigtstp = signal.getsignal(signal.SIGTSTP)
31932
20a68f714f9b crecord: ensure we reinstall the SIGTSTP handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31931
diff changeset
   583
    try:
20a68f714f9b crecord: ensure we reinstall the SIGTSTP handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31931
diff changeset
   584
        curses.wrapper(chunkselector.main)
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
   585
        if chunkselector.initexc is not None:
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
   586
            raise chunkselector.initexc
31932
20a68f714f9b crecord: ensure we reinstall the SIGTSTP handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31931
diff changeset
   587
        # ncurses does not restore signal handler for SIGTSTP
20a68f714f9b crecord: ensure we reinstall the SIGTSTP handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31931
diff changeset
   588
    finally:
31933
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   589
        if origsigtstp is not sentinel:
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   590
            signal.signal(signal.SIGTSTP, origsigtstp)
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   591
    return chunkselector.opts
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   592
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   593
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   594
def testdecorator(testfn, f):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   595
    def u(*args, **kwargs):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   596
        return f(testfn, *args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   597
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   598
    return u
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   599
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   600
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   601
def testchunkselector(testfn, ui, headerlist, operation=None):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   602
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   603
    test interface to get selection of chunks, and mark the applied flags
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   604
    of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   605
    """
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   606
    chunkselector = curseschunkselector(headerlist, ui, operation)
41991
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   607
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   608
    class dummystdscr(object):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   609
        def clear(self):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   610
            pass
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   611
41991
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   612
        def refresh(self):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   613
            pass
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   614
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   615
    chunkselector.stdscr = dummystdscr()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   616
    if testfn and os.path.exists(testfn):
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
   617
        testf = open(testfn, 'r')
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
   618
        testcommands = [x.rstrip('\n') for x in testf.readlines()]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   619
        testf.close()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   620
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   621
            if chunkselector.handlekeypressed(testcommands.pop(0), test=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   622
                break
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   623
    return chunkselector.opts
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   624
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   625
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   626
_headermessages = {  # {operation: text}
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   627
    b'apply': _(b'Select hunks to apply'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   628
    b'discard': _(b'Select hunks to discard'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   629
    b'keep': _(b'Select hunks to keep'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   630
    None: _(b'Select hunks to record'),
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   631
}
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   632
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   633
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   634
class curseschunkselector(object):
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   635
    def __init__(self, headerlist, ui, operation=None):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   636
        # put the headers into a patch object
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   637
        self.headerlist = patch(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   638
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   639
        self.ui = ui
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   640
        self.opts = {}
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   641
25556
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
   642
        self.errorstr = None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   643
        # list of all chunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   644
        self.chunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   645
        for h in headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   646
            self.chunklist.append(h)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   647
            self.chunklist.extend(h.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   648
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   649
        # dictionary mapping (fgcolor, bgcolor) pairs to the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   650
        # corresponding curses color-pair value.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   651
        self.colorpairs = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   652
        # maps custom nicknames of color-pairs to curses color-pair values
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   653
        self.colorpairnames = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   654
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   655
        # Honor color setting of ui section. Keep colored setup as
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   656
        # long as not explicitly set to a falsy value - especially,
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   657
        # when not set at all. This is to stay most compatible with
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   658
        # previous (color only) behaviour.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   659
        uicolor = stringutil.parsebool(self.ui.config(b'ui', b'color'))
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   660
        self.usecolor = uicolor is not False
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   661
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   662
        # the currently selected header, hunk, or hunk-line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   663
        self.currentselecteditem = self.headerlist[0]
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
   664
        self.lastapplieditem = None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   665
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   666
        # updated when printing out patch-display -- the 'lines' here are the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   667
        # line positions *in the pad*, not on the screen.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   668
        self.selecteditemstartline = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   669
        self.selecteditemendline = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   670
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   671
        # define indentation levels
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   672
        self.headerindentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   673
        self.hunkindentnumchars = 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   674
        self.hunklineindentnumchars = 6
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   675
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   676
        # the first line of the pad to print to the screen
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   677
        self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   678
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   679
        # keeps track of the number of lines in the pad
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   680
        self.numpadlines = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   681
30554
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
   682
        self.numstatuslines = 1
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   683
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   684
        # keep a running count of the number of lines printed to the pad
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   685
        # (used for determining when the selected item begins/ends)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   686
        self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   687
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   688
        # stores optional text for a commit comment provided by the user
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   689
        self.commenttext = b""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   690
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   691
        # if the last 'toggle all' command caused all changes to be applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   692
        self.waslasttoggleallapplied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   693
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   694
        # affects some ui text
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   695
        if operation not in _headermessages:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   696
            raise error.ProgrammingError(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   697
                b'unexpected operation: %s' % operation
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   698
            )
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   699
        self.operation = operation
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   700
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   701
    def uparrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   702
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   703
        try to select the previous item to the current item that has the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   704
        most-indented level.  for example, if a hunk is selected, try to select
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   705
        the last hunkline of the hunk prior to the selected hunk.  or, if
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   706
        the first hunkline of a hunk is currently selected, then select the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   707
        hunk itself.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   708
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   709
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   710
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   711
        nextitem = currentitem.previtem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   712
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   713
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   714
            # if no parent item (i.e. currentitem is the first header), then
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   715
            # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   716
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   717
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   718
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   719
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   720
    def uparrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   721
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   722
        select (if possible) the previous item on the same level as the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   723
        currently selected item.  otherwise, select (if possible) the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   724
        parent-item of the currently selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   725
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   726
        currentitem = self.currentselecteditem
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   727
        nextitem = currentitem.prevsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   728
        # if there's no previous sibling, try choosing the parent
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   729
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   730
            nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   731
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   732
            # if no parent item (i.e. currentitem is the first header), then
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   733
            # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   734
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   735
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   736
        self.currentselecteditem = nextitem
38419
96871ca32270 crecord: re-center display in interactive curses commit on pageup/down
Matti Hamalainen <ccr@tnsp.org>
parents: 38115
diff changeset
   737
        self.recenterdisplayedarea()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   738
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   739
    def downarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   740
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   741
        try to select the next item to the current item that has the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   742
        most-indented level.  for example, if a hunk is selected, select
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   743
        the first hunkline of the selected hunk.  or, if the last hunkline of
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   744
        a hunk is currently selected, then select the next hunk, if one exists,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   745
        or if not, the next header if one exists.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   746
        """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   747
        # self.startprintline += 1 #debug
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   748
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   749
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   750
        nextitem = currentitem.nextitem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   751
        # if there's no next item, keep the selection as-is
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   752
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   753
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   754
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   755
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   756
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   757
    def downarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   758
        """
29081
4abca4cbe768 crecord: update downarrowshiftevent() docstring, remove todo
Anton Shestakov <av6@dwimlabs.net>
parents: 29080
diff changeset
   759
        select (if possible) the next item on the same level as the currently
4abca4cbe768 crecord: update downarrowshiftevent() docstring, remove todo
Anton Shestakov <av6@dwimlabs.net>
parents: 29080
diff changeset
   760
        selected item.  otherwise, select (if possible) the next item on the
4abca4cbe768 crecord: update downarrowshiftevent() docstring, remove todo
Anton Shestakov <av6@dwimlabs.net>
parents: 29080
diff changeset
   761
        same level as the parent item of the currently selected item.
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   762
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   763
        currentitem = self.currentselecteditem
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   764
        nextitem = currentitem.nextsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   765
        # if there's no next sibling, try choosing the parent's nextsibling
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   766
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   767
            try:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   768
                nextitem = currentitem.parentitem().nextsibling()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   769
            except AttributeError:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   770
                # parentitem returned None, so nextsibling() can't be called
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   771
                nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   772
        if nextitem is None:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   773
            # if parent has no next sibling, then no change...
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   774
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   775
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   776
        self.currentselecteditem = nextitem
38419
96871ca32270 crecord: re-center display in interactive curses commit on pageup/down
Matti Hamalainen <ccr@tnsp.org>
parents: 38115
diff changeset
   777
        self.recenterdisplayedarea()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   778
42572
cd4f1b7c3192 crecord: make KEY_ENTER usable in tests (by not updating UI)
Kyle Lippincott <spectral@google.com>
parents: 42571
diff changeset
   779
    def nextsametype(self, test=False):
40253
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   780
        currentitem = self.currentselecteditem
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   781
        sametype = lambda item: isinstance(item, type(currentitem))
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   782
        nextitem = currentitem.nextitem()
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   783
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   784
        while nextitem is not None and not sametype(nextitem):
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   785
            nextitem = nextitem.nextitem()
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   786
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   787
        if nextitem is None:
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   788
            nextitem = currentitem
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   789
        else:
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   790
            parent = nextitem.parentitem()
40413
7e4ffe2719e4 crecord: make nextsametype() check that parent item exists (issue6009)
Anton Shestakov <av6@dwimlabs.net>
parents: 40253
diff changeset
   791
            if parent is not None and parent.folded:
40253
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   792
                self.togglefolded(parent)
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   793
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   794
        self.currentselecteditem = nextitem
42572
cd4f1b7c3192 crecord: make KEY_ENTER usable in tests (by not updating UI)
Kyle Lippincott <spectral@google.com>
parents: 42571
diff changeset
   795
        if not test:
cd4f1b7c3192 crecord: make KEY_ENTER usable in tests (by not updating UI)
Kyle Lippincott <spectral@google.com>
parents: 42571
diff changeset
   796
            self.recenterdisplayedarea()
40253
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   797
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   798
    def rightarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   799
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   800
        select (if possible) the first of this item's child-items.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   801
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   802
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   803
        nextitem = currentitem.firstchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   804
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   805
        # turn off folding if we want to show a child-item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   806
        if currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   807
            self.togglefolded(currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   808
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   809
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   810
            # if no next item on parent-level, then no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   811
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   812
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   813
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   814
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   815
    def leftarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   816
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   817
        if the current item can be folded (i.e. it is an unfolded header or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   818
        hunk), then fold it.  otherwise try select (if possible) the parent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   819
        of this item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   820
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   821
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   822
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   823
        # try to fold the item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   824
        if not isinstance(currentitem, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   825
            if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   826
                self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   827
                return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   828
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   829
        # if it can't be folded, try to select the parent item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   830
        nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   831
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   832
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   833
            # if no item on parent-level, then no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   834
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   835
            if not nextitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   836
                self.togglefolded(item=nextitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   837
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   838
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   839
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   840
    def leftarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   841
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   842
        select the header of the current item (or fold current item if the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   843
        current item is already a header).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   844
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   845
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   846
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   847
        if isinstance(currentitem, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   848
            if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   849
                self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   850
                return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   851
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   852
        # select the parent item recursively until we're at a header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   853
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   854
            nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   855
            if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   856
                break
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   857
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   858
                currentitem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   859
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   860
        self.currentselecteditem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   861
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   862
    def updatescroll(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   863
        """scroll the screen to fully show the currently-selected"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   864
        selstart = self.selecteditemstartline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   865
        selend = self.selecteditemendline
29946
3664537386ab crecord: delete commented line
Nathan Goldbaum <ngoldbau@illinois.edu>
parents: 29941
diff changeset
   866
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   867
        padstart = self.firstlineofpadtoprint
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   868
        padend = padstart + self.yscreensize - self.numstatuslines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   869
        # 'buffered' pad start/end values which scroll with a certain
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   870
        # top/bottom context margin
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   871
        padstartbuffered = padstart + 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   872
        padendbuffered = padend - 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   873
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   874
        if selend > padendbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   875
            self.scrolllines(selend - padendbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   876
        elif selstart < padstartbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   877
            # negative values scroll in pgup direction
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   878
            self.scrolllines(selstart - padstartbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   879
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   880
    def scrolllines(self, numlines):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   881
        """scroll the screen up (down) by numlines when numlines >0 (<0)."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   882
        self.firstlineofpadtoprint += numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   883
        if self.firstlineofpadtoprint < 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   884
            self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   885
        if self.firstlineofpadtoprint > self.numpadlines - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   886
            self.firstlineofpadtoprint = self.numpadlines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   887
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   888
    def toggleapply(self, item=None):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   889
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   890
        toggle the applied flag of the specified item.  if no item is specified,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   891
        toggle the flag of the currently selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   892
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   893
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   894
            item = self.currentselecteditem
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
   895
            # Only set this when NOT using 'toggleall'
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
   896
            self.lastapplieditem = item
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   897
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   898
        item.applied = not item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   899
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   900
        if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   901
            item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   902
            if item.applied:
24492
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   903
                # apply all its hunks
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   904
                for hnk in item.hunks:
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   905
                    hnk.applied = True
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   906
                    # apply all their hunklines
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   907
                    for hunkline in hnk.changedlines:
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   908
                        hunkline.applied = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   909
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   910
                # un-apply all its hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   911
                for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   912
                    hnk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   913
                    hnk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   914
                    # un-apply all their hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   915
                    for hunkline in hnk.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   916
                        hunkline.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   917
        elif isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   918
            item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   919
            # apply all it's hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   920
            for hunkline in item.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   921
                hunkline.applied = item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   922
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   923
            siblingappliedstatus = [hnk.applied for hnk in item.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   924
            allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   925
            nosiblingsapplied = not (True in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   926
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   927
            siblingspartialstatus = [hnk.partial for hnk in item.header.hunks]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   928
            somesiblingspartial = True in siblingspartialstatus
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   929
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   930
            # cases where applied or partial should be removed from header
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   931
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   932
            # if no 'sibling' hunks are applied (including this hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   933
            if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   934
                if not item.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   935
                    item.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   936
                    item.header.partial = False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   937
            else:  # some/all parent siblings are applied
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   938
                item.header.applied = True
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   939
                item.header.partial = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   940
                    somesiblingspartial or not allsiblingsapplied
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   941
                )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   942
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   943
        elif isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   944
            siblingappliedstatus = [ln.applied for ln in item.hunk.changedlines]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   945
            allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   946
            nosiblingsapplied = not (True in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   947
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   948
            # if no 'sibling' lines are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   949
            if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   950
                item.hunk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   951
                item.hunk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   952
            elif allsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   953
                item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   954
                item.hunk.partial = False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   955
            else:  # some siblings applied
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   956
                item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   957
                item.hunk.partial = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   958
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   959
            parentsiblingsapplied = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   960
                hnk.applied for hnk in item.hunk.header.hunks
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   961
            ]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   962
            noparentsiblingsapplied = not (True in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   963
            allparentsiblingsapplied = not (False in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   964
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   965
            parentsiblingspartial = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   966
                hnk.partial for hnk in item.hunk.header.hunks
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   967
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   968
            someparentsiblingspartial = True in parentsiblingspartial
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   969
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   970
            # if all parent hunks are not applied, un-apply header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   971
            if noparentsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   972
                if not item.hunk.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   973
                    item.hunk.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   974
                    item.hunk.header.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   975
            # set the applied and partial status of the header if needed
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   976
            else:  # some/all parent siblings are applied
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   977
                item.hunk.header.applied = True
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   978
                item.hunk.header.partial = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   979
                    someparentsiblingspartial or not allparentsiblingsapplied
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   980
                )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   981
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   982
    def toggleall(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   983
        """toggle the applied flag of all items."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
   984
        if self.waslasttoggleallapplied:  # then unapply them this time
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   985
            for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   986
                if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   987
                    self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   988
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   989
            for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   990
                if not item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   991
                    self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   992
        self.waslasttoggleallapplied = not self.waslasttoggleallapplied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   993
43893
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   994
    def flipselections(self):
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   995
        """
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   996
        Flip all selections. Every selected line is unselected and vice
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   997
        versa.
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   998
        """
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
   999
        for header in self.headerlist:
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1000
            for hunk in header.allchildren():
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1001
                for line in hunk.allchildren():
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1002
                    self.toggleapply(line)
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1003
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1004
    def toggleallbetween(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1005
        """toggle applied on or off for all items in range [lastapplied,
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1006
        current]. """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1007
        if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1008
            not self.lastapplieditem
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1009
            or self.currentselecteditem == self.lastapplieditem
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1010
        ):
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1011
            # Treat this like a normal 'x'/' '
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1012
            self.toggleapply()
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1013
            return
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1014
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1015
        startitem = self.lastapplieditem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1016
        enditem = self.currentselecteditem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1017
        # Verify that enditem is "after" startitem, otherwise swap them.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1018
        for direction in [b'forward', b'reverse']:
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1019
            nextitem = startitem.nextitem()
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1020
            while nextitem and nextitem != enditem:
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1021
                nextitem = nextitem.nextitem()
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1022
            if nextitem:
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1023
                break
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1024
            # Looks like we went the wrong direction :)
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1025
            startitem, enditem = enditem, startitem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1026
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1027
        if not nextitem:
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1028
            # We didn't find a path going either forward or backward? Don't know
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1029
            # how this can happen, let's not crash though.
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1030
            return
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1031
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1032
        nextitem = startitem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1033
        # Switch all items to be the opposite state of the currently selected
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1034
        # item. Specifically:
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1035
        #  [ ] startitem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1036
        #  [x] middleitem
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1037
        #  [ ] enditem  <-- currently selected
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1038
        # This will turn all three on, since the currently selected item is off.
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1039
        # This does *not* invert each item (i.e. middleitem stays marked/on)
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1040
        desiredstate = not self.currentselecteditem.applied
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1041
        while nextitem != enditem.nextitem():
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1042
            if nextitem.applied != desiredstate:
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1043
                self.toggleapply(item=nextitem)
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1044
            nextitem = nextitem.nextitem()
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1045
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1046
    def togglefolded(self, item=None, foldparent=False):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1047
        """toggle folded flag of specified item (defaults to currently
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1048
        selected)"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1049
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1050
            item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1051
        if foldparent or (isinstance(item, uiheader) and item.neverunfolded):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1052
            if not isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1053
                # we need to select the parent item in this case
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1054
                self.currentselecteditem = item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1055
            elif item.neverunfolded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1056
                item.neverunfolded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1057
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1058
            # also fold any foldable children of the parent/current item
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1059
            if isinstance(item, uiheader):  # the original or 'new' item
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1060
                for child in item.allchildren():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1061
                    child.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1062
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1063
        if isinstance(item, (uiheader, uihunk)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1064
            item.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1065
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1066
    def alignstring(self, instr, window):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1067
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1068
        add whitespace to the end of a string in order to make it fill
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1069
        the screen in the x direction.  the current cursor position is
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1070
        taken into account when making this calculation.  the string can span
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1071
        multiple lines.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1072
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1073
        y, xstart = window.getyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1074
        width = self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1075
        # turn tabs into spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1076
        instr = instr.expandtabs(4)
24351
cdc4f3af2497 crecord: use colwidth instead of ucolwidth
Matt Mackall <mpm@selenic.com>
parents: 24342
diff changeset
  1077
        strwidth = encoding.colwidth(instr)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1078
        numspaces = width - ((strwidth + xstart) % width)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1079
        return instr + b" " * numspaces
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1080
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1081
    def printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1082
        self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1083
        window,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1084
        text,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1085
        fgcolor=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1086
        bgcolor=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1087
        pair=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1088
        pairname=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1089
        attrlist=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1090
        towin=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1091
        align=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1092
        showwhtspc=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1093
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1094
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1095
        print the string, text, with the specified colors and attributes, to
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1096
        the specified curses window object.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1097
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1098
        the foreground and background colors are of the form
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1099
        curses.color_xxxx, where xxxx is one of: [black, blue, cyan, green,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1100
        magenta, red, white, yellow].  if pairname is provided, a color
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1101
        pair will be looked up in the self.colorpairnames dictionary.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1102
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1103
        attrlist is a list containing text attributes in the form of
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1104
        curses.a_xxxx, where xxxx can be: [bold, dim, normal, standout,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1105
        underline].
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1106
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1107
        if align == True, whitespace is added to the printed string such that
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1108
        the string stretches to the right border of the window.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1109
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1110
        if showwhtspc == True, trailing whitespace of a string is highlighted.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1111
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1112
        # preprocess the text, converting tabs to spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1113
        text = text.expandtabs(4)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1114
        # strip \n, and convert control characters to ^[char] representation
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1115
        text = re.sub(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1116
            br'[\x00-\x08\x0a-\x1f]',
44135
ae596fac8ba0 crecord: fix a concatenation of bytes and str on py3
Kyle Lippincott <spectral@google.com>
parents: 43894
diff changeset
  1117
            lambda m: b'^' + pycompat.sysbytes(chr(ord(m.group()) + 64)),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1118
            text.strip(b'\n'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1119
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1120
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1121
        if pair is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1122
            colorpair = pair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1123
        elif pairname is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1124
            colorpair = self.colorpairnames[pairname]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1125
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1126
            if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1127
                fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1128
            if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1129
                bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1130
            if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1131
                colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1132
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1133
                colorpair = self.getcolorpair(fgcolor, bgcolor)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1134
        # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1135
        if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1136
            attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1137
        if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1138
            # then it is safe to apply all attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1139
            for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1140
                colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1141
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1142
            # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1143
            for textattr in (curses.A_UNDERLINE, curses.A_BOLD):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1144
                if textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1145
                    colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1146
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1147
        y, xstart = self.chunkpad.getyx()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1148
        t = b""  # variable for counting lines printed
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1149
        # if requested, show trailing whitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1150
        if showwhtspc:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1151
            origlen = len(text)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1152
            text = text.rstrip(b' \n')  # tabs have already been expanded
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1153
            strippedlen = len(text)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1154
            numtrailingspaces = origlen - strippedlen
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1155
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1156
        if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1157
            window.addstr(text, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1158
        t += text
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1159
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1160
        if showwhtspc:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1161
            wscolorpair = colorpair | curses.A_REVERSE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1162
            if towin:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1163
                for i in range(numtrailingspaces):
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1164
                    window.addch(curses.ACS_CKBOARD, wscolorpair)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1165
            t += b" " * numtrailingspaces
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1166
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1167
        if align:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1168
            if towin:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1169
                extrawhitespace = self.alignstring(b"", window)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1170
                window.addstr(extrawhitespace, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1171
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1172
                # need to use t, since the x position hasn't incremented
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1173
                extrawhitespace = self.alignstring(t, window)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1174
            t += extrawhitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1175
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1176
        # is reset to 0 at the beginning of printitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1177
43159
b02387005515 py3: use integer division in curseschunkselector.printstring()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43158
diff changeset
  1178
        linesprinted = (xstart + len(t)) // self.xscreensize
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1179
        self.linesprintedtopadsofar += linesprinted
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1180
        return t
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1181
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1182
    def _getstatuslinesegments(self):
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1183
        """-> [str]. return segments"""
30556
5129ed3c2548 crecord: change help text for the space key dynamically
Jun Wu <quark@fb.com>
parents: 30555
diff changeset
  1184
        selected = self.currentselecteditem.applied
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1185
        spaceselect = _(b'space/enter: select')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1186
        spacedeselect = _(b'space/enter: deselect')
33834
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1187
        # Format the selected label into a place as long as the longer of the
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1188
        # two possible labels.  This may vary by language.
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1189
        spacelen = max(len(spaceselect), len(spacedeselect))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1190
        selectedlabel = b'%-*s' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1191
            spacelen,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1192
            spacedeselect if selected else spaceselect,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1193
        )
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1194
        segments = [
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
  1195
            _headermessages[self.operation],
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1196
            b'-',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1197
            _(b'[x]=selected **=collapsed'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1198
            _(b'c: confirm'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1199
            _(b'q: abort'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1200
            _(b'arrow keys: move/expand/collapse'),
33834
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1201
            selectedlabel,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1202
            _(b'?: help'),
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1203
        ]
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1204
        return segments
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1205
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1206
    def _getstatuslines(self):
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1207
        """() -> [str]. return short help used in the top status window"""
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1208
        if self.errorstr is not None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1209
            lines = [self.errorstr, _(b'Press any key to continue')]
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1210
        else:
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1211
            # wrap segments to lines
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1212
            segments = self._getstatuslinesegments()
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1213
            width = self.xscreensize
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1214
            lines = []
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1215
            lastwidth = width
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1216
            for s in segments:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1217
                w = encoding.colwidth(s)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1218
                sep = b' ' * (1 + (s and s[0] not in b'-['))
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1219
                if lastwidth + w + len(sep) >= width:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1220
                    lines.append(s)
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1221
                    lastwidth = w
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1222
                else:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1223
                    lines[-1] += sep + s
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1224
                    lastwidth += w + len(sep)
30554
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1225
        if len(lines) != self.numstatuslines:
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1226
            self.numstatuslines = len(lines)
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1227
            self.statuswin.resize(self.numstatuslines, self.xscreensize)
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36466
diff changeset
  1228
        return [stringutil.ellipsis(l, self.xscreensize - 1) for l in lines]
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1229
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1230
    def updatescreen(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1231
        self.statuswin.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1232
        self.chunkpad.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1233
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1234
        printstring = self.printstring
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1235
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1236
        # print out the status lines at the top
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1237
        try:
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1238
            for line in self._getstatuslines():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1239
                printstring(self.statuswin, line, pairname=b"legend")
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1240
            self.statuswin.refresh()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1241
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1242
            pass
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1243
        if self.errorstr is not None:
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1244
            return
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1245
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1246
        # print out the patch in the remaining part of the window
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1247
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1248
            self.printitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1249
            self.updatescroll()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1250
            self.chunkpad.refresh(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1251
                self.firstlineofpadtoprint,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1252
                0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1253
                self.numstatuslines,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1254
                0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1255
                self.yscreensize - self.numstatuslines,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1256
                self.xscreensize,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1257
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1258
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1259
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1260
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1261
    def getstatusprefixstring(self, item):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1262
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1263
        create a string to prefix a line with which indicates whether 'item'
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1264
        is applied and/or folded.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1265
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1266
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1267
        # create checkbox string
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1268
        if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1269
            if not isinstance(item, uihunkline) and item.partial:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1270
                checkbox = b"[~]"
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1271
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1272
                checkbox = b"[x]"
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1273
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1274
            checkbox = b"[ ]"
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1275
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1276
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1277
            if item.folded:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1278
                checkbox += b"**"
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1279
                if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1280
                    # one of "m", "a", or "d" (modified, added, deleted)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1281
                    filestatus = item.changetype
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1282
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1283
                    checkbox += filestatus + b" "
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1284
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1285
                checkbox += b"  "
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1286
                if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1287
                    # add two more spaces for headers
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1288
                    checkbox += b"  "
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1289
        except AttributeError:  # not foldable
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1290
            checkbox += b"  "
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1291
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1292
        return checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1293
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1294
    def printheader(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1295
        self, header, selected=False, towin=True, ignorefolding=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1296
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1297
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1298
        print the header to the pad.  if countlines is True, don't print
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1299
        anything, but just count the number of lines which would be printed.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1300
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1301
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1302
        outstr = b""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1303
        text = header.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1304
        chunkindex = self.chunklist.index(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1305
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1306
        if chunkindex != 0 and not header.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1307
            # add separating line before headers
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1308
            outstr += self.printstring(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1309
                self.chunkpad, b'_' * self.xscreensize, towin=towin, align=False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1310
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1311
        # select color-pair based on if the header is selected
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1312
        colorpair = self.getcolorpair(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1313
            name=selected and b"selected" or b"normal", attrlist=[curses.A_BOLD]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1314
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1315
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1316
        # print out each line of the chunk, expanding it to screen width
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1317
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1318
        # number of characters to indent lines on this level by
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1319
        indentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1320
        checkbox = self.getstatusprefixstring(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1321
        if not header.folded or ignorefolding:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1322
            textlist = text.split(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1323
            linestr = checkbox + textlist[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1324
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1325
            linestr = checkbox + header.filename()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1326
        outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1327
            self.chunkpad, linestr, pair=colorpair, towin=towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1328
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1329
        if not header.folded or ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1330
            if len(textlist) > 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1331
                for line in textlist[1:]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1332
                    linestr = b" " * (indentnumchars + len(checkbox)) + line
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1333
                    outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1334
                        self.chunkpad, linestr, pair=colorpair, towin=towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1335
                    )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1336
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1337
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1338
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1339
    def printhunklinesbefore(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1340
        self, hunk, selected=False, towin=True, ignorefolding=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1341
    ):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1342
        """includes start/end line indicator"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1343
        outstr = b""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1344
        # where hunk is in list of siblings
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1345
        hunkindex = hunk.header.hunks.index(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1346
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1347
        if hunkindex != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1348
            # add separating line before headers
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1349
            outstr += self.printstring(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1350
                self.chunkpad, b' ' * self.xscreensize, towin=towin, align=False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1351
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1352
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1353
        colorpair = self.getcolorpair(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1354
            name=selected and b"selected" or b"normal", attrlist=[curses.A_BOLD]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1355
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1356
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1357
        # print out from-to line with checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1358
        checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1359
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1360
        lineprefix = b" " * self.hunkindentnumchars + checkbox
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1361
        frtoline = b"   " + hunk.getfromtoline().strip(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1362
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1363
        outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1364
            self.chunkpad, lineprefix, towin=towin, align=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1365
        )  # add uncolored checkbox/indent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1366
        outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1367
            self.chunkpad, frtoline, pair=colorpair, towin=towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1368
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1369
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1370
        if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1371
            # skip remainder of output
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1372
            return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1373
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1374
        # print out lines of the chunk preceeding changed-lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1375
        for line in hunk.before:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1376
            linestr = (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1377
                b" " * (self.hunklineindentnumchars + len(checkbox)) + line
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1378
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1379
            outstr += self.printstring(self.chunkpad, linestr, towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1380
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1381
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1382
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1383
    def printhunklinesafter(self, hunk, towin=True, ignorefolding=False):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1384
        outstr = b""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1385
        if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1386
            return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1387
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1388
        # a bit superfluous, but to avoid hard-coding indent amount
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1389
        checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1390
        for line in hunk.after:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1391
            linestr = (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1392
                b" " * (self.hunklineindentnumchars + len(checkbox)) + line
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1393
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1394
            outstr += self.printstring(self.chunkpad, linestr, towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1395
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1396
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1397
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1398
    def printhunkchangedline(self, hunkline, selected=False, towin=True):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1399
        outstr = b""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1400
        checkbox = self.getstatusprefixstring(hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1401
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1402
        linestr = hunkline.prettystr().strip(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1403
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1404
        # select color-pair based on whether line is an addition/removal
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1405
        if selected:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1406
            colorpair = self.getcolorpair(name=b"selected")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1407
        elif linestr.startswith(b"+"):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1408
            colorpair = self.getcolorpair(name=b"addition")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1409
        elif linestr.startswith(b"-"):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1410
            colorpair = self.getcolorpair(name=b"deletion")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1411
        elif linestr.startswith(b"\\"):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1412
            colorpair = self.getcolorpair(name=b"normal")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1413
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1414
        lineprefix = b" " * self.hunklineindentnumchars + checkbox
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1415
        outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1416
            self.chunkpad, lineprefix, towin=towin, align=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1417
        )  # add uncolored checkbox/indent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1418
        outstr += self.printstring(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1419
            self.chunkpad, linestr, pair=colorpair, towin=towin, showwhtspc=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1420
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1421
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1422
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1423
    def printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1424
        self, item=None, ignorefolding=False, recursechildren=True, towin=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1425
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1426
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1427
        use __printitem() to print the the specified item.applied.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1428
        if item is not specified, then print the entire patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1429
        (hiding folded elements, etc. -- see __printitem() docstring)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1430
        """
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1431
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1432
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1433
            item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1434
        if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1435
            self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1436
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1437
        outstr = []
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1438
        self.__printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1439
            item, ignorefolding, recursechildren, outstr, towin=towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1440
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1441
        return b''.join(outstr)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1442
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1443
    def outofdisplayedarea(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1444
        y, _ = self.chunkpad.getyx()  # cursor location
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1445
        # * 2 here works but an optimization would be the max number of
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1446
        # consecutive non selectable lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1447
        # i.e the max number of context line for any hunk in the patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1448
        miny = min(0, self.firstlineofpadtoprint - self.yscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1449
        maxy = self.firstlineofpadtoprint + self.yscreensize * 2
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1450
        return y < miny or y > maxy
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1451
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1452
    def handleselection(self, item, recursechildren):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1453
        selected = item is self.currentselecteditem
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1454
        if selected and recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1455
            # assumes line numbering starting from line 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1456
            self.selecteditemstartline = self.linesprintedtopadsofar
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1457
            selecteditemlines = self.getnumlinesdisplayed(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1458
                item, recursechildren=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1459
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1460
            self.selecteditemendline = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1461
                self.selecteditemstartline + selecteditemlines - 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1462
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1463
        return selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1464
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1465
    def __printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1466
        self, item, ignorefolding, recursechildren, outstr, towin=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1467
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1468
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1469
        recursive method for printing out patch/header/hunk/hunk-line data to
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1470
        screen.  also returns a string with all of the content of the displayed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1471
        patch (not including coloring, etc.).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1472
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1473
        if ignorefolding is True, then folded items are printed out.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1474
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1475
        if recursechildren is False, then only print the item without its
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1476
        child items.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1477
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1478
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1479
        if towin and self.outofdisplayedarea():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1480
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1481
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1482
        selected = self.handleselection(item, recursechildren)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1483
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1484
        # patch object is a list of headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1485
        if isinstance(item, patch):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1486
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1487
                for hdr in item:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1488
                    self.__printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1489
                        hdr, ignorefolding, recursechildren, outstr, towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1490
                    )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1491
        # todo: eliminate all isinstance() calls
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1492
        if isinstance(item, uiheader):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1493
            outstr.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1494
                self.printheader(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1495
                    item, selected, towin=towin, ignorefolding=ignorefolding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1496
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1497
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1498
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1499
                for hnk in item.hunks:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1500
                    self.__printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1501
                        hnk, ignorefolding, recursechildren, outstr, towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1502
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1503
        elif isinstance(item, uihunk) and (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1504
            (not item.header.folded) or ignorefolding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1505
        ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1506
            # print the hunk data which comes before the changed-lines
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1507
            outstr.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1508
                self.printhunklinesbefore(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1509
                    item, selected, towin=towin, ignorefolding=ignorefolding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1510
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1511
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1512
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1513
                for l in item.changedlines:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1514
                    self.__printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1515
                        l, ignorefolding, recursechildren, outstr, towin
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1516
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1517
                outstr.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1518
                    self.printhunklinesafter(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1519
                        item, towin=towin, ignorefolding=ignorefolding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1520
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1521
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1522
        elif isinstance(item, uihunkline) and (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1523
            (not item.hunk.folded) or ignorefolding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1524
        ):
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1525
            outstr.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1526
                self.printhunkchangedline(item, selected, towin=towin)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1527
            )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1528
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1529
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1530
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1531
    def getnumlinesdisplayed(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1532
        self, item=None, ignorefolding=False, recursechildren=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1533
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1534
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1535
        return the number of lines which would be displayed if the item were
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1536
        to be printed to the display.  the item will not be printed to the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1537
        display (pad).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1538
        if no item is given, assume the entire patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1539
        if ignorefolding is True, folded items will be unfolded when counting
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1540
        the number of lines.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1541
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1542
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1543
        # temporarily disable printing to windows by printstring
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1544
        patchdisplaystring = self.printitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1545
            item, ignorefolding, recursechildren, towin=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1546
        )
36339
3496bffe266d py3: make sure we are doing integer division by using '//'
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36338
diff changeset
  1547
        numlines = len(patchdisplaystring) // self.xscreensize
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1548
        return numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1549
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1550
    def sigwinchhandler(self, n, frame):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1551
        """handle window resizing"""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1552
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1553
            curses.endwin()
30328
0911191dc4c9 crecord: use scmutil.termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 29961
diff changeset
  1554
            self.xscreensize, self.yscreensize = scmutil.termsize(self.ui)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1555
            self.statuswin.resize(self.numstatuslines, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1556
            self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1557
            self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1558
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1559
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1560
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1561
    def getcolorpair(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1562
        self, fgcolor=None, bgcolor=None, name=None, attrlist=None
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1563
    ):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1564
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1565
        get a curses color pair, adding it to self.colorpairs if it is not
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1566
        already defined.  an optional string, name, can be passed as a shortcut
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1567
        for referring to the color-pair.  by default, if no arguments are
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1568
        specified, the white foreground / black background color-pair is
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1569
        returned.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1570
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1571
        it is expected that this function will be used exclusively for
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1572
        initializing color pairs, and not curses.init_pair().
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1573
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1574
        attrlist is used to 'flavor' the returned color-pair.  this information
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1575
        is not stored in self.colorpairs.  it contains attribute values like
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1576
        curses.A_BOLD.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1577
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1578
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1579
        if (name is not None) and name in self.colorpairnames:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1580
            # then get the associated color pair and return it
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1581
            colorpair = self.colorpairnames[name]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1582
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1583
            if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1584
                fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1585
            if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1586
                bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1587
            if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1588
                colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1589
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1590
                pairindex = len(self.colorpairs) + 1
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1591
                if self.usecolor:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1592
                    curses.init_pair(pairindex, fgcolor, bgcolor)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1593
                    colorpair = self.colorpairs[
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1594
                        (fgcolor, bgcolor)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1595
                    ] = curses.color_pair(pairindex)
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1596
                    if name is not None:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1597
                        self.colorpairnames[name] = curses.color_pair(pairindex)
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1598
                else:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1599
                    cval = 0
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1600
                    if name is not None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1601
                        if name == b'selected':
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1602
                            cval = curses.A_REVERSE
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1603
                        self.colorpairnames[name] = cval
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1604
                    colorpair = self.colorpairs[(fgcolor, bgcolor)] = cval
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1605
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1606
        # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1607
        if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1608
            attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1609
        if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1610
            # then it is safe to apply all attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1611
            for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1612
                colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1613
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1614
            # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1615
            for textattrib in (curses.A_UNDERLINE, curses.A_BOLD):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1616
                if textattrib in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1617
                    colorpair |= textattrib
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1618
        return colorpair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1619
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1620
    def initcolorpair(self, *args, **kwargs):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1621
        """same as getcolorpair."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1622
        self.getcolorpair(*args, **kwargs)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1623
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1624
    def helpwindow(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1625
        """print a help window to the screen.  exit after any keypress."""
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1626
        helptext = _(
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1627
            """            [press any key to return to the patch-display]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1628
43891
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1629
The curses hunk selector allows you to interactively choose among the
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1630
changes you have made, and confirm only those changes you select for
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1631
further processing by the command you are running (such as commit,
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1632
shelve, or revert). After confirming the selected changes, the
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1633
unselected changes are still present in your working copy, so you can
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1634
use the hunk selector multiple times to split large changes into
ac54b8a2ebea crecord: rewrite help string to avoid mentioning "crecord"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43807
diff changeset
  1635
smaller changesets. the following are valid keystrokes:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1636
42570
75fd9421440b crecord: add "x" alias for space, remove test-only "TOGGLE" alias
Kyle Lippincott <spectral@google.com>
parents: 42569
diff changeset
  1637
              x [space] : (un-)select item ([~]/[x] = partly/fully applied)
40253
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
  1638
                [enter] : (un-)select item and go to next item of same type
27936
fedd81659643 crecord: fix typo in the help text
Laurent Charignon <lcharignon@fb.com>
parents: 27914
diff changeset
  1639
                      A : (un-)select all items
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1640
                      X : (un-)select all items between current and most-recent
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1641
    up/down-arrow [k/j] : go to previous/next unfolded item
25460
bd4bcfa48c9e crecord: fix three typos introduced while moving crecord into core
Laurent Charignon <lcharignon@fb.com>
parents: 25447
diff changeset
  1642
        pgup/pgdn [K/J] : go to previous/next item of same type
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1643
 right/left-arrow [l/h] : go to child item / parent item
25460
bd4bcfa48c9e crecord: fix three typos introduced while moving crecord into core
Laurent Charignon <lcharignon@fb.com>
parents: 25447
diff changeset
  1644
 shift-left-arrow   [H] : go to parent header / fold selected header
42073
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1645
                      g : go to the top
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1646
                      G : go to the bottom
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1647
                      f : fold / unfold item, hiding/revealing its children
25447
093d38165e5a crecord: fix a typo introduced when moving crecord to core
Laurent Charignon <lcharignon@fb.com>
parents: 24840
diff changeset
  1648
                      F : fold / unfold parent item and all of its ancestors
29961
7d053ba73178 crecord: add an event that scrolls the selected line to the top of the screen
Nathan Goldbaum <ngoldbau@illinois.edu>
parents: 29946
diff changeset
  1649
                 ctrl-l : scroll the selected line to the top of the screen
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1650
                      m : edit / resume editing the commit message
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1651
                      e : edit the currently selected hunk
43893
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1652
                      a : toggle all selections
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1653
                      c : confirm selected changes
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1654
                      r : review/edit and confirm selected changes
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1655
                      q : quit without confirming (no changes will be made)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1656
                      ? : help (what you're currently reading)"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1657
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1658
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1659
        helpwin = curses.newwin(self.yscreensize, 0, 0, 0)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1660
        helplines = helptext.split(b"\n")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1661
        helplines = helplines + [b" "] * (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1662
            self.yscreensize - self.numstatuslines - len(helplines) - 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1663
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1664
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1665
            for line in helplines:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1666
                self.printstring(helpwin, line, pairname=b"legend")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1667
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1668
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1669
        helpwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1670
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1671
            with self.ui.timeblockedsection(b'crecord'):
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1672
                helpwin.getkey()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1673
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1674
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1675
33991
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1676
    def commitMessageWindow(self):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1677
        """Create a temporary commit message editing window on the screen."""
33991
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1678
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1679
        curses.raw()
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1680
        curses.def_prog_mode()
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1681
        curses.endwin()
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1682
        self.commenttext = self.ui.edit(self.commenttext, self.ui.username())
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1683
        curses.cbreak()
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1684
        self.stdscr.refresh()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1685
        self.stdscr.keypad(1)  # allow arrow-keys to continue to function
33991
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1686
42073
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1687
    def handlefirstlineevent(self):
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1688
        """
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1689
        Handle 'g' to navigate to the top most file in the ncurses window.
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1690
        """
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1691
        self.currentselecteditem = self.headerlist[0]
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1692
        currentitem = self.currentselecteditem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1693
        # select the parent item recursively until we're at a header
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1694
        while True:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1695
            nextitem = currentitem.parentitem()
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1696
            if nextitem is None:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1697
                break
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1698
            else:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1699
                currentitem = nextitem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1700
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1701
        self.currentselecteditem = currentitem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1702
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1703
    def handlelastlineevent(self):
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1704
        """
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1705
        Handle 'G' to navigate to the bottom most file/hunk/line depending
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1706
        on the whether the fold is active or not.
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1707
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1708
        If the bottom most file is folded, it navigates to that file and
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1709
        stops there. If the bottom most file is unfolded, it navigates to
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1710
        the bottom most hunk in that file and stops there. If the bottom most
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1711
        hunk is unfolded, it navigates to the bottom most line in that hunk.
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1712
        """
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1713
        currentitem = self.currentselecteditem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1714
        nextitem = currentitem.nextitem()
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1715
        # select the child item recursively until we're at a footer
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1716
        while nextitem is not None:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1717
            nextitem = currentitem.nextitem()
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1718
            if nextitem is None:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1719
                break
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1720
            else:
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1721
                currentitem = nextitem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1722
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1723
        self.currentselecteditem = currentitem
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1724
        self.recenterdisplayedarea()
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1725
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1726
    def confirmationwindow(self, windowtext):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1727
        """display an informational window, then wait for and return a
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
  1728
        keypress."""
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1729
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1730
        confirmwin = curses.newwin(self.yscreensize, 0, 0, 0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1731
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1732
            lines = windowtext.split(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1733
            for line in lines:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1734
                self.printstring(confirmwin, line, pairname=b"selected")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1735
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1736
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1737
        self.stdscr.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1738
        confirmwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1739
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1740
            with self.ui.timeblockedsection(b'crecord'):
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1741
                response = chr(self.stdscr.getch())
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1742
        except ValueError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1743
            response = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1744
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1745
        return response
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1746
28926
0411b7998d9b crecord: cleanup the remains of commit confirmation
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28925
diff changeset
  1747
    def reviewcommit(self):
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1748
        """ask for 'y' to be pressed to confirm selected. return True if
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1749
        confirmed."""
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1750
        confirmtext = _(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1751
            """If you answer yes to the following, your currently chosen patch chunks
41944
7e95ade0f369 chunkselector: fix typos in instructions when user reviews patch
Kyle Lippincott <spectral@google.com>
parents: 41848
diff changeset
  1752
will be loaded into an editor. To modify the patch, make the changes in your
7e95ade0f369 chunkselector: fix typos in instructions when user reviews patch
Kyle Lippincott <spectral@google.com>
parents: 41848
diff changeset
  1753
editor and save. To accept the current patch as-is, close the editor without
7e95ade0f369 chunkselector: fix typos in instructions when user reviews patch
Kyle Lippincott <spectral@google.com>
parents: 41848
diff changeset
  1754
saving.
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1755
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1756
note: don't add/remove lines unless you also modify the range information.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1757
      failing to follow this rule will result in the commit aborting.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1758
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1759
are you sure you want to review/edit and confirm the selected changes [yn]?
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1760
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1761
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1762
        with self.ui.timeblockedsection(b'crecord'):
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1763
            response = self.confirmationwindow(confirmtext)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1764
        if response is None:
43423
117b41b1859d py3: compare response of crecord's confirmationwindow with str
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43364
diff changeset
  1765
            response = "n"
117b41b1859d py3: compare response of crecord's confirmationwindow with str
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43364
diff changeset
  1766
        if response.lower().startswith("y"):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1767
            return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1768
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1769
            return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1770
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1771
    def recenterdisplayedarea(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1772
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1773
        once we scrolled with pg up pg down we can be pointing outside of the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1774
        display zone. we print the patch with towin=False to compute the
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
  1775
        location of the selected item even though it is outside of the displayed
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1776
        zone and then update the scroll.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1777
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1778
        self.printitem(towin=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1779
        self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1780
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1781
    def toggleedit(self, item=None, test=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1782
        """
28580
8b41ad798fb7 crecord: fix docblock indentation
Ryan McElroy <rmcelroy@fb.com>
parents: 28579
diff changeset
  1783
        edit the currently selected chunk
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1784
        """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1785
25555
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1786
        def updateui(self):
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1787
            self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1788
            self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1789
            self.updatescroll()
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1790
            self.stdscr.refresh()
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1791
            self.statuswin.refresh()
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1792
            self.stdscr.keypad(1)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1793
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1794
        def editpatchwitheditor(self, chunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1795
            if chunk is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1796
                self.ui.write(_(b'cannot edit patch for whole file'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1797
                self.ui.write(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1798
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1799
            if chunk.header.binary():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1800
                self.ui.write(_(b'cannot edit patch for binary file'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1801
                self.ui.write(b"\n")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1802
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1803
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1804
            # write the initial patch
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
  1805
            patch = stringio()
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
  1806
            patch.write(diffhelptext + hunkhelptext)
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1807
            chunk.header.write(patch)
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1808
            chunk.write(patch)
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1809
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1810
            # start the editor and wait for it to complete
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1811
            try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1812
                patch = self.ui.edit(patch.getvalue(), b"", action=b"diff")
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1813
            except error.Abort as exc:
43364
dd64e229c46b py3: fix crecord.py's editpatchwitheditor exception message encoding
Emmanuel Leblond <emmanuel.leblond@gmail.com>
parents: 43159
diff changeset
  1814
                self.errorstr = stringutil.forcebytestr(exc)
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1815
                return None
41991
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1816
            finally:
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1817
                self.stdscr.clear()
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1818
                self.stdscr.refresh()
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1819
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1820
            # remove comment lines
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1821
            patch = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1822
                line + b'\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1823
                for line in patch.splitlines()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1824
                if not line.startswith(b'#')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1825
            ]
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1826
            return patchmod.parsepatch(patch)
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1827
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1828
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1829
            item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1830
        if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1831
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1832
        if isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1833
            item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1834
        if not isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1835
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1836
27914
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1837
        # To go back to that hunk or its replacement at the end of the edit
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1838
        itemindex = item.parentitem().hunks.index(item)
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1839
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1840
        beforeadded, beforeremoved = item.added, item.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1841
        newpatches = editpatchwitheditor(self, item)
25557
52c552a05414 crecord: exit edition of hunk with non-zero status does not interrupt session
Laurent Charignon <lcharignon@fb.com>
parents: 25556
diff changeset
  1842
        if newpatches is None:
52c552a05414 crecord: exit edition of hunk with non-zero status does not interrupt session
Laurent Charignon <lcharignon@fb.com>
parents: 25556
diff changeset
  1843
            if not test:
52c552a05414 crecord: exit edition of hunk with non-zero status does not interrupt session
Laurent Charignon <lcharignon@fb.com>
parents: 25556
diff changeset
  1844
                updateui(self)
52c552a05414 crecord: exit edition of hunk with non-zero status does not interrupt session
Laurent Charignon <lcharignon@fb.com>
parents: 25556
diff changeset
  1845
            return
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1846
        header = item.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1847
        editedhunkindex = header.hunks.index(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1848
        hunksbefore = header.hunks[:editedhunkindex]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1849
        hunksafter = header.hunks[editedhunkindex + 1 :]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1850
        newpatchheader = newpatches[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1851
        newhunks = [uihunk(h, header) for h in newpatchheader.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1852
        newadded = sum([h.added for h in newhunks])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1853
        newremoved = sum([h.removed for h in newhunks])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1854
        offset = (newadded - beforeadded) - (newremoved - beforeremoved)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1855
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1856
        for h in hunksafter:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1857
            h.toline += offset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1858
        for h in newhunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1859
            h.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1860
        header.hunks = hunksbefore + newhunks + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1861
        if self.emptypatch():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1862
            header.hunks = hunksbefore + [item] + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1863
        self.currentselecteditem = header
27914
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1864
        if len(header.hunks) > itemindex:
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1865
            self.currentselecteditem = header.hunks[itemindex]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1866
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1867
        if not test:
25555
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1868
            updateui(self)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1869
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1870
    def emptypatch(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1871
        item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1872
        if not item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1873
            return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1874
        for header in item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1875
            if header.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1876
                return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1877
        return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1878
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1879
    def handlekeypressed(self, keypressed, test=False):
28581
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1880
        """
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1881
        Perform actions based on pressed keys.
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1882
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1883
        Return true to exit the main loop.
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1884
        """
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1885
        if keypressed in ["k", "KEY_UP"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1886
            self.uparrowevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1887
        elif keypressed in ["K", "KEY_PPAGE"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1888
            self.uparrowshiftevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1889
        elif keypressed in ["j", "KEY_DOWN"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1890
            self.downarrowevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1891
        elif keypressed in ["J", "KEY_NPAGE"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1892
            self.downarrowshiftevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1893
        elif keypressed in ["l", "KEY_RIGHT"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1894
            self.rightarrowevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1895
        elif keypressed in ["h", "KEY_LEFT"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1896
            self.leftarrowevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1897
        elif keypressed in ["H", "KEY_SLEFT"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1898
            self.leftarrowshiftevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1899
        elif keypressed in ["q"]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1900
            raise error.Abort(_(b'user quit'))
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1901
        elif keypressed in ['a']:
43893
c06eba91c380 crecord: repurpose "a" key to toggle all selections (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 43891
diff changeset
  1902
            self.flipselections()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1903
        elif keypressed in ["c"]:
28925
ee56a86e2782 crecord: drop the extra confirmation screen
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28861
diff changeset
  1904
            return True
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1905
        elif keypressed in ["r"]:
28926
0411b7998d9b crecord: cleanup the remains of commit confirmation
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28925
diff changeset
  1906
            if self.reviewcommit():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1907
                self.opts[b'review'] = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1908
                return True
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1909
        elif test and keypressed in ["R"]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1910
            self.opts[b'review'] = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1911
            return True
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1912
        elif keypressed in [" ", "x"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1913
            self.toggleapply()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1914
        elif keypressed in ["\n", "KEY_ENTER"]:
40253
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
  1915
            self.toggleapply()
42572
cd4f1b7c3192 crecord: make KEY_ENTER usable in tests (by not updating UI)
Kyle Lippincott <spectral@google.com>
parents: 42571
diff changeset
  1916
            self.nextsametype(test=test)
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1917
        elif keypressed in ["X"]:
42573
9ac1a5a4a64f crecord: provide 'X' as a range-select mechanism
Kyle Lippincott <spectral@google.com>
parents: 42572
diff changeset
  1918
            self.toggleallbetween()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1919
        elif keypressed in ["A"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1920
            self.toggleall()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1921
        elif keypressed in ["e"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1922
            self.toggleedit(test=test)
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1923
        elif keypressed in ["f"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1924
            self.togglefolded()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1925
        elif keypressed in ["F"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1926
            self.togglefolded(foldparent=True)
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1927
        elif keypressed in ["m"]:
33991
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  1928
            self.commitMessageWindow()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1929
        elif keypressed in ["g", "KEY_HOME"]:
42073
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1930
            self.handlefirstlineevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1931
        elif keypressed in ["G", "KEY_END"]:
42073
80103ed2e8ee crecord: new keys g & G to navigate to the top and bottom respectively
Arun Chandrasekaran <aruncxy@gmail.com>
parents: 42025
diff changeset
  1932
            self.handlelastlineevent()
43424
7cc913396f8c py3: keep "keypressed" a native str in crecord
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43423
diff changeset
  1933
        elif keypressed in ["?"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1934
            self.helpwindow()
25419
6e62a5b3442d crecord: fix blue artifact bug coming back from help screen
Laurent Charignon <lcharignon@fb.com>
parents: 25359
diff changeset
  1935
            self.stdscr.clear()
6e62a5b3442d crecord: fix blue artifact bug coming back from help screen
Laurent Charignon <lcharignon@fb.com>
parents: 25359
diff changeset
  1936
            self.stdscr.refresh()
43425
be0f77fd274d py3: fix handling of ctrl keys in crecord (issue6213)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43424
diff changeset
  1937
        elif keypressed in [curses.ascii.ctrl("L")]:
41992
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1938
            # scroll the current line to the top of the screen, and redraw
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1939
            # everything
29961
7d053ba73178 crecord: add an event that scrolls the selected line to the top of the screen
Nathan Goldbaum <ngoldbau@illinois.edu>
parents: 29946
diff changeset
  1940
            self.scrolllines(self.selecteditemstartline)
41992
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1941
            self.stdscr.clear()
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1942
            self.stdscr.refresh()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1943
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1944
    def main(self, stdscr):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1945
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1946
        method to be wrapped by curses.wrapper() for selecting chunks.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1947
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1948
31931
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1949
        origsigwinch = sentinel = object()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1950
        if util.safehasattr(signal, b'SIGWINCH'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1951
            origsigwinch = signal.signal(signal.SIGWINCH, self.sigwinchhandler)
31930
7e7743a01103 crecord: ensure we reinstall the SIGWINCH handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31929
diff changeset
  1952
        try:
7e7743a01103 crecord: ensure we reinstall the SIGWINCH handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31929
diff changeset
  1953
            return self._main(stdscr)
7e7743a01103 crecord: ensure we reinstall the SIGWINCH handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31929
diff changeset
  1954
        finally:
31931
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1955
            if origsigwinch is not sentinel:
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1956
                signal.signal(signal.SIGWINCH, origsigwinch)
31929
bf6b44da1d8e crecord: extract most of 'main' into a sub function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31654
diff changeset
  1957
bf6b44da1d8e crecord: extract most of 'main' into a sub function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31654
diff changeset
  1958
    def _main(self, stdscr):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1959
        self.stdscr = stdscr
25820
701d8c362aa2 crecord: add error reporting for failure in curses interface initialization
Laurent Charignon <lcharignon@fb.com>
parents: 25807
diff changeset
  1960
        # error during initialization, cannot be printed in the curses
701d8c362aa2 crecord: add error reporting for failure in curses interface initialization
Laurent Charignon <lcharignon@fb.com>
parents: 25807
diff changeset
  1961
        # interface, it should be printed by the calling code
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
  1962
        self.initexc = None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1963
        self.yscreensize, self.xscreensize = self.stdscr.getmaxyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1964
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1965
        curses.start_color()
35532
fb2e59e92651 crecord: fallback to color = no when curses.use_default_colors() fails
Elmar Bartel <elb_hg@leo.org>
parents: 35531
diff changeset
  1966
        try:
fb2e59e92651 crecord: fallback to color = no when curses.use_default_colors() fails
Elmar Bartel <elb_hg@leo.org>
parents: 35531
diff changeset
  1967
            curses.use_default_colors()
fb2e59e92651 crecord: fallback to color = no when curses.use_default_colors() fails
Elmar Bartel <elb_hg@leo.org>
parents: 35531
diff changeset
  1968
        except curses.error:
fb2e59e92651 crecord: fallback to color = no when curses.use_default_colors() fails
Elmar Bartel <elb_hg@leo.org>
parents: 35531
diff changeset
  1969
            self.usecolor = False
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1970
41993
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1971
        # In some situations we may have some cruft left on the "alternate
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1972
        # screen" from another program (or previous iterations of ourself), and
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1973
        # we won't clear it if the scroll region is small enough to comfortably
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1974
        # fit on the terminal.
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1975
        self.stdscr.clear()
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1976
42025
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1977
        # don't display the cursor
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1978
        try:
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1979
            curses.curs_set(0)
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1980
        except curses.error:
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1981
            pass
66fc05ff0ea3 crecord: draw on the whole screen
Alexander Kobjolke <alex@jakalx.net>
parents: 41993
diff changeset
  1982
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1983
        # available colors: black, blue, cyan, green, magenta, white, yellow
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1984
        # init_pair(color_id, foreground_color, background_color)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1985
        self.initcolorpair(None, None, name=b"normal")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1986
        self.initcolorpair(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1987
            curses.COLOR_WHITE, curses.COLOR_MAGENTA, name=b"selected"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1988
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1989
        self.initcolorpair(curses.COLOR_RED, None, name=b"deletion")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1990
        self.initcolorpair(curses.COLOR_GREEN, None, name=b"addition")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1991
        self.initcolorpair(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1992
            curses.COLOR_WHITE, curses.COLOR_BLUE, name=b"legend"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1993
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1994
        # newwin([height, width,] begin_y, begin_x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1995
        self.statuswin = curses.newwin(self.numstatuslines, 0, 0, 0)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  1996
        self.statuswin.keypad(1)  # interpret arrow-key, etc. esc sequences
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1997
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1998
        # figure out how much space to allocate for the chunk-pad which is
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1999
        # used for displaying the patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2000
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2001
        # stupid hack to prevent getnumlinesdisplayed from failing
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2002
        self.chunkpad = curses.newpad(1, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2003
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2004
        # add 1 so to account for last line text reaching end of line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2005
        self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2006
25821
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  2007
        try:
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  2008
            self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  2009
        except curses.error:
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
  2010
            self.initexc = fallbackerror(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2011
                _(b'this diff is too large to be displayed')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2012
            )
25821
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  2013
            return
30342
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30328
diff changeset
  2014
        # initialize selecteditemendline (initial start-line is 0)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2015
        self.selecteditemendline = self.getnumlinesdisplayed(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2016
            self.currentselecteditem, recursechildren=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2017
        )
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2018
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2019
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2020
            self.updatescreen()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2021
            try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2022
                with self.ui.timeblockedsection(b'crecord'):
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  2023
                    keypressed = self.statuswin.getkey()
25556
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  2024
                if self.errorstr is not None:
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  2025
                    self.errorstr = None
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  2026
                    continue
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2027
            except curses.error:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2028
                keypressed = b"foobar"
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2029
            if self.handlekeypressed(keypressed):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  2030
                break
33991
a1cd6eae2ad3 record: make the m key open an editor for the commit message (issue5667)
Peter Vitt <peter.vitt2@uni-siegen.de>
parents: 33834
diff changeset
  2031
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2032
        if self.commenttext != b"":
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2033
            whitespaceremoved = re.sub(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2034
                br"(?m)^\s.*(\n|$)", b"", self.commenttext
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42650
diff changeset
  2035
            )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2036
            if whitespaceremoved != b"":
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  2037
                self.opts[b'message'] = self.commenttext