~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: abentley
  • Date: 2005-04-30 07:31:13 UTC
  • Revision ID: abentley@lappy-20050430073113-bb4f4a80c01a6cf5
GPLed the project, ignored files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
from userinteractor import UserInteractor, UserOption
4
 
 
5
 
class HunkSelector:
6
 
    strings = {}
7
 
 
8
 
    def __init__(self, patches):
9
 
        self.standard_options = [
10
 
            UserOption('y', self._selected, self.strings['select_desc'],
11
 
                default=True),
12
 
            UserOption('n', self._unselected, self.strings['unselect_desc']),
13
 
            UserOption('d', UserInteractor.FINISH, 'done, skip to the end.'),
14
 
            UserOption('i', self._invert,
15
 
                'invert the current selection status of all hunks.'),
16
 
            UserOption('s', self._status,
17
 
                'show selection status of all hunks.'),
18
 
            UserOption('q', UserInteractor.QUIT, 'quit')
19
 
        ]
20
 
 
21
 
        self.end_options = [
22
 
            UserOption('y', UserInteractor.FINISH, self.strings['finish_desc'],
23
 
                default=True),
24
 
            UserOption('r', UserInteractor.RESTART,
25
 
                'restart the hunk selection loop.'),
26
 
            UserOption('s', self._status,
27
 
                'show selection status of all hunks.'),
28
 
            UserOption('i', self._invert,
29
 
                'invert the current selection status of all hunks.'),
30
 
            UserOption('q', UserInteractor.QUIT, 'quit')
31
 
        ]
32
 
 
33
 
        self.patches = patches
34
 
        self.total_hunks = 0
35
 
 
36
 
        self.interactor = UserInteractor()
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)
40
 
 
41
 
        for patch in patches:
42
 
            for hunk in patch.hunks:
43
 
                # everything's selected by default
44
 
                hunk.selected = True
45
 
                self.total_hunks += 1
46
 
                # we need a back pointer in the callbacks
47
 
                hunk.patch = patch
48
 
                self.interactor.add_item(hunk)
49
 
 
50
 
    # Called at the start of the main loop
51
 
    def _start_callback(self):
52
 
        self.last_printed = -1
53
 
        self.interactor.set_prompt(self.strings['prompt'])
54
 
        self.interactor.set_options(self.standard_options)
55
 
 
56
 
    # Called at the end of the item loop, return False to indicate that the
57
 
    # interaction isn't finished and the confirmation prompt should be displayed
58
 
    def _end_callback(self):
59
 
        self._status()
60
 
        self.interactor.set_prompt(self.strings['end_prompt'])
61
 
        self.interactor.set_options(self.end_options)
62
 
        return False
63
 
 
64
 
    # Called once for each hunk
65
 
    def _hunk_callback(self, hunk, count):
66
 
        if self.last_printed != count:
67
 
            print hunk.patch.get_header(), hunk
68
 
            self.last_printed = count
69
 
 
70
 
        if hunk.selected:
71
 
            self.interactor.get_option('y').default = True
72
 
            self.interactor.get_option('n').default = False
73
 
        else:
74
 
            self.interactor.get_option('y').default = False
75
 
            self.interactor.get_option('n').default = True
76
 
 
77
 
    # The user chooses to (un)shelve a hunk
78
 
    def _selected(self, hunk):
79
 
        hunk.selected = True
80
 
        return True
81
 
 
82
 
    # The user chooses to keep a hunk
83
 
    def _unselected(self, hunk):
84
 
        hunk.selected = False
85
 
        return True
86
 
 
87
 
    # The user chooses to invert the selection
88
 
    def _invert(self, hunk):
89
 
        for patch in self.patches:
90
 
            for hunk in patch.hunks:
91
 
                if hunk.__dict__.has_key('selected'):
92
 
                    hunk.selected = not hunk.selected
93
 
                else:
94
 
                    hunk.selected = True
95
 
        self._status()
96
 
        return False
97
 
 
98
 
    # The user wants to see the status
99
 
    def _status(self, hunk=None):
100
 
        print '\nStatus:'
101
 
        for patch in self.patches:
102
 
            print '  %s' % patch.oldname
103
 
            selected = 0
104
 
            unselected = 0
105
 
            for hunk in patch.hunks:
106
 
                if hunk.selected:
107
 
                    selected += 1
108
 
                else:
109
 
                    unselected += 1
110
 
 
111
 
            print '  ', self.strings['status_selected'] % selected
112
 
            print '  ', self.strings['status_unselected'] % unselected
113
 
            print
114
 
 
115
 
        # Tell the interactor we're not done with this item
116
 
        return False
117
 
 
118
 
    def select(self):
119
 
        if self.total_hunks == 0:
120
 
            return []
121
 
 
122
 
        if not self.interactor.interact():
123
 
            # False from interact means they quit
124
 
            return []
125
 
 
126
 
        for patch in self.patches:
127
 
            tmp = []
128
 
            for hunk in patch.hunks:
129
 
                if hunk.selected:
130
 
                    tmp.append(hunk)
131
 
            patch.hunks = tmp
132
 
 
133
 
        tmp = []
134
 
        for patch in self.patches:
135
 
            if len(patch.hunks):
136
 
                tmp.append(patch)
137
 
        self.patches = tmp
138
 
 
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)