~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 01:41:15 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129014115-eff5eb80bc51e0f5
Make HunkSelector agnostic as to whether it's selecting for shelving or
unshelving. Create ShelveHunkSelector() and UnshelveHunkSelector() to
encapsulate this, a bit ugly, but reasonable.

Rename callbacks to be private, ie. beginning with _.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from userinteractor import UserInteractor, UserOption
4
4
 
5
5
class HunkSelector:
 
6
    strings = {}
 
7
 
6
8
    def __init__(self, patches):
7
9
        self.standard_options = [
8
 
            UserOption('y', self.shelve, 'shelve this change.', default=True),
9
 
            UserOption('n', self.keep, 'keep this change in your tree.'),
 
10
            UserOption('y', self._selected, self.strings['select_desc'],
 
11
                default=True),
 
12
            UserOption('n', self._unselected, self.strings['unselect_desc']),
10
13
            UserOption('d', UserInteractor.FINISH, 'done, skip to the end.'),
11
 
            UserOption('i', self.invert,
 
14
            UserOption('i', self._invert,
12
15
                'invert the current selection status of all hunks.'),
13
 
            UserOption('s', self.status, 'show selection status of all hunks.'),
 
16
            UserOption('s', self._status,
 
17
                'show selection status of all hunks.'),
14
18
            UserOption('q', UserInteractor.QUIT, 'quit')
15
19
        ]
16
20
 
17
21
        self.end_options = [
18
 
            UserOption('y', UserInteractor.FINISH,
19
 
                'shelve selected changes.', default=True),
 
22
            UserOption('y', UserInteractor.FINISH, self.strings['finish_desc'],
 
23
                default=True),
20
24
            UserOption('r', UserInteractor.RESTART,
21
25
                'restart the hunk selection loop.'),
22
 
            UserOption('s', self.status, 'show selection status of all hunks.'),
23
 
            UserOption('i', self.invert,
 
26
            UserOption('s', self._status,
 
27
                'show selection status of all hunks.'),
 
28
            UserOption('i', self._invert,
24
29
                'invert the current selection status of all hunks.'),
25
30
            UserOption('q', UserInteractor.QUIT, 'quit')
26
31
        ]
29
34
        self.total_hunks = 0
30
35
 
31
36
        self.interactor = UserInteractor()
32
 
        self.interactor.set_item_callback(self.hunk_callback)
33
 
        self.interactor.set_start_callback(self.start_callback)
34
 
        self.interactor.set_end_callback(self.end_callback)
 
37
        self.interactor.set_item_callback(self._hunk_callback)
 
38
        self.interactor.set_start_callback(self._start_callback)
 
39
        self.interactor.set_end_callback(self._end_callback)
35
40
 
36
41
        for patch in patches:
37
42
            for hunk in patch.hunks:
38
 
                # everything's shelved by default
 
43
                # everything's selected by default
39
44
                hunk.selected = True
40
45
                self.total_hunks += 1
41
46
                # we need a back pointer in the callbacks
43
48
                self.interactor.add_item(hunk)
44
49
 
45
50
    # Called at the start of the main loop
46
 
    def start_callback(self):
 
51
    def _start_callback(self):
47
52
        self.last_printed = -1
48
 
        self.interactor.set_prompt('Shelve this change? ' \
49
 
            '(%(count)d of %(total)d)')
 
53
        self.interactor.set_prompt(self.strings['prompt'])
50
54
        self.interactor.set_options(self.standard_options)
51
55
 
52
56
    # Called at the end of the item loop, return False to indicate that the
53
57
    # interaction isn't finished and the confirmation prompt should be displayed
54
 
    def end_callback(self):
55
 
        self.status()
56
 
        self.interactor.set_prompt("Shelve these changes?")
 
58
    def _end_callback(self):
 
59
        self._status()
 
60
        self.interactor.set_prompt(self.strings['end_prompt'])
57
61
        self.interactor.set_options(self.end_options)
58
62
        return False
59
63
 
60
64
    # Called once for each hunk
61
 
    def hunk_callback(self, hunk, count):
 
65
    def _hunk_callback(self, hunk, count):
62
66
        if self.last_printed != count:
63
67
            print hunk.patch.get_header(), hunk
64
68
            self.last_printed = count
70
74
            self.interactor.get_option('y').default = False
71
75
            self.interactor.get_option('n').default = True
72
76
 
73
 
    # The user chooses to shelve a hunk
74
 
    def shelve(self, hunk):
 
77
    # The user chooses to (un)shelve a hunk
 
78
    def _selected(self, hunk):
75
79
        hunk.selected = True
76
80
        return True
77
81
 
78
82
    # The user chooses to keep a hunk
79
 
    def keep(self, hunk):
 
83
    def _unselected(self, hunk):
80
84
        hunk.selected = False
81
85
        return True
82
86
 
83
87
    # The user chooses to invert the selection
84
 
    def invert(self, hunk):
 
88
    def _invert(self, hunk):
85
89
        for patch in self.patches:
86
90
            for hunk in patch.hunks:
87
91
                if hunk.__dict__.has_key('selected'):
88
92
                    hunk.selected = not hunk.selected
89
93
                else:
90
94
                    hunk.selected = True
91
 
        self.status()
 
95
        self._status()
92
96
        return False
93
97
 
94
98
    # The user wants to see the status
95
 
    def status(self, hunk=None):
 
99
    def _status(self, hunk=None):
96
100
        print '\nStatus:'
97
101
        for patch in self.patches:
98
102
            print '  %s' % patch.oldname
99
 
            shelve = 0
100
 
            keep = 0
 
103
            selected = 0
 
104
            unselected = 0
101
105
            for hunk in patch.hunks:
102
106
                if hunk.selected:
103
 
                    shelve += 1
 
107
                    selected += 1
104
108
                else:
105
 
                    keep += 1
 
109
                    unselected += 1
106
110
 
107
 
            print '    %d hunks to be shelved' % shelve
108
 
            print '    %d hunks to be kept' % keep
 
111
            print '  ', self.strings['status_selected'] % selected
 
112
            print '  ', self.strings['status_unselected'] % unselected
109
113
            print
110
114
 
111
115
        # Tell the interactor we're not done with this item
133
137
        self.patches = tmp
134
138
 
135
139
        return self.patches
 
140
 
 
141
class ShelveHunkSelector(HunkSelector):
 
142
    def __init__(self, patches):
 
143
        self.strings = {}
 
144
        self.strings['status_selected'] = '%d hunks to be shelved'
 
145
        self.strings['status_unselected'] = '%d hunks to be kept'
 
146
        self.strings['select_desc'] = 'shelve this change.'
 
147
        self.strings['unselect_desc'] = 'keep this change in your tree.'
 
148
        self.strings['finish_desc'] = 'shelve selected changes.'
 
149
        self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
 
150
        self.strings['end_prompt'] = 'Shelve these changes?'
 
151
        HunkSelector.__init__(self, patches)
 
152
 
 
153
class UnshelveHunkSelector(HunkSelector):
 
154
    def __init__(self, patches):
 
155
        self.strings = {}
 
156
        self.strings['status_selected'] = '%d hunks to be unshelved'
 
157
        self.strings['status_unselected'] = '%d hunks left on shelf'
 
158
        self.strings['select_desc'] = 'unshelve this change.'
 
159
        self.strings['unselect_desc'] = 'leave this change on the shelf.'
 
160
        self.strings['finish_desc'] = 'unshelve selected changes.'
 
161
        self.strings['prompt'] = 'Unshelve this change? ' \
 
162
            '(%(count)d of %(total)d)'
 
163
        self.strings['end_prompt'] = 'Unshelve these changes?'
 
164
        HunkSelector.__init__(self, patches)