3
from userinteractor import UserInteractor, UserOption
9
def __init__(self, patches):
11
from colordiff import DiffWriter
12
from terminal import has_ansi_colors
14
self.diff_stream = DiffWriter(sys.stdout)
16
self.diff_stream = sys.stdout
18
self.diff_stream = sys.stdout
19
self.standard_options = [
20
UserOption('y', self._selected, self.strings['select_desc'],
22
UserOption('n', self._unselected, self.strings['unselect_desc']),
23
UserOption('d', UserInteractor.FINISH, 'done, skip to the end.'),
24
UserOption('i', self._invert,
25
'invert the current selection status of all hunks.'),
26
UserOption('s', self._status,
27
'show selection status of all hunks.'),
28
UserOption('q', UserInteractor.QUIT, 'quit')
32
UserOption('y', UserInteractor.FINISH, self.strings['finish_desc'],
34
UserOption('r', UserInteractor.RESTART,
35
'restart the hunk selection loop.'),
36
UserOption('s', self._status,
37
'show selection status of all hunks.'),
38
UserOption('i', self._invert,
39
'invert the current selection status of all hunks.'),
40
UserOption('q', UserInteractor.QUIT, 'quit')
43
self.patches = patches
46
self.interactor = UserInteractor()
47
self.interactor.set_item_callback(self._hunk_callback)
48
self.interactor.set_start_callback(self._start_callback)
49
self.interactor.set_end_callback(self._end_callback)
52
for hunk in patch.hunks:
53
# everything's selected by default
56
# we need a back pointer in the callbacks
58
self.interactor.add_item(hunk)
60
# Called at the start of the main loop
61
def _start_callback(self):
62
self.last_printed = -1
63
self.interactor.set_prompt(self.strings['prompt'])
64
self.interactor.set_options(self.standard_options)
66
# Called at the end of the item loop, return False to indicate that the
67
# interaction isn't finished and the confirmation prompt should be displayed
68
def _end_callback(self):
70
self.interactor.set_prompt(self.strings['end_prompt'])
71
self.interactor.set_options(self.end_options)
74
# Called once for each hunk
75
def _hunk_callback(self, hunk, count):
76
if self.last_printed != count:
77
self.diff_stream.write(str(hunk.patch.get_header()))
78
self.diff_stream.write(str(hunk))
79
self.last_printed = count
82
self.interactor.get_option('y').default = True
83
self.interactor.get_option('n').default = False
85
self.interactor.get_option('y').default = False
86
self.interactor.get_option('n').default = True
88
# The user chooses to (un)shelve a hunk
89
def _selected(self, hunk):
93
# The user chooses to keep a hunk
94
def _unselected(self, hunk):
98
# The user chooses to invert the selection
99
def _invert(self, hunk):
100
for patch in self.patches:
101
for hunk in patch.hunks:
102
if hunk.__dict__.has_key('selected'):
103
hunk.selected = not hunk.selected
109
# The user wants to see the status
110
def _status(self, hunk=None):
112
for patch in self.patches:
113
print ' %s' % patch.oldname
116
for hunk in patch.hunks:
122
print ' ', self.strings['status_selected'] % selected
123
print ' ', self.strings['status_unselected'] % unselected
126
# Tell the interactor we're not done with this item
130
if self.total_hunks == 0 or not self.interactor.interact():
131
# False from interact means they chose to quit
134
# Go through each patch and collect all selected/unselected hunks
135
for patch in self.patches:
137
patch.unselected = []
138
for hunk in patch.hunks:
140
patch.selected.append(hunk)
142
patch.unselected.append(hunk)
144
# Now build two lists, one of selected patches the other unselected
145
selected_patches = []
146
unselected_patches = []
148
for patch in self.patches:
149
if len(patch.selected):
150
tmp = copy.copy(patch)
151
tmp.hunks = tmp.selected
154
selected_patches.append(tmp)
156
if len(patch.unselected):
157
tmp = copy.copy(patch)
158
tmp.hunks = tmp.unselected
161
unselected_patches.append(tmp)
163
return (selected_patches, unselected_patches)
165
class ShelveHunkSelector(HunkSelector):
166
def __init__(self, patches):
168
self.strings['status_selected'] = '%d hunks to be shelved'
169
self.strings['status_unselected'] = '%d hunks to be kept'
170
self.strings['select_desc'] = 'shelve this change.'
171
self.strings['unselect_desc'] = 'keep this change in your tree.'
172
self.strings['finish_desc'] = 'shelve selected changes.'
173
self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
174
self.strings['end_prompt'] = 'Shelve these changes?'
175
HunkSelector.__init__(self, patches)
177
class UnshelveHunkSelector(HunkSelector):
178
def __init__(self, patches):
180
self.strings['status_selected'] = '%d hunks to be unshelved'
181
self.strings['status_unselected'] = '%d hunks left on shelf'
182
self.strings['select_desc'] = 'unshelve this change.'
183
self.strings['unselect_desc'] = 'leave this change on the shelf.'
184
self.strings['finish_desc'] = 'unshelve selected changes.'
185
self.strings['prompt'] = 'Unshelve this change? ' \
186
'(%(count)d of %(total)d)'
187
self.strings['end_prompt'] = 'Unshelve these changes?'
188
HunkSelector.__init__(self, patches)