annotate mercurial/crecord.py @ 24310:6409fb6c934d

record: add crecord's ui logic to core Code adapted from https://bitbucket.org/edgimar/crecord/src to respect coding convention an record's interface
author Laurent Charignon <lcharignon@fb.com>
date Thu, 12 Mar 2015 14:19:11 -0700
parents
children ed535f2c15c3
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
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
11 from mercurial.i18n import _
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
12
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
13 from mercurial import patch as patchmod
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
14 from mercurial import util
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
15 from mercurial import demandimport
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
16 demandimport.ignore.append('mercurial.encoding')
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
17 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
18 import mercurial.encoding as encoding
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
19 code = encoding.encoding
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
20 except ImportError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
21 encoding = util
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
22 code = encoding._encoding
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
23
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
24 import os
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
25 import re
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
26 import sys
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
27 import fcntl
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
28 import struct
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
29 import termios
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
30 import signal
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
31 import tempfile
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
32 import locale
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
33 import cStringIO
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
34 # This is required for ncurses to display non-ASCII characters in default user
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
35 # locale encoding correctly. --immerrr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
36 locale.setlocale(locale.LC_ALL, '')
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
37 # os.name is one of: 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce'
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
38 if os.name == 'posix':
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
39 import curses
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
40 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
41 # I have no idea if wcurses works with crecord...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
42 import wcurses as curses
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
43
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
44 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
45 curses
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
46 except NameError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
47 raise util.Abort(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
48 _('the python curses/wcurses module is not available/installed'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
49
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
50
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
51 orig_stdout = sys.__stdout__ # used by gethw()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
52
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
53
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
54
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
55 class patchnode(object):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
56 """abstract class for patch graph nodes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
57 (i.e. patchroot, header, hunk, hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
58 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
59
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
60 def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
61 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
62
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
63 def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
64 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
65
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
66 def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
67 "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
68 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
69 def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
70 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
71 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
72 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
73 If no such item exists, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
74
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
75 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
76 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
77
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
78 def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
79 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
80 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
81 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
82 If no such item exists, return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
83
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
84 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
85 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
86
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
87 def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
88 raise NotImplementedError("method must be implemented by subclass")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
89
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
90
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
91 def nextitem(self, constrainlevel=True, skipfolded=True):
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 If constrainLevel == True, return the closest next item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
94 of the same type where there are no items of different types between
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
95 the current item and this closest item.
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 If constrainLevel == False, then try to return the next item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
98 closest to this item, regardless of item's type (header, hunk, or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
99 HunkLine).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
100
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
101 If skipFolded == True, and the current item is folded, then the child
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
102 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
103 the next item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
104
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
105 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
106
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
107 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
108 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
109 itemfolded = self.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
110 except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
111 itemfolded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
112 if constrainlevel:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
113 return self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
114 elif skipfolded and itemfolded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
115 nextitem = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
116 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
117 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
118 nextitem = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
119 except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
120 nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
121 return nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
122 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
123 # try child
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
124 item = self.firstchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
125 if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
126 return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
127
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
128 # else try next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
129 item = self.nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
130 if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
131 return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
132
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
133 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
134 # else try parent's next sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
135 item = self.parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
136 if item is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
137 return item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
138
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
139 # 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
140 return self.parentitem().parentitem().nextsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
141
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
142 except AttributeError: # parent and/or grandparent was None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
143 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
144
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
145 def previtem(self, constrainlevel=True, skipfolded=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
146 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
147 If constrainLevel == True, return the closest previous item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
148 of the same type where there are no items of different types between
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
149 the current item and this closest item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
150
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
151 If constrainLevel == False, then try to return the previous item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
152 closest to this item, regardless of item's type (header, hunk, or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
153 HunkLine).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
154
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
155 If skipFolded == True, and the current item is folded, then the items
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
156 that are hidden due to folding will be skipped when determining the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
157 next item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
158
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
159 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
160
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
161 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
162 if constrainlevel:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
163 return self.prevsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
164 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
165 # try previous sibling's last child's last child,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
166 # else try previous sibling's last child, else try previous sibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
167 prevsibling = self.prevsibling()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
168 if prevsibling is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
169 prevsiblinglastchild = prevsibling.lastchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
170 if ((prevsiblinglastchild is not None) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
171 not prevsibling.folded):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
172 prevsiblinglclc = prevsiblinglastchild.lastchild()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
173 if ((prevsiblinglclc is not None) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
174 not prevsiblinglastchild.folded):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
175 return prevsiblinglclc
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
176 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
177 return prevsiblinglastchild
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
178 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
179 return prevsibling
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
180
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
181 # try parent (or None)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
182 return self.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
183
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
184 class patch(patchnode, list): # todo: rename patchroot
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
185 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
186 list of header objects representing the patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
187
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
188 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
189 def __init__(self, headerlist):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
190 self.extend(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
191 # 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
192 for header in self:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
193 header.patch = self
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
194
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
195 class uiheader(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
196 """patch header
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 xxx shoudn't we move this to mercurial/patch.py ?
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
199 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
200
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
201 def __init__(self, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
202 self.nonuiheader = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
203 # 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
204 self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
205 # 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
206 # 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
207 self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
208
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
209 # 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
210 self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
211
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
212 # list of all headers in patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
213 self.patch = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
214
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
215 # 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
216 self.neverunfolded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
217 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
218
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
219
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
220 def prettystr(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
221 x = cStringIO.StringIO()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
222 self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
223 return x.getvalue()
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 def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
226 numheadersinpatch = len(self.patch)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
227 indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
228
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
229 if indexofthisheader < numheadersinpatch - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
230 nextheader = self.patch[indexofthisheader + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
231 return nextheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
232 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
233 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
234
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
235 def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
236 indexofthisheader = self.patch.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
237 if indexofthisheader > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
238 previousheader = self.patch[indexofthisheader - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
239 return previousheader
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
240 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
241 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
242
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
243 def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
244 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
245 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
246 so return None.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
247 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
248 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
249
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
250 def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
251 "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
252 if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
253 return self.hunks[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
254 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
255 return None
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 def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
258 "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
259 if len(self.hunks) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
260 return self.hunks[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
261 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
262 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
263
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
264 def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
265 "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
266 return self.hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
267
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
268 def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
269 return getattr(self.nonuiheader, name)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
270
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
271 class uihunkline(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
272 "represents a changed line in a hunk"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
273 def __init__(self, linetext, hunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
274 self.linetext = linetext
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
275 self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
276 # 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
277 self.hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
278 # 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
279 # in the previtem method.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
280 self.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
281
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
282 def prettystr(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
283 return self.linetext
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
284
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
285 def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
286 numlinesinhunk = len(self.hunk.changedlines)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
287 indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
288
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
289 if (indexofthisline < numlinesinhunk - 1):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
290 nextline = self.hunk.changedlines[indexofthisline + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
291 return nextline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
292 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
293 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
294
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
295 def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
296 indexofthisline = self.hunk.changedlines.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
297 if indexofthisline > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
298 previousline = self.hunk.changedlines[indexofthisline - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
299 return previousline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
300 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
301 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
302
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
303 def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
304 "return the parent to the current item"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
305 return self.hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
306
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
307 def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
308 "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
309 # hunk-lines don't have children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
310 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
311
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
312 def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
313 "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
314 # hunk-lines don't have children
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 class uihunk(patchnode):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
318 """ui patch hunk, wraps a hunk and keep track of ui behavior """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
319 maxcontext = 3
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 __init__(self, hunk, header):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
322 self._hunk = hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
323 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
324 self.header = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
325 # 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
326 self.originalremoved = self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
327
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
328 # 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
329 self.folded = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
330 # 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
331 self.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
332 # 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
333 # 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
334 self.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
335
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
336 def nextsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
337 numhunksinheader = len(self.header.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
338 indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
339
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
340 if (indexofthishunk < numhunksinheader - 1):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
341 nexthunk = self.header.hunks[indexofthishunk + 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
342 return nexthunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
343 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
344 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
345
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
346 def prevsibling(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
347 indexofthishunk = self.header.hunks.index(self)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
348 if indexofthishunk > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
349 previoushunk = self.header.hunks[indexofthishunk - 1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
350 return previoushunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
351 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
352 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
353
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
354 def parentitem(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
355 "return the parent to the current item"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
356 return self.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
357
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
358 def firstchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
359 "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
360 if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
361 return self.changedlines[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
362 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
363 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
364
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
365 def lastchild(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
366 "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
367 if len(self.changedlines) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
368 return self.changedlines[-1]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
369 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
370 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
371
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
372 def allchildren(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
373 "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
374 return self.changedlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
375 def countchanges(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
376 """changedlines -> (n+,n-)"""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
377 add = len([l for l in self.changedlines if l.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
378 and l.prettystr()[0] == '+'])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
379 rem = len([l for l in self.changedlines if l.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
380 and l.prettystr()[0] == '-'])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
381 return add, rem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
382
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
383 def getfromtoline(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
384 # 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
385 removedconvertedtocontext = self.originalremoved - self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
386
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
387 contextlen = (len(self.before) + len(self.after) +
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
388 removedconvertedtocontext)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
389 if self.after and self.after[-1] == '\\ no newline at end of file\n':
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
390 contextlen -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
391 fromlen = contextlen + self.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
392 tolen = contextlen + self.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
393
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
394 # 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
395 # 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
396 # precedes the hunk."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
397 #
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
398 # 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
399 # 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
400 fromline, toline = self.fromline, self.toline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
401 if fromline != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
402 if fromlen == 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
403 fromline -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
404 if tolen == 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
405 toline -= 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
406
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
407 fromtoline = '@@ -%d,%d +%d,%d @@%s\n' % (
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
408 fromline, fromlen, toline, tolen,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
409 self.proc and (' ' + self.proc))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
410 return fromtoline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
411
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
412 def write(self, fp):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
413 # 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
414 self.added, self.removed = self.countchanges()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
415 fp.write(self.getfromtoline())
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
416
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
417 hunklinelist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
418 # 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
419 # (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
420 for changedline in self.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
421 changedlinestr = changedline.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
422 if changedline.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
423 hunklinelist.append(changedlinestr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
424 elif changedlinestr[0] == "-":
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
425 hunklinelist.append(" " + changedlinestr[1:])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
426
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
427 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
428
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
429 pretty = write
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
430
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
431 def prettystr(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
432 x = cStringIO.StringIO()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
433 self.pretty(x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
434 return x.getvalue()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
435
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
436 def __getattr__(self, name):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
437 return getattr(self._hunk, name)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
438 def __repr__(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
439 return '<hunk %r@%d>' % (self.filename(), self.fromline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
440
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
441 def filterpatch(ui, chunks, chunk_selector):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
442 """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
443
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
444 chunks = list(chunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
445 # 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
446 # 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
447 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
448
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
449 # if there are no changed files
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
450 if len(headers) == 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
451 return []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
452 uiheaders = [uiheader(h) for h in headers]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
453 # 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
454 # accordingly
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
455 chunk_selector(uiheaders, ui)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
456 appliedhunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
457 for hdr in uiheaders:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
458 if (hdr.applied and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
459 (hdr.special() or len([h for h in hdr.hunks if h.applied]) > 0)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
460 appliedhunklist.append(hdr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
461 fixoffset = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
462 for hnk in hdr.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
463 if hnk.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
464 appliedhunklist.append(hnk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
465 # 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
466 # 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
467 if fixoffset:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
468 #hnk = copy.copy(hnk) # necessary??
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
469 hnk.toline += fixoffset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
470 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
471 fixoffset += hnk.removed - hnk.added
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
472
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
473 return appliedhunklist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
474
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
475
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
476
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
477 def gethw():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
478 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
479 magically get the current height and width of the window (without initscr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
480
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
481 this is a rip-off of a rip-off - taken from the bpython code. it is
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
482 useful / necessary because otherwise curses.initscr() must be called,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
483 which can leave the terminal in a nasty state after exiting.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
484
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
485 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
486 h, w = struct.unpack(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
487 "hhhh", fcntl.ioctl(orig_stdout, termios.TIOCGWINSZ, "\000"*8))[0:2]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
488 return h, w
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
489
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
490
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
491 def chunkselector(headerlist, ui):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
492 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
493 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
494 of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
495
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
496 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
497 chunkselector = curseschunkselector(headerlist, ui)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
498 curses.wrapper(chunkselector.main)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
499
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
500 def testdecorator(testfn, f):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
501 def u(*args, **kwargs):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
502 return f(testfn, *args, **kwargs)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
503 return u
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
504
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
505 def testchunkselector(testfn, headerlist, ui):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
506 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
507 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
508 of the chosen chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
509
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
510 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
511 chunkselector = curseschunkselector(headerlist, ui)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
512 if testfn and os.path.exists(testfn):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
513 testf = open(testfn)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
514 testcommands = map(lambda x: x.rstrip('\n'), testf.readlines())
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
515 testf.close()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
516 while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
517 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
518 break
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
519
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
520 class curseschunkselector(object):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
521 def __init__(self, headerlist, ui):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
522 # put the headers into a patch object
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
523 self.headerlist = patch(headerlist)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
524
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
525 self.ui = ui
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
526
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
527 # list of all chunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
528 self.chunklist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
529 for h in headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
530 self.chunklist.append(h)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
531 self.chunklist.extend(h.hunks)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
532
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
533 # dictionary mapping (fgcolor, bgcolor) pairs to the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
534 # corresponding curses color-pair value.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
535 self.colorpairs = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
536 # 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
537 self.colorpairnames = {}
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
538
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
539 # 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
540 self.currentselecteditem = self.headerlist[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
541
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
542 # 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
543 # 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
544 self.selecteditemstartline = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
545 self.selecteditemendline = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
546
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
547 # define indentation levels
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
548 self.headerindentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
549 self.hunkindentnumchars = 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
550 self.hunklineindentnumchars = 6
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
551
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
552 # 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
553 self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
554
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
555 # 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
556 self.numpadlines = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
557
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
558 self.numstatuslines = 2
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
559
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
560 # 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
561 # (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
562 self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
563
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
564 # 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
565 self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
566
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
567 # 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
568 self.commenttext = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
569
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
570 # 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
571 self.waslasttoggleallapplied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
572
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
573 def uparrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
574 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
575 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
576 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
577 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
578 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
579 hunk itself.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
580
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
581 if the currently selected item is already at the top of the screen,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
582 scroll the screen down to show the new-selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
583
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
584 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
585 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
586
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
587 nextitem = currentitem.previtem(constrainlevel=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
588
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
589 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
590 # 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
591 # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
592 nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
593
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
594 self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
595
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
596 def uparrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
597 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
598 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
599 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
600 parent-item of the currently selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
601
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
602 if the currently selected item is already at the top of the screen,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
603 scroll the screen down to show the new-selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
604
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
605 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
606 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
607 nextitem = currentitem.previtem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
608 # if there's no previous item on this level, try choosing the parent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
609 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
610 nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
611 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
612 # 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
613 # no change...
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
614 nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
615
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
616 self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
617
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
618 def downarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
619 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
620 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
621 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
622 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
623 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
624 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
625
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
626 if the currently selected item is already at the bottom of the screen,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
627 scroll the screen up to show the new-selected item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
628
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
629 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
630 #self.startprintline += 1 #debug
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
631 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
632
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
633 nextitem = currentitem.nextitem(constrainlevel=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
634 # 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
635 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
636 nextitem = currentitem
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 self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
639
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
640 def downarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
641 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
642 if the cursor is already at the bottom chunk, scroll the screen up and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
643 move the cursor-position to the subsequent chunk. otherwise, only move
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
644 the cursor position down one chunk.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
645
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
646 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
647 # todo: update docstring
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
648
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
649 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
650 nextitem = currentitem.nextitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
651 # if there's no previous item on this level, try choosing the parent's
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
652 # nextitem.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
653 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
654 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
655 nextitem = currentitem.parentitem().nextitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
656 except AttributeError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
657 # parentitem returned None, so nextitem() can't be called
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
658 nextitem = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
659 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
660 # 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
661 nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
662
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
663 self.currentselecteditem = nextitem
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 def rightarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
666 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
667 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
668
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
669 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
670 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
671 nextitem = currentitem.firstchild()
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 # 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
674 if currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
675 self.togglefolded(currentitem)
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 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
678 # 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
679 nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
680
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
681 self.currentselecteditem = nextitem
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 def leftarrowevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
684 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
685 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
686 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
687 of this item.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
688
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
689 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
690 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
691
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
692 # try to fold the item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
693 if not isinstance(currentitem, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
694 if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
695 self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
696 return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
697
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
698 # 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
699 nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
700
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
701 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
702 # 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
703 nextitem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
704 if not nextitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
705 self.togglefolded(item=nextitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
706
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
707 self.currentselecteditem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
708
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
709 def leftarrowshiftevent(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
710 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
711 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
712 current item is already a header).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
713
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
714 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
715 currentitem = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
716
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
717 if isinstance(currentitem, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
718 if not currentitem.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
719 self.togglefolded(item=currentitem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
720 return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
721
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
722 # select 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
723 while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
724 nextitem = currentitem.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
725 if nextitem is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
726 break
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
727 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
728 currentitem = nextitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
729
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
730 self.currentselecteditem = currentitem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
731
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
732 def updatescroll(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
733 "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
734 selstart = self.selecteditemstartline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
735 selend = self.selecteditemendline
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
736 #selnumlines = selend - selstart
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
737 padstart = self.firstlineofpadtoprint
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
738 padend = padstart + self.yscreensize - self.numstatuslines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
739 # '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
740 # top/bottom context margin
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
741 padstartbuffered = padstart + 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
742 padendbuffered = padend - 3
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
743
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
744 if selend > padendbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
745 self.scrolllines(selend - padendbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
746 elif selstart < padstartbuffered:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
747 # negative values scroll in pgup direction
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
748 self.scrolllines(selstart - padstartbuffered)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
749
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
750
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
751 def scrolllines(self, numlines):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
752 "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
753 self.firstlineofpadtoprint += numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
754 if self.firstlineofpadtoprint < 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
755 self.firstlineofpadtoprint = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
756 if self.firstlineofpadtoprint > self.numpadlines - 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
757 self.firstlineofpadtoprint = self.numpadlines - 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
758
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
759 def toggleapply(self, item=None):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
760 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
761 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
762 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
763
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
764 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
765 if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
766 item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
767
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
768 item.applied = not item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
769
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
770 if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
771 item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
772 if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
773 if not item.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
774 # apply all its hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
775 for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
776 hnk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
777 # apply all their hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
778 for hunkline in hnk.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
779 hunkline.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
780 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
781 # all children are off (but the header is on)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
782 if len(item.allchildren()) > 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
783 item.partial = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
784 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
785 # un-apply all its hunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
786 for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
787 hnk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
788 hnk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
789 # un-apply all their hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
790 for hunkline in hnk.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
791 hunkline.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
792 elif isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
793 item.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
794 # apply all it's hunklines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
795 for hunkline in item.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
796 hunkline.applied = item.applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
797
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
798 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
799 allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
800 nosiblingsapplied = not (True in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
801
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
802 siblingspartialstatus = [hnk.partial for hnk in item.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
803 somesiblingspartial = (True in siblingspartialstatus)
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 #cases where applied or partial should be removed from header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
806
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
807 # 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
808 if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
809 if not item.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
810 item.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
811 item.header.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
812 else: # some/all parent siblings are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
813 item.header.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
814 item.header.partial = (somesiblingspartial or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
815 not allsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
816
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
817 elif isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
818 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
819 allsiblingsapplied = not (False in siblingappliedstatus)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
820 nosiblingsapplied = not (True in siblingappliedstatus)
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 no 'sibling' lines are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
823 if nosiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
824 item.hunk.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
825 item.hunk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
826 elif allsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
827 item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
828 item.hunk.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
829 else: # some siblings applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
830 item.hunk.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
831 item.hunk.partial = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
832
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
833 parentsiblingsapplied = [hnk.applied for hnk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
834 in item.hunk.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
835 noparentsiblingsapplied = not (True in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
836 allparentsiblingsapplied = not (False in parentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
837
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
838 parentsiblingspartial = [hnk.partial for hnk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
839 in item.hunk.header.hunks]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
840 someparentsiblingspartial = (True in parentsiblingspartial)
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 # 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
843 if noparentsiblingsapplied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
844 if not item.hunk.header.special():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
845 item.hunk.header.applied = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
846 item.hunk.header.partial = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
847 # set the applied and partial status of the header if needed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
848 else: # some/all parent siblings are applied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
849 item.hunk.header.applied = True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
850 item.hunk.header.partial = (someparentsiblingspartial or
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
851 not allparentsiblingsapplied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
852
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
853 def toggleall(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
854 "toggle the applied flag of all items."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
855 if self.waslasttoggleallapplied: # then unapply them this time
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
856 for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
857 if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
858 self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
859 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
860 for item in self.headerlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
861 if not item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
862 self.toggleapply(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
863 self.waslasttoggleallapplied = not self.waslasttoggleallapplied
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
864
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
865 def togglefolded(self, item=None, foldparent=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
866 "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
867 if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
868 item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
869 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
870 if not isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
871 # 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
872 self.currentselecteditem = item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
873 elif item.neverunfolded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
874 item.neverunfolded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
875
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
876 # also fold any foldable children of the parent/current item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
877 if isinstance(item, uiheader): # the original or 'new' item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
878 for child in item.allchildren():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
879 child.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
880
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
881 if isinstance(item, (uiheader, uihunk)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
882 item.folded = not item.folded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
883
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
884
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
885 def alignstring(self, instr, window):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
886 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
887 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
888 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
889 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
890 multiple lines.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
891
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
892 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
893 y, xstart = window.getyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
894 width = self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
895 # turn tabs into spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
896 instr = instr.expandtabs(4)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
897 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
898 strlen = len(unicode(encoding.fromlocal(instr), code))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
899 except Exception:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
900 # if text is not utf8, then assume an 8-bit single-byte encoding.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
901 strlen = len(instr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
902
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
903 numspaces = (width - ((strlen + xstart) % width) - 1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
904 return instr + " " * numspaces + "\n"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
905
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
906 def printstring(self, window, text, fgcolor=None, bgcolor=None, pair=None,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
907 pairname=None, attrlist=None, towin=True, align=True, showwhtspc=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
908 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
909 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
910 the specified curses window object.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
911
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
912 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
913 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
914 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
915 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
916
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
917 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
918 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
919 underline].
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
920
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
921 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
922 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
923
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
924 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
925
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
926 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
927 # preprocess the text, converting tabs to spaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
928 text = text.expandtabs(4)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
929 # strip \n, and convert control characters to ^[char] representation
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
930 text = re.sub(r'[\x00-\x08\x0a-\x1f]',
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
931 lambda m:'^' + chr(ord(m.group()) + 64), text.strip('\n'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
932
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
933 if pair is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
934 colorpair = pair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
935 elif pairname is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
936 colorpair = self.colorpairnames[pairname]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
937 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
938 if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
939 fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
940 if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
941 bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
942 if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
943 colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
944 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
945 colorpair = self.getcolorpair(fgcolor, bgcolor)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
946 # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
947 if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
948 attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
949 if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
950 # 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
951 for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
952 colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
953 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
954 # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
955 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
956 if textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
957 colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
958
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
959 y, xstart = self.chunkpad.getyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
960 t = "" # variable for counting lines printed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
961 # if requested, show trailing whitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
962 if showwhtspc:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
963 origlen = len(text)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
964 text = text.rstrip(' \n') # tabs have already been expanded
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
965 strippedlen = len(text)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
966 numtrailingspaces = origlen - strippedlen
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
967
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
968 if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
969 window.addstr(text, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
970 t += text
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 if showwhtspc:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
973 wscolorpair = colorpair | curses.A_REVERSE
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
974 if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
975 for i in range(numtrailingspaces):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
976 window.addch(curses.ACS_CKBOARD, wscolorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
977 t += " " * numtrailingspaces
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
978
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
979 if align:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
980 if towin:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
981 extrawhitespace = self.alignstring("", window)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
982 window.addstr(extrawhitespace, colorpair)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
983 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
984 # 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
985 extrawhitespace = self.alignstring(t, window)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
986 t += extrawhitespace
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
987
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
988 # 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
989
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
990 linesprinted = (xstart + len(t)) / self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
991 self.linesprintedtopadsofar += linesprinted
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
992 return t
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
993
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
994 def updatescreen(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
995 self.statuswin.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
996 self.chunkpad.erase()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
997
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
998 printstring = self.printstring
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
999
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1000 # 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
1001 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1002 printstring(self.statuswin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1003 "SELECT CHUNKS: (j/k/up/dn/pgup/pgdn) move cursor; "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1004 "(space/A) toggle hunk/all; (e)dit hunk;",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1005 pairname="legend")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1006 printstring(self.statuswin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1007 " (f)old/unfold; (c)ommit applied; (q)uit; (?) help "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1008 "| [X]=hunk applied **=folded",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1009 pairname="legend")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1010 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1011 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1012
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1013 # 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
1014 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1015 self.printitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1016 self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1017 self.chunkpad.refresh(self.firstlineofpadtoprint, 0,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1018 self.numstatuslines, 0,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1019 self.yscreensize + 1 - self.numstatuslines,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1020 self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1021 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1022 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1023
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1024 # refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1025 self.statuswin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1026
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1027 def getstatusprefixstring(self, item):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1028 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1029 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
1030 is applied and/or folded.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1031
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1032 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1033 # create checkbox string
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1034 if item.applied:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1035 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
1036 checkbox = "[~]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1037 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1038 checkbox = "[x]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1039 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1040 checkbox = "[ ]"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1041
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1042 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1043 if item.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1044 checkbox += "**"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1045 if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1046 # 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
1047 filestatus = item.changetype
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1048
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1049 checkbox += filestatus + " "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1050 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1051 checkbox += " "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1052 if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1053 # add two more spaces for headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1054 checkbox += " "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1055 except AttributeError: # not foldable
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1056 checkbox += " "
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1057
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1058 return checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1059
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1060 def printheader(self, header, selected=False, towin=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1061 ignorefolding=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1062 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1063 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
1064 anything, but just count the number of lines which would be printed.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1065
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1066 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1067 outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1068 text = header.prettystr()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1069 chunkindex = self.chunklist.index(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1070
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1071 if chunkindex != 0 and not header.folded:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1072 # add separating line before headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1073 outstr += self.printstring(self.chunkpad, '_' * self.xscreensize,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1074 towin=towin, align=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1075 # select color-pair based on if the header is selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1076 colorpair = self.getcolorpair(name=selected and "selected" or "normal",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1077 attrlist=[curses.A_BOLD])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1078
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1079 # 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
1080
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1081 # 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
1082 indentnumchars = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1083 checkbox = self.getstatusprefixstring(header)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1084 if not header.folded or ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1085 textlist = text.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1086 linestr = checkbox + textlist[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1087 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1088 linestr = checkbox + header.filename()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1089 outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1090 towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1091 if not header.folded or ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1092 if len(textlist) > 1:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1093 for line in textlist[1:]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1094 linestr = " "*(indentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1095 outstr += self.printstring(self.chunkpad, linestr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1096 pair=colorpair, towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1097
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1098 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1099
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1100 def printhunklinesbefore(self, hunk, selected=False, towin=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1101 ignorefolding=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1102 "includes start/end line indicator"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1103 outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1104 # where hunk is in list of siblings
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1105 hunkindex = hunk.header.hunks.index(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1106
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1107 if hunkindex != 0:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1108 # add separating line before headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1109 outstr += self.printstring(self.chunkpad, ' '*self.xscreensize,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1110 towin=towin, align=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1111
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1112 colorpair = self.getcolorpair(name=selected and "selected" or "normal",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1113 attrlist=[curses.A_BOLD])
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1114
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1115 # print out from-to line with checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1116 checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1117
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1118 lineprefix = " "*self.hunkindentnumchars + checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1119 frtoline = " " + hunk.getfromtoline().strip("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1120
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1121
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1122 outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1123 align=False) # add uncolored checkbox/indent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1124 outstr += self.printstring(self.chunkpad, frtoline, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1125 towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1126
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1127 if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1128 # skip remainder of output
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1129 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1130
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1131 # 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
1132 for line in hunk.before:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1133 linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1134 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
1135
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1136 return outstr
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 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
1139 outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1140 if hunk.folded and not ignorefolding:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1141 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1142
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1143 # 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
1144 checkbox = self.getstatusprefixstring(hunk)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1145 for line in hunk.after:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1146 linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1147 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
1148
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1149 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1150
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1151 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
1152 outstr = ""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1153 checkbox = self.getstatusprefixstring(hunkline)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1154
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1155 linestr = hunkline.prettystr().strip("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1156
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1157 # 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
1158 if selected:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1159 colorpair = self.getcolorpair(name="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1160 elif linestr.startswith("+"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1161 colorpair = self.getcolorpair(name="addition")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1162 elif linestr.startswith("-"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1163 colorpair = self.getcolorpair(name="deletion")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1164 elif linestr.startswith("\\"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1165 colorpair = self.getcolorpair(name="normal")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1166
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1167 lineprefix = " "*self.hunklineindentnumchars + checkbox
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1168 outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1169 align=False) # add uncolored checkbox/indent
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1170 outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1171 towin=towin, showwhtspc=True)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1172 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1173
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1174 def printitem(self, item=None, ignorefolding=False, recursechildren=True,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1175 towin=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1176 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1177 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
1178 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
1179 (hiding folded elements, etc. -- see __printitem() docstring)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1180 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1181 if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1182 item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1183 if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1184 self.linesprintedtopadsofar = 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1185
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1186 outstr = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1187 self.__printitem(item, ignorefolding, recursechildren, outstr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1188 towin=towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1189 return ''.join(outstr)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1190
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1191 def outofdisplayedarea(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1192 y, _ = self.chunkpad.getyx() # cursor location
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1193 # * 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
1194 # consecutive non selectable lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1195 # 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
1196 miny = min(0, self.firstlineofpadtoprint - self.yscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1197 maxy = self.firstlineofpadtoprint + self.yscreensize * 2
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1198 return y < miny or y > maxy
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1199
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1200 def handleselection(self, item, recursechildren):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1201 selected = (item is self.currentselecteditem)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1202 if selected and recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1203 # assumes line numbering starting from line 0
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1204 self.selecteditemstartline = self.linesprintedtopadsofar
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1205 selecteditemlines = self.getnumlinesdisplayed(item,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1206 recursechildren=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1207 self.selecteditemendline = (self.selecteditemstartline +
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1208 selecteditemlines - 1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1209 return selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1210
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1211 def __printitem(self, item, ignorefolding, recursechildren, outstr,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1212 towin=True):
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 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
1215 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
1216 patch (not including coloring, etc.).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1217
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1218 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
1219
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1220 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
1221 child items.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1222
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 if towin and self.outofdisplayedarea():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1225 return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1226
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1227 selected = self.handleselection(item, recursechildren)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1228
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1229 # patch object is a list of headers
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1230 if isinstance(item, patch):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1231 if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1232 for hdr in item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1233 self.__printitem(hdr, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1234 recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1235 # todo: eliminate all isinstance() calls
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1236 if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1237 outstr.append(self.printheader(item, selected, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1238 ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1239 if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1240 for hnk in item.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1241 self.__printitem(hnk, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1242 recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1243 elif (isinstance(item, uihunk) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1244 ((not item.header.folded) or ignorefolding)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1245 # print the hunk data which comes before the changed-lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1246 outstr.append(self.printhunklinesbefore(item, selected, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1247 ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1248 if recursechildren:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1249 for l in item.changedlines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1250 self.__printitem(l, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1251 recursechildren, outstr, towin)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1252 outstr.append(self.printhunklinesafter(item, towin=towin,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1253 ignorefolding=ignorefolding))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1254 elif (isinstance(item, uihunkline) and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1255 ((not item.hunk.folded) or ignorefolding)):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1256 outstr.append(self.printhunkchangedline(item, selected,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1257 towin=towin))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1258
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1259 return outstr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1260
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1261 def getnumlinesdisplayed(self, item=None, ignorefolding=False,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1262 recursechildren=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1263 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1264 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
1265 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
1266 display (pad).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1267 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
1268 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
1269 the number of lines.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1270
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1271 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1272 # temporarily disable printing to windows by printstring
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1273 patchdisplaystring = self.printitem(item, ignorefolding,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1274 recursechildren, towin=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1275 numlines = len(patchdisplaystring) / self.xscreensize
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1276 return numlines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1277
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1278 def sigwinchhandler(self, n, frame):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1279 "handle window resizing"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1280 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1281 curses.endwin()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1282 self.yscreensize, self.xscreensize = gethw()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1283 self.statuswin.resize(self.numstatuslines, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1284 self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1285 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
1286 # todo: try to resize commit message window if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1287 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1288 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1289
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1290 def getcolorpair(self, fgcolor=None, bgcolor=None, name=None,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1291 attrlist=None):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1292 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1293 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
1294 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
1295 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
1296 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
1297 returned.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1298
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1299 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
1300 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
1301
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1302 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
1303 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
1304 curses.A_BOLD.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1305
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1306 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1307 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
1308 # 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
1309 colorpair = self.colorpairnames[name]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1310 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1311 if fgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1312 fgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1313 if bgcolor is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1314 bgcolor = -1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1315 if (fgcolor, bgcolor) in self.colorpairs:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1316 colorpair = self.colorpairs[(fgcolor, bgcolor)]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1317 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1318 pairindex = len(self.colorpairs) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1319 curses.init_pair(pairindex, fgcolor, bgcolor)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1320 colorpair = self.colorpairs[(fgcolor, bgcolor)] = (
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1321 curses.color_pair(pairindex))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1322 if name is not None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1323 self.colorpairnames[name] = curses.color_pair(pairindex)
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 # add attributes if possible
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1326 if attrlist is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1327 attrlist = []
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1328 if colorpair < 256:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1329 # 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
1330 for textattr in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1331 colorpair |= textattr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1332 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1333 # just apply a select few (safe?) attributes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1334 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
1335 if textattrib in attrlist:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1336 colorpair |= textattrib
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1337 return colorpair
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1338
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1339 def initcolorpair(self, *args, **kwargs):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1340 "same as getcolorpair."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1341 self.getcolorpair(*args, **kwargs)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1342
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1343 def helpwindow(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1344 "print a help window to the screen. exit after any keypress."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1345 helptext = """ [press any key to return to the patch-display]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1346
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1347 crecord allows you to interactively choose among the changes you have made,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1348 and commit only those changes you select. after committing the selected
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1349 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
1350 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
1351 the following are valid keystrokes:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1352
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1353 [space] : (un-)select item ([~]/[x] = partly/fully applied)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1354 a : (un-)select all items
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1355 up/down-arrow [k/j] : go to previous/next unfolded item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1356 pgup/pgdn [k/j] : go to previous/next item of same type
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1357 right/left-arrow [l/h] : go to child item / parent item
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1358 shift-left-arrow [h] : go to parent header / fold selected header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1359 f : fold / unfold item, hiding/revealing its children
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1360 f : fold / unfold parent item and all of its ancestors
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1361 m : edit / resume editing the commit message
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1362 e : edit the currently selected hunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1363 a : toggle amend mode (hg rev >= 2.2)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1364 c : commit selected changes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1365 r : review/edit and commit selected changes
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1366 q : quit without committing (no changes will be made)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1367 ? : help (what you're currently reading)"""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1368
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1369 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
1370 helplines = helptext.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1371 helplines = helplines + [" "]*(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1372 self.yscreensize - self.numstatuslines - len(helplines) - 1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1373 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1374 for line in helplines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1375 self.printstring(helpwin, line, pairname="legend")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1376 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1377 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1378 helpwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1379 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1380 helpwin.getkey()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1381 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1382 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1383
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1384 def confirmationwindow(self, windowtext):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1385 "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
1386
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1387 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
1388 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1389 lines = windowtext.split("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1390 for line in lines:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1391 self.printstring(confirmwin, line, pairname="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1392 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1393 pass
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1394 self.stdscr.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1395 confirmwin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1396 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1397 response = chr(self.stdscr.getch())
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1398 except ValueError:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1399 response = None
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 return response
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1402
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1403 def confirmcommit(self, review=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1404 "ask for 'y' to be pressed to confirm commit. return True if confirmed."
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1405 if review:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1406 confirmtext = (
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1407 """if you answer yes to the following, the your currently chosen patch chunks
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1408 will be loaded into an editor. you may modify the patch from the editor, and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1409 save the changes if you wish to change the patch. otherwise, you can just
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1410 close the editor without saving to accept the current patch as-is.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1411
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1412 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
1413 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
1414
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1415 are you sure you want to review/edit and commit the selected changes [yn]? """)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1416 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1417 confirmtext = (
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1418 "are you sure you want to commit the selected changes [yn]? ")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1419
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1420 response = self.confirmationwindow(confirmtext)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1421 if response is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1422 response = "n"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1423 if response.lower().startswith("y"):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1424 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1425 else:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1426 return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1427
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1428 def recenterdisplayedarea(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1429 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1430 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
1431 display zone. we print the patch with towin=False to compute the
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1432 location of the selected item eventhough it is outside of the displayed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1433 zone and then update the scroll.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1434 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1435 self.printitem(towin=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1436 self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1437
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1438 def toggleedit(self, item=None, test=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1439 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1440 edit the currently chelected chunk
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1441 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1442
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1443 def editpatchwitheditor(self, chunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1444 if chunk is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1445 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
1446 self.ui.write("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1447 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1448 if chunk.header.binary():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1449 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
1450 self.ui.write("\n")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1451 return None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1452 # patch comment based on the git one (based on comment at end of
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1453 # http://mercurial.selenic.com/wiki/recordextension)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1454 phelp = '---' + _("""
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1455 to remove '-' lines, make them ' ' lines (context).
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1456 to remove '+' lines, delete them.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1457 lines starting with # will be removed from the patch.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1458
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1459 if the patch applies cleanly, the edited hunk will immediately be
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1460 added to the record list. if it does not apply cleanly, a rejects
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1461 file will be generated: you can use that when you try again. if
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1462 all lines of the hunk are removed, then the edit is aborted and
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1463 the hunk is left unchanged.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1464 """)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1465 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1466 suffix=".diff", text=True)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1467 ncpatchfp = None
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1468 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1469 # write the initial patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1470 f = os.fdopen(patchfd, "w")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1471 chunk.header.write(f)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1472 chunk.write(f)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1473 f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1474 f.close()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1475 # start the editor and wait for it to complete
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1476 editor = self.ui.geteditor()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1477 self.ui.system("%s \"%s\"" % (editor, patchfn),
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1478 environ={'hguser': self.ui.username()},
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1479 onerr=util.Abort, errprefix=_("edit failed"))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1480 # remove comment lines
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1481 patchfp = open(patchfn)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1482 ncpatchfp = cStringIO.StringIO()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1483 for line in patchfp:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1484 if not line.startswith('#'):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1485 ncpatchfp.write(line)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1486 patchfp.close()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1487 ncpatchfp.seek(0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1488 newpatches = patchmod.parsepatch(ncpatchfp)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1489 finally:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1490 os.unlink(patchfn)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1491 del ncpatchfp
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1492 return newpatches
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1493 if item is None:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1494 item = self.currentselecteditem
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1495 if isinstance(item, uiheader):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1496 return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1497 if isinstance(item, uihunkline):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1498 item = item.parentitem()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1499 if not isinstance(item, uihunk):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1500 return
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1501
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1502 beforeadded, beforeremoved = item.added, item.removed
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1503 newpatches = editpatchwitheditor(self, item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1504 header = item.header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1505 editedhunkindex = header.hunks.index(item)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1506 hunksbefore = header.hunks[:editedhunkindex]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1507 hunksafter = header.hunks[editedhunkindex + 1:]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1508 newpatchheader = newpatches[0]
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1509 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
1510 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
1511 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
1512 offset = (newadded - beforeadded) - (newremoved - beforeremoved)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1513
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1514 for h in hunksafter:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1515 h.toline += offset
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1516 for h in newhunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1517 h.folded = False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1518 header.hunks = hunksbefore + newhunks + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1519 if self.emptypatch():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1520 header.hunks = hunksbefore + [item] + hunksafter
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1521 self.currentselecteditem = header
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1522
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1523 if not test:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1524 self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1525 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
1526 self.updatescroll()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1527 self.stdscr.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1528 self.statuswin.refresh()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1529 self.stdscr.keypad(1)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1530
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1531 def emptypatch(self):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1532 item = self.headerlist
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1533 if not item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1534 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1535 for header in item:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1536 if header.hunks:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1537 return False
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1538 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1539
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1540 def handlekeypressed(self, keypressed, test=False):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1541 if keypressed in ["k", "KEY_UP"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1542 self.uparrowevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1543 if keypressed in ["k", "KEY_PPAGE"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1544 self.uparrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1545 elif keypressed in ["j", "KEY_DOWN"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1546 self.downarrowevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1547 elif keypressed in ["j", "KEY_NPAGE"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1548 self.downarrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1549 elif keypressed in ["l", "KEY_RIGHT"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1550 self.rightarrowevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1551 elif keypressed in ["h", "KEY_LEFT"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1552 self.leftarrowevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1553 elif keypressed in ["h", "KEY_SLEFT"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1554 self.leftarrowshiftevent()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1555 elif keypressed in ["q"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1556 raise util.Abort(_('user quit'))
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1557 elif keypressed in ["c"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1558 if self.confirmcommit():
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1559 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1560 elif keypressed in ["r"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1561 if self.confirmcommit(review=True):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1562 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1563 elif test and keypressed in ['X']:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1564 return True
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1565 elif keypressed in [' '] or (test and keypressed in ["TOGGLE"]):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1566 self.toggleapply()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1567 elif keypressed in ['A']:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1568 self.toggleall()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1569 elif keypressed in ['e']:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1570 self.toggleedit(test=test)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1571 elif keypressed in ["f"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1572 self.togglefolded()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1573 elif keypressed in ["f"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1574 self.togglefolded(foldparent=True)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1575 elif keypressed in ["?"]:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1576 self.helpwindow()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1577
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1578 def main(self, stdscr):
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 method to be wrapped by curses.wrapper() for selecting chunks.
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1581
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1582 """
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1583 signal.signal(signal.SIGWINCH, self.sigwinchhandler)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1584 self.stdscr = stdscr
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1585 self.yscreensize, self.xscreensize = self.stdscr.getmaxyx()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1586
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1587 curses.start_color()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1588 curses.use_default_colors()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1589
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1590 # 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
1591 # 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
1592 self.initcolorpair(None, None, name="normal")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1593 self.initcolorpair(curses.COLOR_WHITE, curses.COLOR_MAGENTA,
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1594 name="selected")
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1595 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
1596 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
1597 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
1598 # newwin([height, width,] begin_y, begin_x)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1599 self.statuswin = curses.newwin(self.numstatuslines, 0, 0, 0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1600 self.statuswin.keypad(1) # interpret arrow-key, etc. esc sequences
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1601
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1602 # 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
1603 # used for displaying the patch
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1604
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1605 # stupid hack to prevent getnumlinesdisplayed from failing
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1606 self.chunkpad = curses.newpad(1, self.xscreensize)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1607
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1608 # 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
1609 self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1610 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
1611
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1612 # initialize selecteitemendline (initial start-line is 0)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1613 self.selecteditemendline = self.getnumlinesdisplayed(
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1614 self.currentselecteditem, recursechildren=False)
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1615
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1616 while True:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1617 self.updatescreen()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1618 try:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1619 keypressed = self.statuswin.getkey()
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1620 except curses.error:
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1621 keypressed = "foobar"
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1622 if self.handlekeypressed(keypressed):
6409fb6c934d record: add crecord's ui logic to core
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
1623 break