annotate mercurial/crecord.py @ 43076:2372284d9457

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