mercurial/crecord.py
author Kyle Lippincott <spectral@google.com>
Wed, 13 Mar 2019 20:09:56 -0700
changeset 41993 cde5827d09a7
parent 41992 fa3b0ca9d74f
child 42025 66fc05ff0ea3
permissions -rw-r--r--
crecord: redraw the screen when starting up chunkselector Failure to do this can cause screen corruption like: <headerline> [X] filename.cc <several blank lines> <output from previous iteration of split that happened to be here> I believe this might only happen in some terminals, and maybe only when using the "alternate screen". Regardless of the exact conditions to reproduce, it should be safe to always clear it when starting up and is probably the correct thing to do anyway :) Differential Revision: https://phab.mercurial-scm.org/D6131
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 _
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    19
from . import (
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    20
    encoding,
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26421
diff changeset
    21
    error,
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    22
    patch as patchmod,
41848
25420df87903 py3: convert KEY_PRESSED value to bytes in crecord.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41842
diff changeset
    23
    pycompat,
30328
0911191dc4c9 crecord: use scmutil.termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 29961
diff changeset
    24
    scmutil,
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
    25
    util,
25940
f4356e5094ba crecord: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25821
diff changeset
    26
)
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36466
diff changeset
    27
from .utils import (
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36466
diff changeset
    28
    stringutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36466
diff changeset
    29
)
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
    30
stringio = util.stringio
24314
348492ba632a crecord: more import style
Matt Mackall <mpm@selenic.com>
parents: 24313
diff changeset
    31
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    32
# This is required for ncurses to display non-ASCII characters in default user
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    33
# locale encoding correctly.  --immerrr
41842
82d9728ace95 global: use raw string for setlocale() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41711
diff changeset
    34
locale.setlocale(locale.LC_ALL, r'')
24317
f559cae7892f crecord: fiddle with vertical whitespace
Matt Mackall <mpm@selenic.com>
parents: 24316
diff changeset
    35
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    36
# patch comments based on the git one
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    37
diffhelptext = _("""# To remove '-' lines, make them ' ' lines (context).
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.
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    40
""")
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    41
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    42
hunkhelptext = _("""#
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    43
# 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
    44
# 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
    45
# 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
    46
# 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
    47
# unchanged.
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
    48
""")
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    49
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    50
patchhelptext = _("""#
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    51
# 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
    52
# 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
    53
# generated. You can use those when you try again.
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    54
""")
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
    55
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    56
try:
24909
d71492ca2fdd crecord: fix mixed imports warning
Matt Harbison <matt_harbison@yahoo.com>
parents: 24840
diff changeset
    57
    import curses
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    58
    curses.error
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    59
except ImportError:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    60
    # 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
    61
    try:
01b39e821d00 crecord: conditionalize the imports that are not available on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 24351
diff changeset
    62
        import wcurses as curses
27528
7cc654610204 crecord: use try/except for import of curses
Sean Farley <sean@farley.io>
parents: 26781
diff changeset
    63
        curses.error
24423
01b39e821d00 crecord: conditionalize the imports that are not available on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 24351
diff changeset
    64
    except ImportError:
27530
ba30ef5bba95 crecord: ensure that curses is False if not imported
Sean Farley <sean@farley.io>
parents: 27529
diff changeset
    65
        # 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
    66
        # compiled with curses
ba30ef5bba95 crecord: ensure that curses is False if not imported
Sean Farley <sean@farley.io>
parents: 27529
diff changeset
    67
        curses = False
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    68
38080
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    69
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
    70
    """Error that indicates the client should try to fallback to text mode."""
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
    71
    # 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
    72
    # 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
    73
27529
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    74
def checkcurses(ui):
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    75
    """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
    76
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    77
    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
    78
    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
    79
    """
28543
f7874de435c5 crecord: use ui.interface to choose curses interface
Simon Farnsworth <simonfar@fb.com>
parents: 27937
diff changeset
    80
    return curses and ui.interface("chunkselector") == "curses"
