~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Aaron Bentley
  • Date: 2006-08-02 03:23:09 UTC
  • mto: This revision was merged to the branch mainline in revision 425.
  • Revision ID: aaron.bentley@utoronto.ca-20060802032309-6ad0139e61304b19
Etienne Goyer: remove unused shebangs, update packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
 
 
3
from userinteractor import UserInteractor, UserOption
 
4
import copy
 
5
 
 
6
class HunkSelector:
 
7
    strings = {}
 
8
 
 
9
    def __init__(self, patches):
 
10
        try:
 
11
            from colordiff import DiffWriter
 
12
            from terminal import has_ansi_colors
 
13
            if has_ansi_colors():
 
14
                self.diff_stream = DiffWriter(sys.stdout)
 
15
            else:
 
16
                self.diff_stream = sys.stdout
 
17
        except ImportError:
 
18
            self.diff_stream = sys.stdout
 
19
        self.standard_options = [
 
20
            UserOption('y', self._selected, self.strings['select_desc'],
 
21
                default=True),
 
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')
 
29
        ]
 
30
 
 
31
        self.end_options = [
 
32
            UserOption('y', UserInteractor.FINISH, self.strings['finish_desc'],
 
33
                default=True),
 
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')
 
41
        ]
 
42
 
 
43
        self.patches = patches
 
44
        self.total_hunks = 0
 
45
 
 
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)
 
50
 
 
51
        for patch in patches:
 
52
            for hunk in patch.hunks:
 
53
                # everything's selected by default
 
54
                hunk.selected = True
 
55
                self.total_hunks += 1
 
56
                # we need a back pointer in the callbacks
 
57
                hunk.patch = patch
 
58
                self.interactor.add_item(hunk)
 
59
 
 
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)
 
65
 
 
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):
 
69
        self._status()
 
70
        self.interactor.set_prompt(self.strings['end_prompt'])
 
71
        self.interactor.set_options(self.end_options)
 
72
        return False
 
73
 
 
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
 
80
 
 
81
        if hunk.selected:
 
82
            self.interactor.get_option('y').default = True
 
83
            self.interactor.get_option('n').default = False
 
84
        else:
 
85
            self.interactor.get_option('y').default = False
 
86
            self.interactor.get_option('n').default = True
 
87
 
 
88
    # The user chooses to (un)shelve a hunk
 
89
    def _selected(self, hunk):
 
90
        hunk.selected = True
 
91
        return True
 
92
 
 
93
    # The user chooses to keep a hunk
 
94
    def _unselected(self, hunk):
 
95
        hunk.selected = False
 
96
        return True
 
97
 
 
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
 
104
                else:
 
105
                    hunk.selected = True
 
106
        self._status()
 
107
        return False
 
108
 
 
109
    # The user wants to see the status
 
110
    def _status(self, hunk=None):
 
111
        print '\nStatus:'
 
112
        for patch in self.patches:
 
113
            print '  %s' % patch.oldname
 
114
            selected = 0
 
115
            unselected = 0
 
116
            for hunk in patch.hunks:
 
117
                if hunk.selected:
 
118
                    selected += 1
 
119
                else:
 
120
                    unselected += 1
 
121
 
 
122
            print '  ', self.strings['status_selected'] % selected
 
123
            print '  ', self.strings['status_unselected'] % unselected
 
124
            print
 
125
 
 
126
        # Tell the interactor we're not done with this item
 
127
        return False
 
128
 
 
129
    def select(self):
 
130
        if self.total_hunks == 0 or not self.interactor.interact():
 
131
            # False from interact means they chose to quit
 
132
            return ([], [])
 
133
 
 
134
        # Go through each patch and collect all selected/unselected hunks
 
135
        for patch in self.patches:
 
136
            patch.selected = []
 
137
            patch.unselected = []
 
138
            for hunk in patch.hunks:
 
139
                if hunk.selected:
 
140
                    patch.selected.append(hunk)
 
141
                else:
 
142
                    patch.unselected.append(hunk)
 
143
 
 
144
        # Now build two lists, one of selected patches the other unselected
 
145
        selected_patches = []
 
146
        unselected_patches = []
 
147
 
 
148
        for patch in self.patches:
 
149
            if len(patch.selected):
 
150
                tmp = copy.copy(patch)
 
151
                tmp.hunks = tmp.selected
 
152
                del tmp.selected
 
153
                del tmp.unselected
 
154
                selected_patches.append(tmp)
 
155
 
 
156
            if len(patch.unselected):
 
157
                tmp = copy.copy(patch)
 
158
                tmp.hunks = tmp.unselected
 
159
                del tmp.selected
 
160
                del tmp.unselected
 
161
                unselected_patches.append(tmp)
 
162
 
 
163
        return (selected_patches, unselected_patches)
 
164
 
 
165
class ShelveHunkSelector(HunkSelector):
 
166
    def __init__(self, patches):
 
167
        self.strings = {}
 
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)
 
176
 
 
177
class UnshelveHunkSelector(HunkSelector):
 
178
    def __init__(self, patches):
 
179
        self.strings = {}
 
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)