27529
940cedaee988 crecord: add helper function to determine if we should use curses
Sean Farley <sean@farley.io>
parents: 27528
diff changeset
    81
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    82
class patchnode(object):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    83
    """abstract class for patch graph nodes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    84
    (i.e. patchroot, header, hunk, hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    85
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    86
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    87
    def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    88
        raise NotImplementedError("method must be implemented by subclass")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    89
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    90
    def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    91
        raise NotImplementedError("method must be implemented by subclass")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    92
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    93
    def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    94
        "Return a list of all of the direct children of this node"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    95
        raise NotImplementedError("method must be implemented by subclass")
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
    96
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    97
    def nextsibling(self):
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
        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
   100
        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
   101
        If no such item exists, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   102
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   103
        raise NotImplementedError("method must be implemented by subclass")
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 prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   106
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   107
        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
   108
        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
   109
        If no such item exists, return None.
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
        raise NotImplementedError("method must be implemented by subclass")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   112
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   113
    def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   114
        raise NotImplementedError("method must be implemented by subclass")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   115
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   116
    def nextitem(self, skipfolded=True):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   117
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   118
        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
   119
        type (header, hunk, or hunkline).
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   120
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   121
        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
   122
        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
   123
        the next item.
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
        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
   126
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   127
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   128
            itemfolded = self.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   129
        except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   130
            itemfolded = False
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   131
        if skipfolded and itemfolded:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   132
            nextitem = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   133
            if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   134
                try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   135
                    nextitem = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   136
                except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   137
                    nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   138
            return nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   139
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   140
            # try child
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   141
            item = self.firstchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   142
            if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   143
                return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   144
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   145
            # else try next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   146
            item = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   147
            if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   148
                return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   149
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   150
            try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   151
                # else try parent's next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   152
                item = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   153
                if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   154
                    return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   155
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   156
                # 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
   157
                return self.parentitem().parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   158
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   159
            except AttributeError: # parent and/or grandparent was None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   160
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   161
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   162
    def previtem(self):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   163
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   164
        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
   165
        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
   166
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   167
        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
   168
        """
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   169
        # 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
   170
        # 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
   171
        prevsibling = self.prevsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   172
        if prevsibling is not None:
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   173
            prevsiblinglastchild = prevsibling.lastchild()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   174
            if ((prevsiblinglastchild is not None) and
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   175
                not prevsibling.folded):
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   176
                prevsiblinglclc = prevsiblinglastchild.lastchild()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   177
                if ((prevsiblinglclc is not None) and
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   178
                    not prevsiblinglastchild.folded):
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   179
                    return prevsiblinglclc
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   180
                else:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   181
                    return prevsiblinglastchild
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   182
            else:
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   183
                return prevsibling
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   184
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   185
        # try parent (or None)
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   186
        return self.parentitem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   187
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   188
class patch(patchnode, list): # todo: rename patchroot
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   189
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   190
    list of header objects representing the patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   191
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   192
    def __init__(self, headerlist):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   193
        self.extend(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   194
        # 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
   195
        for header in self:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   196
            header.patch = self
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   197
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   198
class uiheader(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   199
    """patch header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   200
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
   201
    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
   202
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   203
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   204
    def __init__(self, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   205
        self.nonuiheader = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   206
        # 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
   207
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   208
        # 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
   209
        # 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
   210
        self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   211
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   212
        # 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
   213
        self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   214
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   215
        # list of all headers in patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   216
        self.patch = None
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
        # 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
   219
        self.neverunfolded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   220
        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
   221
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   222
    def prettystr(self):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   223
        x = stringio()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   224
        self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   225
        return x.getvalue()
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
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   228
        numheadersinpatch = len(self.patch)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   229
        indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   230
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   231
        if indexofthisheader < numheadersinpatch - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   232
            nextheader = self.patch[indexofthisheader + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   233
            return nextheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   234
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   235
            return None
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 prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   238
        indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   239
        if indexofthisheader > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   240
            previousheader = self.patch[indexofthisheader - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   241
            return previousheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   242
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   243
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   244
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   245
    def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   246
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   247
        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
   248
        so return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   249
        """
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 firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   253
        "return the first child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   254
        if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   255
            return self.hunks[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   256
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   257
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   258
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   259
    def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   260
        "return the last child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   261
        if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   262
            return self.hunks[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   263
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   264
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   265
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   266
    def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   267
        "return a list of all of the direct children of this node"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   268
        return self.hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   269
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   270
    def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   271
        return getattr(self.nonuiheader, name)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   272
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   273
class uihunkline(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   274
    "represents a changed line in a hunk"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   275
    def __init__(self, linetext, hunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   276
        self.linetext = linetext
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   277
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   278
        # 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
   279
        self.hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   280
        # 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
   281
        # in the previtem method.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   282
        self.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   283
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   284
    def prettystr(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   285
        return self.linetext
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 nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   288
        numlinesinhunk = len(self.hunk.changedlines)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   289
        indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   290
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   291
        if (indexofthisline < numlinesinhunk - 1):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   292
            nextline = self.hunk.changedlines[indexofthisline + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   293
            return nextline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   294
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   295
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   296
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   297
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   298
        indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   299
        if indexofthisline > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   300
            previousline = self.hunk.changedlines[indexofthisline - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   301
            return previousline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   302
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   303
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   304
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   305
    def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   306
        "return the parent to the current item"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   307
        return self.hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   308
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   309
    def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   310
        "return the first child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   311
        # hunk-lines don't have children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   312
        return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   313
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   314
    def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   315
        "return the last child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   316
        # hunk-lines don't have children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   317
        return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   318
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   319
class uihunk(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   320
    """ui patch hunk, wraps a hunk and keep track of ui behavior """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   321
    maxcontext = 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   322
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   323
    def __init__(self, hunk, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   324
        self._hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   325
        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
   326
        self.header = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   327
        # 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
   328
        self.originalremoved = self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   329
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   330
        # 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
   331
        self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   332
        # 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
   333
        self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   334
        # 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
   335
        # 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
   336
        self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   337
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   338
    def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   339
        numhunksinheader = len(self.header.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   340
        indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   341
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   342
        if (indexofthishunk < numhunksinheader - 1):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   343
            nexthunk = self.header.hunks[indexofthishunk + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   344
            return nexthunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   345
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   346
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   347
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   348
    def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   349
        indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   350
        if indexofthishunk > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   351
            previoushunk = self.header.hunks[indexofthishunk - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   352
            return previoushunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   353
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   354
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   355
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   356
    def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   357
        "return the parent to the current item"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   358
        return self.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   359
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   360
    def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   361
        "return the first child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   362
        if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   363
            return self.changedlines[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   364
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   365
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   366
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   367
    def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   368
        "return the last child of this item, if one exists.  otherwise None."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   369
        if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   370
            return self.changedlines[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   371
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   372
            return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   373
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   374
    def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   375
        "return a list of all of the direct children of this node"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   376
        return self.changedlines
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
   377
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   378
    def countchanges(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   379
        """changedlines -> (n+,n-)"""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   380
        add = len([l for l in self.changedlines if l.applied
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   381
                    and l.prettystr().startswith('+')])
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   382
        rem = len([l for l in self.changedlines if l.applied
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   383
                    and l.prettystr().startswith('-')])
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   384
        return add, rem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   385
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   386
    def getfromtoline(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   387
        # 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
   388
        removedconvertedtocontext = self.originalremoved - self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   389
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   390
        contextlen = (len(self.before) + len(self.after) +
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   391
                      removedconvertedtocontext)
29668
e7766022a61a crecord: properly handle files with No newline at eof (issue5268)
timeless <timeless@mozdev.org>
parents: 29327
diff changeset
   392
        if self.after and self.after[-1] == '\\ 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
   393
            contextlen -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   394
        fromlen = contextlen + self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   395
        tolen = contextlen + self.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   396
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   397
        # 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
   398
        # 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
   399
        # precedes the hunk."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   400
        #
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   401
        # 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
   402
        # 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
   403
        fromline, toline = self.fromline, self.toline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   404
        if fromline != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   405
            if fromlen == 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   406
                fromline -= 1
37875
03350f5234a4 crecord: fix line number in hunk header (issue5917)
Jun Wu <quark@fb.com>
parents: 37087
diff changeset
   407
            if tolen == 0 and toline > 0:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   408
                toline -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   409
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   410
        fromtoline = '@@ -%d,%d +%d,%d @@%s\n' % (
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   411
            fromline, fromlen, toline, tolen,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   412
            self.proc and (' ' + self.proc))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   413
        return fromtoline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   414
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   415
    def write(self, fp):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   416
        # 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
   417
        self.added, self.removed = self.countchanges()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   418
        fp.write(self.getfromtoline())
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   419
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   420
        hunklinelist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   421
        # 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
   422
        # (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
   423
        for changedline in self.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   424
            changedlinestr = changedline.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   425
            if changedline.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   426
                hunklinelist.append(changedlinestr)
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   427
            elif changedlinestr.startswith("-"):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   428
                hunklinelist.append(" " + changedlinestr[1:])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   429
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   430
        fp.write(''.join(self.before + hunklinelist + self.after))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   431
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   432
    pretty = write
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   433
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   434
    def prettystr(self):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   435
        x = stringio()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   436
        self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   437
        return x.getvalue()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   438
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   439
    def reversehunk(self):
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   440
        """return a recordhunk which is the reverse of the hunk
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   441
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   442
        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
   443
        intended to be applied to B, instead of A.
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   444
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   445
        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
   446
        the user made the following selection:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   447
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   448
                 0
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   449
            [x] -1           [x]: selected
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   450
            [ ] -2           [ ]: not selected
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   451
            [x] +3
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   452
            [ ] +4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   453
            [x] +5
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   454
                 6
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   455
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   456
        This function returns a hunk like:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   457
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   458
                 0
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   459
                -3
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   460
                -4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   461
                -5
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   462
                +1
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   463
                +4
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   464
                 6
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   465
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   466
        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
   467
        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
   468
        applicable to B.
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   469
        """
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   470
        dels = []
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   471
        adds = []
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   472
        for line in self.changedlines:
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   473
            text = line.linetext
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   474
            if line.applied:
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   475
                if text.startswith('+'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   476
                    dels.append(text[1:])
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   477
                elif text.startswith('-'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   478
                    adds.append(text[1:])
41634
7c4e205f71ca py3: use bytes.startswith() instead of comparing with bytes[0]
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41549
diff changeset
   479
            elif text.startswith('+'):
32997
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   480
                dels.append(text[1:])
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   481
                adds.append(text[1:])
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   482
        hunk = ['-%s' % l for l in dels] + ['+%s' % l for l in adds]
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   483
        h = self._hunk
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   484
        return patchmod.recordhunk(h.header, h.toline, h.fromline, h.proc,
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   485
                                   h.before, hunk, h.after)
66117dae87f9 patch: rewrite reversehunks (issue5337)
Jun Wu <quark@fb.com>
parents: 31933
diff changeset
   486
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   487
    def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   488
        return getattr(self._hunk, name)
29076
36d3535c6a47 crecord: add/remove blank lines (coding style)
Anton Shestakov <av6@dwimlabs.net>
parents: 28926
diff changeset
   489
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   490
    def __repr__(self):
41439
5c73441a47e5 crecord: always return a str from uihunk.__repr__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40413
diff changeset
   491
        return r'<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
   492
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   493
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
   494
    """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
   495
    chunks = list(chunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   496
    # 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
   497
    # 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
   498
    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
   499
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   500
    # if there are no changed files
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   501
    if len(headers) == 0:
27321
dcdf0a52ad36 crecord: add dictionary to default return value of filterpatch
Laurent Charignon <lcharignon@fb.com>
parents: 27156
diff changeset
   502
        return [], {}
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   503
    uiheaders = [uiheader(h) for h in headers]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   504
    # 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
   505
    # accordingly
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   506
    ret = chunkselector(ui, uiheaders, operation=operation)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   507
    appliedhunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   508
    for hdr in uiheaders:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   509
        if (hdr.applied and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   510
            (hdr.special() or len([h for h in hdr.hunks if h.applied]) > 0)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   511
            appliedhunklist.append(hdr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   512
            fixoffset = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   513
            for hnk in hdr.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   514
                if hnk.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   515
                    appliedhunklist.append(hnk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   516
                    # 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
   517
                    # 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
   518
                    if fixoffset:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   519
                        #hnk = copy.copy(hnk) # necessary??
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   520
                        hnk.toline += fixoffset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   521
                else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   522
                    fixoffset += hnk.removed - hnk.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   523
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   524
    return (appliedhunklist, ret)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   525
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   526
def chunkselector(ui, headerlist, operation=None):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   527
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   528
    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
   529
    of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   530
    """
24779
23727465ff72 record: add message when starting record's curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24492
diff changeset
   531
    ui.write(_('starting interactive selection\n'))
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   532
    chunkselector = curseschunkselector(headerlist, ui, operation)
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
   533
    origsigtstp = sentinel = object()
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   534
    if util.safehasattr(signal, 'SIGTSTP'):
b2478a996a82 crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31932
diff changeset
   535
        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
   536
    try:
20a68f714f9b crecord: ensure we reinstall the SIGTSTP handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31931
diff changeset
   537
        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
   538
        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
   539
            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
   540
        # 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
   541
    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
   542
        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
   543
            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
   544
    return chunkselector.opts
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   545
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   546
def testdecorator(testfn, f):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   547
    def u(*args, **kwargs):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   548
        return f(testfn, *args, **kwargs)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   549
    return u
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   550
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   551
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
   552
    """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   553
    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
   554
    of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   555
    """
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   556
    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
   557
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   558
    class dummystdscr(object):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   559
        def clear(self):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   560
            pass
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   561
        def refresh(self):
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   562
            pass
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   563
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
   564
    chunkselector.stdscr = dummystdscr()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   565
    if testfn and os.path.exists(testfn):
38115
1978abdb216c py3: make sure we open files in bytes mode
Pulkit Goyal <7895pulkit@gmail.com>
parents: 38080
diff changeset
   566
        testf = open(testfn, 'rb')
36466
4223bef1489c py3: convert a map expression into list comprehension
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36339
diff changeset
   567
        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
   568
        testf.close()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   569
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   570
            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
   571
                break
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 26781
diff changeset
   572
    return chunkselector.opts
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   573
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   574
_headermessages = { # {operation: text}
35091
57c79542bebb crecord: fix revert -ir '.^' crash caused by 3649c3f2cd
Jun Wu <quark@fb.com>
parents: 34046
diff changeset
   575
    'apply': _('Select hunks to apply'),
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   576
    'discard': _('Select hunks to discard'),
41987
c1d83d916e85 revert: option to choose what to keep, not what to discard
Martin von Zweigbergk <martinvonz@google.com>
parents: 41944
diff changeset
   577
    'keep': _('Select hunks to keep'),
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   578
    None: _('Select hunks to record'),
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   579
}
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   580
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   581
class curseschunkselector(object):
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   582
    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
   583
        # put the headers into a patch object
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   584
        self.headerlist = patch(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   585
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   586
        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
   587
        self.opts = {}
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   588
25556
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
   589
        self.errorstr = None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   590
        # list of all chunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   591
        self.chunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   592
        for h in headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   593
            self.chunklist.append(h)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   594
            self.chunklist.extend(h.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   595
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   596
        # dictionary mapping (fgcolor, bgcolor) pairs to the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   597
        # corresponding curses color-pair value.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   598
        self.colorpairs = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   599
        # 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
   600
        self.colorpairnames = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   601
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   602
        # 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
   603
        # 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
   604
        # 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
   605
        # previous (color only) behaviour.
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36466
diff changeset
   606
        uicolor = stringutil.parsebool(self.ui.config('ui', 'color'))
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   607
        self.usecolor = uicolor is not False
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
   608
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   609
        # 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
   610
        self.currentselecteditem = self.headerlist[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   611
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   612
        # 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
   613
        # 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
   614
        self.selecteditemstartline = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   615
        self.selecteditemendline = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   616
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   617
        # define indentation levels
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   618
        self.headerindentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   619
        self.hunkindentnumchars = 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   620
        self.hunklineindentnumchars = 6
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   621
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   622
        # 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
   623
        self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   624
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   625
        # 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
   626
        self.numpadlines = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   627
30554
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
   628
        self.numstatuslines = 1
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   629
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   630
        # 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
   631
        # (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
   632
        self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   633
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   634
        # the first line of the pad which is visible on the screen
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   635
        self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   636
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   637
        # stores optional text for a commit comment provided by the user
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   638
        self.commenttext = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   639
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   640
        # 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
   641
        self.waslasttoggleallapplied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   642
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   643
        # affects some ui text
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
   644
        if operation not in _headermessages:
31654
4bc3e55cf386 crecord: use ProgrammingError
Jun Wu <quark@fb.com>
parents: 31001
diff changeset
   645
            raise error.ProgrammingError('unexpected operation: %s' % operation)
30542
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   646
        self.operation = operation
40fe96fc7cd2 crecord: add an "operation" field
Jun Wu <quark@fb.com>
parents: 30342
diff changeset
   647
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   648
    def uparrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   649
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   650
        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
   651
        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
   652
        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
   653
        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
   654
        hunk itself.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   655
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   656
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   657
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   658
        nextitem = currentitem.previtem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   659
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   660
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   661
            # 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
   662
            # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   663
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   664
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   665
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   666
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   667
    def uparrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   668
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   669
        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
   670
        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
   671
        parent-item of the currently selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   672
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   673
        currentitem = self.currentselecteditem
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   674
        nextitem = currentitem.prevsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   675
        # 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
   676
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   677
            nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   678
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   679
            # 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
   680
            # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   681
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   682
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   683
        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
   684
        self.recenterdisplayedarea()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   685
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   686
    def downarrowevent(self):
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
        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
   689
        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
   690
        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
   691
        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
   692
        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
   693
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   694
        #self.startprintline += 1 #debug
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   695
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   696
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   697
        nextitem = currentitem.nextitem()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   698
        # 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
   699
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   700
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   701
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   702
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   703
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   704
    def downarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   705
        """
29081
4abca4cbe768 crecord: update downarrowshiftevent() docstring, remove todo
Anton Shestakov <av6@dwimlabs.net>
parents: 29080
diff changeset
   706
        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
   707
        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
   708
        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
   709
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   710
        currentitem = self.currentselecteditem
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   711
        nextitem = currentitem.nextsibling()
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   712
        # 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
   713
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   714
            try:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   715
                nextitem = currentitem.parentitem().nextsibling()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   716
            except AttributeError:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   717
                # 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
   718
                nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   719
        if nextitem is None:
29130
ed2a3818c1fc crecord: call prevsibling() and nextsibling() directly
Anton Shestakov <av6@dwimlabs.net>
parents: 29081
diff changeset
   720
            # 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
   721
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   722
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   723
        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
   724
        self.recenterdisplayedarea()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   725
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
   726
    def nextsametype(self):
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   727
        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
   728
        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
   729
        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
   730
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   731
        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
   732
            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
   733
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   734
        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
   735
            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
   736
        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
   737
            parent = nextitem.parentitem()
40413
7e4ffe2719e4 crecord: make nextsametype() check that parent item exists (issue6009)
Anton Shestakov <av6@dwimlabs.net>
parents: 40253
diff changeset
   738
            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
   739
                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
   740
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   741
        self.currentselecteditem = 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
   742
        self.recenterdisplayedarea()
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
   743
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   744
    def rightarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   745
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   746
        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
   747
        """
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
        nextitem = currentitem.firstchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   750
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   751
        # 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
   752
        if currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   753
            self.togglefolded(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
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   756
            # 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
   757
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   758
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   759
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   760
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   761
    def leftarrowevent(self):
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
        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
   764
        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
   765
        of this item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   766
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   767
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   768
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   769
        # try to fold the item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   770
        if not isinstance(currentitem, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   771
            if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   772
                self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   773
                return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   774
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   775
        # 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
   776
        nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   777
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   778
        if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   779
            # 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
   780
            nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   781
            if not nextitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   782
                self.togglefolded(item=nextitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   783
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   784
        self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   785
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   786
    def leftarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   787
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   788
        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
   789
        current item is already a header).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   790
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   791
        currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   792
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   793
        if isinstance(currentitem, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   794
            if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   795
                self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   796
                return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   797
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   798
        # 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
   799
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   800
            nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   801
            if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   802
                break
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   803
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   804
                currentitem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   805
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   806
        self.currentselecteditem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   807
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   808
    def updatescroll(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   809
        "scroll the screen to fully show the currently-selected"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   810
        selstart = self.selecteditemstartline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   811
        selend = self.selecteditemendline
29946
3664537386ab crecord: delete commented line
Nathan Goldbaum <ngoldbau@illinois.edu>
parents: 29941
diff changeset
   812
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   813
        padstart = self.firstlineofpadtoprint
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   814
        padend = padstart + self.yscreensize - self.numstatuslines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   815
        # '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
   816
        # top/bottom context margin
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   817
        padstartbuffered = padstart + 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   818
        padendbuffered = padend - 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   819
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   820
        if selend > padendbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   821
            self.scrolllines(selend - padendbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   822
        elif selstart < padstartbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   823
            # negative values scroll in pgup direction
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   824
            self.scrolllines(selstart - padstartbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   825
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   826
    def scrolllines(self, numlines):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   827
        "scroll the screen up (down) by numlines when numlines >0 (<0)."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   828
        self.firstlineofpadtoprint += numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   829
        if self.firstlineofpadtoprint < 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   830
            self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   831
        if self.firstlineofpadtoprint > self.numpadlines - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   832
            self.firstlineofpadtoprint = self.numpadlines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   833
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   834
    def toggleapply(self, item=None):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   835
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   836
        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
   837
        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
   838
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   839
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   840
            item = self.currentselecteditem
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
        item.applied = not item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   843
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   844
        if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   845
            item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   846
            if item.applied:
24492
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   847
                # apply all its hunks
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   848
                for hnk in item.hunks:
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   849
                    hnk.applied = True
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   850
                    # apply all their hunklines
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   851
                    for hunkline in hnk.changedlines:
efa094701a05 record_curses: fix ui bug for newly added file
Laurent Charignon <lcharignon@fb.com>
parents: 24425
diff changeset
   852
                        hunkline.applied = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   853
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   854
                # un-apply all its hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   855
                for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   856
                    hnk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   857
                    hnk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   858
                    # un-apply all their hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   859
                    for hunkline in hnk.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   860
                        hunkline.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   861
        elif isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   862
            item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   863
            # apply all it's hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   864
            for hunkline in item.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   865
                hunkline.applied = item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   866
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   867
            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
   868
            allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   869
            nosiblingsapplied = not (True in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   870
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   871
            siblingspartialstatus = [hnk.partial for hnk in item.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   872
            somesiblingspartial = (True in siblingspartialstatus)
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
            #cases where applied or partial should be removed from header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   875
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   876
            # 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
   877
            if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   878
                if not item.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   879
                    item.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   880
                    item.header.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   881
            else: # some/all parent siblings are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   882
                item.header.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   883
                item.header.partial = (somesiblingspartial or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   884
                                        not allsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   885
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   886
        elif isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   887
            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
   888
            allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   889
            nosiblingsapplied = not (True in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   890
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   891
            # if no 'sibling' lines are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   892
            if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   893
                item.hunk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   894
                item.hunk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   895
            elif allsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   896
                item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   897
                item.hunk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   898
            else: # some siblings applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   899
                item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   900
                item.hunk.partial = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   901
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   902
            parentsiblingsapplied = [hnk.applied for hnk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   903
                                     in item.hunk.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   904
            noparentsiblingsapplied = not (True in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   905
            allparentsiblingsapplied = not (False in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   906
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   907
            parentsiblingspartial = [hnk.partial for hnk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   908
                                     in item.hunk.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   909
            someparentsiblingspartial = (True in parentsiblingspartial)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   910
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   911
            # 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
   912
            if noparentsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   913
                if not item.hunk.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   914
                    item.hunk.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   915
                    item.hunk.header.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   916
            # set the applied and partial status of the header if needed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   917
            else: # some/all parent siblings are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   918
                item.hunk.header.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   919
                item.hunk.header.partial = (someparentsiblingspartial or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   920
                                            not allparentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   921
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   922
    def toggleall(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   923
        "toggle the applied flag of all items."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   924
        if self.waslasttoggleallapplied: # then unapply them this time
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   925
            for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   926
                if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   927
                    self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   928
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   929
            for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   930
                if not item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   931
                    self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   932
        self.waslasttoggleallapplied = not self.waslasttoggleallapplied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   933
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   934
    def togglefolded(self, item=None, foldparent=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   935
        "toggle folded flag of specified item (defaults to currently selected)"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   936
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   937
            item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   938
        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
   939
            if not isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   940
                # 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
   941
                self.currentselecteditem = item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   942
            elif item.neverunfolded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   943
                item.neverunfolded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   944
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   945
            # also fold any foldable children of the parent/current item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   946
            if isinstance(item, uiheader): # the original or 'new' item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   947
                for child in item.allchildren():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   948
                    child.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   949
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   950
        if isinstance(item, (uiheader, uihunk)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   951
            item.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   952
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   953
    def alignstring(self, instr, window):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   954
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   955
        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
   956
        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
   957
        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
   958
        multiple lines.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   959
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   960
        y, xstart = window.getyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   961
        width = self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   962
        # turn tabs into spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   963
        instr = instr.expandtabs(4)
24351
cdc4f3af2497 crecord: use colwidth instead of ucolwidth
Matt Mackall <mpm@selenic.com>
parents: 24342
diff changeset
   964
        strwidth = encoding.colwidth(instr)
24315
7d5f935bd705 crecord: use encoding.ucolwidth
Matt Mackall <mpm@selenic.com>
parents: 24314
diff changeset
   965
        numspaces = (width - ((strwidth + xstart) % width) - 1)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   966
        return instr + " " * numspaces + "\n"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   967
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   968
    def printstring(self, window, text, fgcolor=None, bgcolor=None, pair=None,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   969
        pairname=None, attrlist=None, towin=True, align=True, showwhtspc=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   970
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   971
        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
   972
        the specified curses window object.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   973
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   974
        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
   975
        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
   976
        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
   977
        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
   978
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   979
        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
   980
        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
   981
        underline].
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   982
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   983
        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
   984
        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
   985
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   986
        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
   987
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   988
        # preprocess the text, converting tabs to spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   989
        text = text.expandtabs(4)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   990
        # strip \n, and convert control characters to ^[char] representation
36338
a8bca9d0cea0 py3: make sure regex is bytes in crecord.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35532
diff changeset
   991
        text = re.sub(br'[\x00-\x08\x0a-\x1f]',
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   992
                lambda m:'^' + chr(ord(m.group()) + 64), text.strip('\n'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   993
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   994
        if pair is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   995
            colorpair = pair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   996
        elif pairname is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   997
            colorpair = self.colorpairnames[pairname]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   998
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   999
            if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1000
                fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1001
            if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1002
                bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1003
            if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1004
                colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1005
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1006
                colorpair = self.getcolorpair(fgcolor, bgcolor)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1007
        # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1008
        if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1009
            attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1010
        if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1011
            # 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
  1012
            for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1013
                colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1014
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1015
            # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1016
            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
  1017
                if textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1018
                    colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1019
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1020
        y, xstart = self.chunkpad.getyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1021
        t = "" # variable for counting lines printed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1022
        # if requested, show trailing whitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1023
        if showwhtspc:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1024
            origlen = len(text)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1025
            text = text.rstrip(' \n') # tabs have already been expanded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1026
            strippedlen = len(text)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1027
            numtrailingspaces = origlen - strippedlen
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1028
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1029
        if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1030
            window.addstr(text, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1031
        t += text
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1032
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1033
        if showwhtspc:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1034
                wscolorpair = colorpair | curses.A_REVERSE
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1035
                if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1036
                    for i in range(numtrailingspaces):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1037
                        window.addch(curses.ACS_CKBOARD, wscolorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1038
                t += " " * numtrailingspaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1039
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1040
        if align:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1041
            if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1042
                extrawhitespace = self.alignstring("", window)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1043
                window.addstr(extrawhitespace, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1044
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1045
                # 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
  1046
                extrawhitespace = self.alignstring(t, window)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1047
            t += extrawhitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1048
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1049
        # 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
  1050
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1051
        linesprinted = (xstart + len(t)) / self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1052
        self.linesprintedtopadsofar += linesprinted
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1053
        return t
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1054
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1055
    def _getstatuslinesegments(self):
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1056
        """-> [str]. return segments"""
30556
5129ed3c2548 crecord: change help text for the space key dynamically
Jun Wu <quark@fb.com>
parents: 30555
diff changeset
  1057
        selected = self.currentselecteditem.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
  1058
        spaceselect = _('space/enter: select')
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
  1059
        spacedeselect = _('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
  1060
        # 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
  1061
        # 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
  1062
        spacelen = max(len(spaceselect), len(spacedeselect))
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1063
        selectedlabel = '%-*s' % (spacelen,
1e71a27dee97 crecord: fixes the formatting of the select status in the status line
Filip Filmar <filmil@gmail.com>
parents: 32997
diff changeset
  1064
                                  spacedeselect if selected else spaceselect)
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1065
        segments = [
30557
8d9745ff1e62 crecord: change the verb according to the operation
Jun Wu <quark@fb.com>
parents: 30556
diff changeset
  1066
            _headermessages[self.operation],
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1067
            '-',
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1068
            _('[x]=selected **=collapsed'),
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1069
            _('c: confirm'),
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1070
            _('q: abort'),
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1071
            _('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
  1072
            selectedlabel,
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1073
            _('?: help'),
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1074
        ]
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1075
        return segments
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1076
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1077
    def _getstatuslines(self):
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1078
        """() -> [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
  1079
        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
  1080
            lines = [self.errorstr, _('Press any key to continue')]
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1081
        else:
30555
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1082
            # wrap segments to lines
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1083
            segments = self._getstatuslinesegments()
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1084
            width = self.xscreensize
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1085
            lines = []
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1086
            lastwidth = width
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1087
            for s in segments:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1088
                w = encoding.colwidth(s)
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1089
                sep = ' ' * (1 + (s and s[0] not in '-['))
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1090
                if lastwidth + w + len(sep) >= width:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1091
                    lines.append(s)
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1092
                    lastwidth = w
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1093
                else:
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1094
                    lines[-1] += sep + s
9b674957e2e4 crecord: rewrite status line text (BC)
Jun Wu <quark@fb.com>
parents: 30554
diff changeset
  1095
                    lastwidth += w + len(sep)
30554
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1096
        if len(lines) != self.numstatuslines:
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1097
            self.numstatuslines = len(lines)
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1098
            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
  1099
        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
  1100
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1101
    def updatescreen(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1102
        self.statuswin.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1103
        self.chunkpad.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1104
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1105
        printstring = self.printstring
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
        # 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
  1108
        try:
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1109
            for line in self._getstatuslines():
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1110
                printstring(self.statuswin, line, pairname="legend")
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1111
            self.statuswin.refresh()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1112
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1113
            pass
30553
d4035372db8d crecord: move status window text calculation to a separate method
Jun Wu <quark@fb.com>
parents: 30544
diff changeset
  1114
        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
  1115
            return
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1116
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1117
        # 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
  1118
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1119
            self.printitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1120
            self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1121
            self.chunkpad.refresh(self.firstlineofpadtoprint, 0,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1122
                                  self.numstatuslines, 0,
30554
f3cff00c7a00 crecord: make _getstatuslines update numstatuslines
Jun Wu <quark@fb.com>
parents: 30553
diff changeset
  1123
                                  self.yscreensize - self.numstatuslines,
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1124
                                  self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1125
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1126
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1127
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1128
    def getstatusprefixstring(self, item):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1129
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1130
        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
  1131
        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
  1132
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1133
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1134
        # create checkbox string
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1135
        if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1136
            if not isinstance(item, uihunkline) and item.partial:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1137
                checkbox = "[~]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1138
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1139
                checkbox = "[x]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1140
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1141
            checkbox = "[ ]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1142
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1143
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1144
            if item.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1145
                checkbox += "**"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1146
                if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1147
                    # 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
  1148
                    filestatus = item.changetype
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1149
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1150
                    checkbox += filestatus + " "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1151
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1152
                checkbox += "  "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1153
                if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1154
                    # add two more spaces for headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1155
                    checkbox += "  "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1156
        except AttributeError: # not foldable
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1157
            checkbox += "  "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1158
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1159
        return checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1160
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1161
    def printheader(self, header, selected=False, towin=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1162
                    ignorefolding=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1163
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1164
        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
  1165
        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
  1166
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1167
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1168
        outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1169
        text = header.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1170
        chunkindex = self.chunklist.index(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1171
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1172
        if chunkindex != 0 and not header.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1173
            # add separating line before headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1174
            outstr += self.printstring(self.chunkpad, '_' * self.xscreensize,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1175
                                       towin=towin, align=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1176
        # select color-pair based on if the header is selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1177
        colorpair = self.getcolorpair(name=selected and "selected" or "normal",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1178
                                      attrlist=[curses.A_BOLD])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1179
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1180
        # 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
  1181
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1182
        # 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
  1183
        indentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1184
        checkbox = self.getstatusprefixstring(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1185
        if not header.folded or ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1186
            textlist = text.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1187
            linestr = checkbox + textlist[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1188
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1189
            linestr = checkbox + header.filename()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1190
        outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1191
                                   towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1192
        if not header.folded or ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1193
            if len(textlist) > 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1194
                for line in textlist[1:]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1195
                    linestr = " "*(indentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1196
                    outstr += self.printstring(self.chunkpad, linestr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1197
                                               pair=colorpair, towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1198
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1199
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1200
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1201
    def printhunklinesbefore(self, hunk, selected=False, towin=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1202
                             ignorefolding=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1203
        "includes start/end line indicator"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1204
        outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1205
        # where hunk is in list of siblings
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1206
        hunkindex = hunk.header.hunks.index(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1207
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1208
        if hunkindex != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1209
            # add separating line before headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1210
            outstr += self.printstring(self.chunkpad, ' '*self.xscreensize,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1211
                                       towin=towin, align=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1212
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1213
        colorpair = self.getcolorpair(name=selected and "selected" or "normal",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1214
                                      attrlist=[curses.A_BOLD])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1215
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1216
        # print out from-to line with checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1217
        checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1218
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1219
        lineprefix = " "*self.hunkindentnumchars + checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1220
        frtoline = "   " + hunk.getfromtoline().strip("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1221
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1222
        outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1223
                                   align=False) # add uncolored checkbox/indent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1224
        outstr += self.printstring(self.chunkpad, frtoline, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1225
                                   towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1226
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1227
        if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1228
            # skip remainder of output
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1229
            return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1230
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1231
        # 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
  1232
        for line in hunk.before:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1233
            linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1234
            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
  1235
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1236
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1237
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1238
    def printhunklinesafter(self, hunk, towin=True, ignorefolding=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1239
        outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1240
        if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1241
            return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1242
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1243
        # 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
  1244
        checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1245
        for line in hunk.after:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1246
            linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1247
            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
  1248
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1249
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1250
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1251
    def printhunkchangedline(self, hunkline, selected=False, towin=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1252
        outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1253
        checkbox = self.getstatusprefixstring(hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1254
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1255
        linestr = hunkline.prettystr().strip("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1256
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1257
        # 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
  1258
        if selected:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1259
            colorpair = self.getcolorpair(name="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1260
        elif linestr.startswith("+"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1261
            colorpair = self.getcolorpair(name="addition")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1262
        elif linestr.startswith("-"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1263
            colorpair = self.getcolorpair(name="deletion")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1264
        elif linestr.startswith("\\"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1265
            colorpair = self.getcolorpair(name="normal")
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
        lineprefix = " "*self.hunklineindentnumchars + checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1268
        outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1269
                                   align=False) # add uncolored checkbox/indent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1270
        outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1271
                                   towin=towin, showwhtspc=True)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1272
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1273
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1274
    def printitem(self, item=None, ignorefolding=False, recursechildren=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1275
                  towin=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1276
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1277
        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
  1278
        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
  1279
        (hiding folded elements, etc. -- see __printitem() docstring)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1280
        """
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1281
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1282
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1283
            item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1284
        if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1285
            self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1286
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1287
        outstr = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1288
        self.__printitem(item, ignorefolding, recursechildren, outstr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1289
                                  towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1290
        return ''.join(outstr)
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
    def outofdisplayedarea(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1293
        y, _ = self.chunkpad.getyx() # cursor location
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1294
        # * 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
  1295
        # consecutive non selectable lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1296
        # 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
  1297
        miny = min(0, self.firstlineofpadtoprint - self.yscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1298
        maxy = self.firstlineofpadtoprint + self.yscreensize * 2
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1299
        return y < miny or y > maxy
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1300
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1301
    def handleselection(self, item, recursechildren):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1302
        selected = (item is self.currentselecteditem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1303
        if selected and recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1304
            # assumes line numbering starting from line 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1305
            self.selecteditemstartline = self.linesprintedtopadsofar
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1306
            selecteditemlines = self.getnumlinesdisplayed(item,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1307
                                                          recursechildren=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1308
            self.selecteditemendline = (self.selecteditemstartline +
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1309
                                        selecteditemlines - 1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1310
        return selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1311
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1312
    def __printitem(self, item, ignorefolding, recursechildren, outstr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1313
                    towin=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1314
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1315
        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
  1316
        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
  1317
        patch (not including coloring, etc.).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1318
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1319
        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
  1320
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1321
        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
  1322
        child items.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1323
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1324
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1325
        if towin and self.outofdisplayedarea():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1326
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1327
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1328
        selected = self.handleselection(item, recursechildren)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1329
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1330
        # patch object is a list of headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1331
        if isinstance(item, patch):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1332
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1333
                for hdr in item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1334
                    self.__printitem(hdr, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1335
                            recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1336
        # todo: eliminate all isinstance() calls
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1337
        if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1338
            outstr.append(self.printheader(item, selected, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1339
                                       ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1340
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1341
                for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1342
                    self.__printitem(hnk, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1343
                            recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1344
        elif (isinstance(item, uihunk) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1345
              ((not item.header.folded) or ignorefolding)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1346
            # print the hunk data which comes before the changed-lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1347
            outstr.append(self.printhunklinesbefore(item, selected, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1348
                                                ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1349
            if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1350
                for l in item.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1351
                    self.__printitem(l, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1352
                            recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1353
                outstr.append(self.printhunklinesafter(item, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1354
                                                ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1355
        elif (isinstance(item, uihunkline) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1356
              ((not item.hunk.folded) or ignorefolding)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1357
            outstr.append(self.printhunkchangedline(item, selected,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1358
                towin=towin))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1359
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1360
        return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1361
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1362
    def getnumlinesdisplayed(self, item=None, ignorefolding=False,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1363
                             recursechildren=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1364
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1365
        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
  1366
        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
  1367
        display (pad).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1368
        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
  1369
        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
  1370
        the number of lines.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1371
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1372
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1373
        # temporarily disable printing to windows by printstring
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1374
        patchdisplaystring = self.printitem(item, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1375
                                            recursechildren, towin=False)
36339
3496bffe266d py3: make sure we are doing integer division by using '//'
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36338
diff changeset
  1376
        numlines = len(patchdisplaystring) // self.xscreensize
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1377
        return numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1378
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1379
    def sigwinchhandler(self, n, frame):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1380
        "handle window resizing"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1381
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1382
            curses.endwin()
30328
0911191dc4c9 crecord: use scmutil.termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 29961
diff changeset
  1383
            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
  1384
            self.statuswin.resize(self.numstatuslines, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1385
            self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1386
            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
  1387
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1388
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1389
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1390
    def getcolorpair(self, fgcolor=None, bgcolor=None, name=None,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1391
                     attrlist=None):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1392
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1393
        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
  1394
        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
  1395
        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
  1396
        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
  1397
        returned.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1398
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1399
        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
  1400
        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
  1401
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1402
        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
  1403
        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
  1404
        curses.A_BOLD.
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1405
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1406
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1407
        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
  1408
            # 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
  1409
            colorpair = self.colorpairnames[name]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1410
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1411
            if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1412
                fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1413
            if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1414
                bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1415
            if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1416
                colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1417
            else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1418
                pairindex = len(self.colorpairs) + 1
35531
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1419
                if self.usecolor:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1420
                    curses.init_pair(pairindex, fgcolor, bgcolor)
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1421
                    colorpair = self.colorpairs[(fgcolor, bgcolor)] = (
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1422
                        curses.color_pair(pairindex))
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1423
                    if name is not None:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1424
                        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
  1425
                else:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1426
                    cval = 0
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1427
                    if name is not None:
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1428
                        if name == 'selected':
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1429
                            cval = curses.A_REVERSE
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1430
                        self.colorpairnames[name] = cval
f43dc62cfe11 crecord: honor "ui.color = no" config option
Elmar Bartel <elb_hg@leo.org>
parents: 35091
diff changeset
  1431
                    colorpair = self.colorpairs[(fgcolor, bgcolor)] = cval
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1432
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1433
        # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1434
        if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1435
            attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1436
        if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1437
            # 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
  1438
            for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1439
                colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1440
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1441
            # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1442
            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
  1443
                if textattrib in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1444
                    colorpair |= textattrib
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1445
        return colorpair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1446
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1447
    def initcolorpair(self, *args, **kwargs):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1448
        "same as getcolorpair."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1449
        self.getcolorpair(*args, **kwargs)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1450
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1451
    def helpwindow(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1452
        "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
  1453
        helptext = _(
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1454
            """            [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
  1455
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1456
crecord allows you to interactively choose among the changes you have made,
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1457
and confirm only those changes you select for further processing by the command
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1458
you are running (commit/shelve/revert), after confirming the selected
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1459
changes, the unselected changes are still present in your working copy, so you
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1460
can use crecord multiple times to split large changes into smaller changesets.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1461
the following are valid keystrokes:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1462
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1463
                [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
  1464
                [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
  1465
                      A : (un-)select all items
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1466
    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
  1467
        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
  1468
 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
  1469
 shift-left-arrow   [H] : go to parent header / fold selected header
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1470
                      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
  1471
                      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
  1472
                 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
  1473
                      m : edit / resume editing the commit message
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1474
                      e : edit the currently selected hunk
29077
6ecd1a00977e crecord: drop the version condition for amend
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29076
diff changeset
  1475
                      a : toggle amend mode, only with commit -i
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1476
                      c : confirm selected changes
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1477
                      r : review/edit and confirm selected changes
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1478
                      q : quit without confirming (no changes will be made)
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1479
                      ? : help (what you're currently reading)""")
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1480
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1481
        helpwin = curses.newwin(self.yscreensize, 0, 0, 0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1482
        helplines = helptext.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1483
        helplines = helplines + [" "]*(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1484
            self.yscreensize - self.numstatuslines - len(helplines) - 1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1485
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1486
            for line in helplines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1487
                self.printstring(helpwin, line, pairname="legend")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1488
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1489
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1490
        helpwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1491
        try:
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1492
            with self.ui.timeblockedsection('crecord'):
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1493
                helpwin.getkey()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1494
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1495
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1496
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
  1497
    def commitMessageWindow(self):
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
  1498
        "Create a temporary commit message editing window on the screen."
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
  1499
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
  1500
        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
  1501
        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
  1502
        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
  1503
        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
  1504
        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
  1505
        self.stdscr.refresh()
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
  1506
        self.stdscr.keypad(1) # allow arrow-keys to continue to function
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
  1507
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1508
    def confirmationwindow(self, windowtext):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1509
        "display an informational window, then wait for and return a keypress."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1510
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1511
        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
  1512
        try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1513
            lines = windowtext.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1514
            for line in lines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1515
                self.printstring(confirmwin, line, pairname="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1516
        except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1517
            pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1518
        self.stdscr.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1519
        confirmwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1520
        try:
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1521
            with self.ui.timeblockedsection('crecord'):
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1522
                response = chr(self.stdscr.getch())
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1523
        except ValueError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1524
            response = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1525
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1526
        return response
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1527
28926
0411b7998d9b crecord: cleanup the remains of commit confirmation
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28925
diff changeset
  1528
    def reviewcommit(self):
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1529
        """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
  1530
        confirmed."""
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1531
        confirmtext = _(
41944
7e95ade0f369 chunkselector: fix typos in instructions when user reviews patch
Kyle Lippincott <spectral@google.com>
parents: 41848
diff changeset
  1532
"""If you answer yes to the following, your currently chosen patch chunks
7e95ade0f369 chunkselector: fix typos in instructions when user reviews patch
Kyle Lippincott <spectral@google.com>
parents: 41848
diff changeset
  1533
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
  1534
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
  1535
saving.
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1536
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1537
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
  1538
      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
  1539
24840
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1540
are you sure you want to review/edit and confirm the selected changes [yn]?
a5e3634ba024 record: change wording for record curses interface
Laurent Charignon <lcharignon@fb.com>
parents: 24779
diff changeset
  1541
""")
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1542
        with self.ui.timeblockedsection('crecord'):
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1543
            response = self.confirmationwindow(confirmtext)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1544
        if response is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1545
            response = "n"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1546
        if response.lower().startswith("y"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1547
            return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1548
        else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1549
            return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1550
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1551
    def toggleamend(self, opts, test):
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1552
        """Toggle the amend flag.
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1553
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1554
        When the amend flag is set, a commit will modify the most recently
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1555
        committed changeset, instead of creating a new changeset.  Otherwise, a
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1556
        new changeset will be created (the normal commit behavior).
28579
f571ea254f75 crecord: clean up empty lines at ends of docblocks
Ryan McElroy <rmcelroy@fb.com>
parents: 28543
diff changeset
  1557
        """
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1558
41711
1f44bfab0fff crecord: remove obsolete version check
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 41634
diff changeset
  1559
        if opts.get('amend') is None:
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1560
            opts['amend'] = True
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1561
            msg = _("Amend option is turned on -- committing the currently "
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1562
                    "selected changes will not create a new changeset, but "
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1563
                    "instead update the most recently committed changeset.\n\n"
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1564
                    "Press any key to continue.")
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1565
        elif opts.get('amend') is True:
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1566
            opts['amend'] = None
30544
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1567
            msg = _("Amend option is turned off -- committing the currently "
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1568
                    "selected changes will create a new changeset.\n\n"
3899c358b45a crecord: filter text via i18n
Jun Wu <quark@fb.com>
parents: 30542
diff changeset
  1569
                    "Press any key to continue.")
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1570
        if not test:
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1571
            self.confirmationwindow(msg)
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1572
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1573
    def recenterdisplayedarea(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1574
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1575
        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
  1576
        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
  1577
        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
  1578
        zone and then update the scroll.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1579
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1580
        self.printitem(towin=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1581
        self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1582
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1583
    def toggleedit(self, item=None, test=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1584
        """
28580
8b41ad798fb7 crecord: fix docblock indentation
Ryan McElroy <rmcelroy@fb.com>
parents: 28579
diff changeset
  1585
        edit the currently selected chunk
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1586
        """
25555
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1587
        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
  1588
            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
  1589
            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
  1590
            self.updatescroll()
838fa1932ff8 crecord: extract ui refresh code after editing of hunk in a function
Laurent Charignon <lcharignon@fb.com>
parents: 25461
diff changeset
  1591
            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
  1592
            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
  1593
            self.stdscr.keypad(1)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1594
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1595
        def editpatchwitheditor(self, chunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1596
            if chunk is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1597
                self.ui.write(_('cannot edit patch for whole file'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1598
                self.ui.write("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1599
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1600
            if chunk.header.binary():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1601
                self.ui.write(_('cannot edit patch for binary file'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1602
                self.ui.write("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1603
                return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1604
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1605
            # write the initial patch
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
  1606
            patch = stringio()
28637
4874b8efe7d2 crecord: break out the help message for editing hunks
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28636
diff changeset
  1607
            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
  1608
            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
  1609
            chunk.write(patch)
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1610
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1611
            # 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
  1612
            try:
34046
6e6452bc441d editor: use an unambiguous path suffix for editor files
Michael Bolin <mbolin@fb.com>
parents: 33991
diff changeset
  1613
                patch = self.ui.edit(patch.getvalue(), "", action="diff")
28636
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1614
            except error.Abort as exc:
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1615
                self.errorstr = str(exc)
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1616
                return None
41991
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1617
            finally:
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1618
                self.stdscr.clear()
3f467db023a2 crecord: completely redraw screen when coming back from editor
Kyle Lippincott <spectral@google.com>
parents: 41987
diff changeset
  1619
                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
  1620
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1621
            # remove comment lines
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1622
            patch = [line + '\n' for line in patch.splitlines()
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1623
                     if not line.startswith('#')]
de64020bb4ec crecord: refactor hunk edit action to use ui.edit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28581
diff changeset
  1624
            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
  1625
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1626
        if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1627
            item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1628
        if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1629
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1630
        if isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1631
            item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1632
        if not isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1633
            return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1634
27914
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1635
        # 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
  1636
        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
  1637
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1638
        beforeadded, beforeremoved = item.added, item.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1639
        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
  1640
        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
  1641
            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
  1642
                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
  1643
            return
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1644
        header = item.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1645
        editedhunkindex = header.hunks.index(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1646
        hunksbefore = header.hunks[:editedhunkindex]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1647
        hunksafter = header.hunks[editedhunkindex + 1:]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1648
        newpatchheader = newpatches[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1649
        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
  1650
        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
  1651
        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
  1652
        offset = (newadded - beforeadded) - (newremoved - beforeremoved)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1653
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1654
        for h in hunksafter:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1655
            h.toline += offset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1656
        for h in newhunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1657
            h.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1658
        header.hunks = hunksbefore + newhunks + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1659
        if self.emptypatch():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1660
            header.hunks = hunksbefore + [item] + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1661
        self.currentselecteditem = header
27914
505a10b504ed crecord: edit during hg crecord should preserve cursor position (issue5041)
Laurent Charignon <lcharignon@fb.com>
parents: 27533
diff changeset
  1662
        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
  1663
            self.currentselecteditem = header.hunks[itemindex]
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1664
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1665
        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
  1666
            updateui(self)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1667
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1668
    def emptypatch(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1669
        item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1670
        if not item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1671
            return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1672
        for header in item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1673
            if header.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1674
                return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1675
        return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1676
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1677
    def handlekeypressed(self, keypressed, test=False):
28581
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1678
        """
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1679
        Perform actions based on pressed keys.
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1680
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1681
        Return true to exit the main loop.
3c8f0a605504 crecord: add docblock to handlekeypressed
Ryan McElroy <rmcelroy@fb.com>
parents: 28580
diff changeset
  1682
        """
41848
25420df87903 py3: convert KEY_PRESSED value to bytes in crecord.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41842
diff changeset
  1683
        keypressed = pycompat.bytestr(keypressed)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1684
        if keypressed in ["k", "KEY_UP"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1685
            self.uparrowevent()
25460
bd4bcfa48c9e crecord: fix three typos introduced while moving crecord into core
Laurent Charignon <lcharignon@fb.com>
parents: 25447
diff changeset
  1686
        if keypressed in ["K", "KEY_PPAGE"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1687
            self.uparrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1688
        elif keypressed in ["j", "KEY_DOWN"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1689
            self.downarrowevent()
25460
bd4bcfa48c9e crecord: fix three typos introduced while moving crecord into core
Laurent Charignon <lcharignon@fb.com>
parents: 25447
diff changeset
  1690
        elif keypressed in ["J", "KEY_NPAGE"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1691
            self.downarrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1692
        elif keypressed in ["l", "KEY_RIGHT"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1693
            self.rightarrowevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1694
        elif keypressed in ["h", "KEY_LEFT"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1695
            self.leftarrowevent()
25460
bd4bcfa48c9e crecord: fix three typos introduced while moving crecord into core
Laurent Charignon <lcharignon@fb.com>
parents: 25447
diff changeset
  1696
        elif keypressed in ["H", "KEY_SLEFT"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1697
            self.leftarrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1698
        elif keypressed in ["q"]:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26421
diff changeset
  1699
            raise error.Abort(_('user quit'))
27156
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1700
        elif keypressed in ['a']:
55fa7c3900ae commit: add amend mode for commit -i
Laurent Charignon <lcharignon@fb.com>
parents: 27155
diff changeset
  1701
            self.toggleamend(self.opts, test)
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1702
        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
  1703
            return True
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
  1704
        elif test and keypressed in ['X']:
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
  1705
            return True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1706
        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
  1707
            if self.reviewcommit():
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
  1708
                self.opts['review'] = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1709
                return True
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
  1710
        elif test and keypressed in ['R']:
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28637
diff changeset
  1711
            self.opts['review'] = True
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1712
            return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1713
        elif keypressed in [' '] or (test and keypressed in ["TOGGLE"]):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1714
            self.toggleapply()
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
  1715
        elif keypressed in ['\n', 'KEY_ENTER']:
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
  1716
            self.toggleapply()
682f73fa924a crecord: make enter move cursor down to the next item of the same type
Anton Shestakov <av6@dwimlabs.net>
parents: 38431
diff changeset
  1717
            self.nextsametype()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1718
        elif keypressed in ['A']:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1719
            self.toggleall()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1720
        elif keypressed in ['e']:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1721
            self.toggleedit(test=test)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1722
        elif keypressed in ["f"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1723
            self.togglefolded()
25447
093d38165e5a crecord: fix a typo introduced when moving crecord to core
Laurent Charignon <lcharignon@fb.com>
parents: 24840
diff changeset
  1724
        elif keypressed in ["F"]:
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1725
            self.togglefolded(foldparent=True)
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
  1726
        elif keypressed in ["m"]:
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
  1727
            self.commitMessageWindow()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1728
        elif keypressed in ["?"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1729
            self.helpwindow()
25419
6e62a5b3442d crecord: fix blue artifact bug coming back from help screen
Laurent Charignon <lcharignon@fb.com>
parents: 25359
diff changeset
  1730
            self.stdscr.clear()
6e62a5b3442d crecord: fix blue artifact bug coming back from help screen
Laurent Charignon <lcharignon@fb.com>
parents: 25359
diff changeset
  1731
            self.stdscr.refresh()
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
  1732
        elif curses.unctrl(keypressed) in ["^L"]:
41992
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1733
            # 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
  1734
            # 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
  1735
            self.scrolllines(self.selecteditemstartline)
41992
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1736
            self.stdscr.clear()
fa3b0ca9d74f crecord: redraw the screen on ctrl-L
Kyle Lippincott <spectral@google.com>
parents: 41991
diff changeset
  1737
            self.stdscr.refresh()
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1738
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1739
    def main(self, stdscr):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1740
        """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1741
        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
  1742
        """
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1743
31931
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1744
        origsigwinch = sentinel = object()
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1745
        if util.safehasattr(signal, 'SIGWINCH'):
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1746
            origsigwinch = signal.signal(signal.SIGWINCH,
0130c3e1b1d9 crecord: avoid setting non-existing signal SIGWINCH on windows
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31930
diff changeset
  1747
                                         self.sigwinchhandler)
31930
7e7743a01103 crecord: ensure we reinstall the SIGWINCH handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31929
diff changeset
  1748
        try:
7e7743a01103 crecord: ensure we reinstall the SIGWINCH handler
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31929
diff changeset
  1749
            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
  1750
        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
  1751
            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
  1752
                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
  1753
bf6b44da1d8e crecord: extract most of 'main' into a sub function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31654
diff changeset
  1754
    def _main(self, stdscr):
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1755
        self.stdscr = stdscr
25820
701d8c362aa2 crecord: add error reporting for failure in curses interface initialization
Laurent Charignon <lcharignon@fb.com>
parents: 25807
diff changeset
  1756
        # 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
  1757
        # 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
  1758
        self.initexc = None
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1759
        self.yscreensize, self.xscreensize = self.stdscr.getmaxyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1760
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1761
        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
  1762
        try:
fb2e59e92651 crecord: fallback to color = no when curses.use_default_colors() fails
Elmar Bartel <elb_hg@leo.org>
parents: 35531
diff changeset
  1763
            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
  1764
        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
  1765
            self.usecolor = False
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1766
41993
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1767
        # 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
  1768
        # 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
  1769
        # 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
  1770
        # fit on the terminal.
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1771
        self.stdscr.clear()
cde5827d09a7 crecord: redraw the screen when starting up chunkselector
Kyle Lippincott <spectral@google.com>
parents: 41992
diff changeset
  1772
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1773
        # 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
  1774
        # init_pair(color_id, foreground_color, background_color)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1775
        self.initcolorpair(None, None, name="normal")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1776
        self.initcolorpair(curses.COLOR_WHITE, curses.COLOR_MAGENTA,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1777
                           name="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1778
        self.initcolorpair(curses.COLOR_RED, None, name="deletion")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1779
        self.initcolorpair(curses.COLOR_GREEN, None, name="addition")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1780
        self.initcolorpair(curses.COLOR_WHITE, curses.COLOR_BLUE, name="legend")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1781
        # newwin([height, width,] begin_y, begin_x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1782
        self.statuswin = curses.newwin(self.numstatuslines, 0, 0, 0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1783
        self.statuswin.keypad(1) # interpret arrow-key, etc. esc sequences
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1784
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1785
        # 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
  1786
        # used for displaying the patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1787
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1788
        # stupid hack to prevent getnumlinesdisplayed from failing
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1789
        self.chunkpad = curses.newpad(1, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1790
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1791
        # 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
  1792
        self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1793
25821
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  1794
        try:
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  1795
            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
  1796
        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
  1797
            self.initexc = fallbackerror(
dabc2237963c crecord: fallback to text mode if diffs are too big for curses mode
Kyle Lippincott <spectral@google.com>
parents: 37087
diff changeset
  1798
                _('this diff is too large to be displayed'))
25821
d68544b69736 crecord: throws error instead of crashing for large diffs
Laurent Charignon <lcharignon@fb.com>
parents: 25820
diff changeset
  1799
            return
30342
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30328
diff changeset
  1800
        # 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
  1801
        self.selecteditemendline = self.getnumlinesdisplayed(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1802
            self.currentselecteditem, recursechildren=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1803
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1804
        while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1805
            self.updatescreen()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1806
            try:
31001
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1807
                with self.ui.timeblockedsection('crecord'):
a0c4e57074fe crecord: log blocked time waiting for curses input
Simon Farnsworth <simonfar@fb.com>
parents: 30797
diff changeset
  1808
                    keypressed = self.statuswin.getkey()
25556
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  1809
                if self.errorstr is not None:
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  1810
                    self.errorstr = None
40f0e9e5b821 crecord: add mechanism for error reporting
Laurent Charignon <lcharignon@fb.com>
parents: 25555
diff changeset
  1811
                    continue
24310
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1812
            except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1813
                keypressed = "foobar"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1814
            if self.handlekeypressed(keypressed):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
  1815
                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
  1816
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
  1817
        if self.commenttext != "":
41549
7a90ff8cd14c crecord: use raw string for regular expression
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41439
diff changeset
  1818
            whitespaceremoved = re.sub(br"(?m)^\s.*(\n|$)", b"",
7a90ff8cd14c crecord: use raw string for regular expression
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41439
diff changeset
  1819
                                       self.commenttext)
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
  1820
            if whitespaceremoved != "":
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
  1821
                self.opts['message'] = self.commenttext