~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Michael Ellerman
  • Date: 2005-11-28 18:32:14 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-20051128183214-abade6823cc5ff40
Add support for 'unshelve --pick'. This works but the UI is broken, as the
hunk selector prompts are all still related to shelving, not unshelving.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
import sys
 
3
from userinteractor import UserInteractor, UserOption
4
4
 
5
5
class HunkSelector:
6
 
    class Option:
7
 
        def __init__(self, char, action, help, default=False):
8
 
            self.char = char
9
 
            self.action = action
10
 
            self.default = default
11
 
            self.help = help
12
 
 
13
 
    standard_options = [
14
 
        Option('n', 'shelve', 'shelve this change for the moment.',
15
 
            default=True),
16
 
        Option('y', 'keep', 'keep this change in your tree.'),
17
 
        Option('d', 'done', 'done, skip to the end.'),
18
 
        Option('i', 'invert', 'invert the current selection of all hunks.'),
19
 
        Option('s', 'status', 'show status of hunks.'),
20
 
        Option('q', 'quit', 'quit')
21
 
    ]
22
 
 
23
 
    end_options = [
24
 
        Option('y', 'continue', 'proceed to shelve selected changes.',
25
 
            default=True),
26
 
        Option('r', 'restart', 'restart the hunk selection loop.'),
27
 
        Option('s', 'status', 'show status of hunks.'),
28
 
        Option('i', 'invert', 'invert the current selection of all hunks.'),
29
 
        Option('q', 'quit', 'quit')
30
 
    ]
31
 
 
32
6
    def __init__(self, patches):
 
7
        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('d', UserInteractor.FINISH, 'done, skip to the end.'),
 
11
            UserOption('i', self.invert,
 
12
                'invert the current selection status of all hunks.'),
 
13
            UserOption('s', self.status, 'show selection status of all hunks.'),
 
14
            UserOption('q', UserInteractor.QUIT, 'quit')
 
15
        ]
 
16
 
 
17
        self.end_options = [
 
18
            UserOption('y', UserInteractor.FINISH,
 
19
                'shelve selected changes.', default=True),
 
20
            UserOption('r', UserInteractor.RESTART,
 
21
                'restart the hunk selection loop.'),
 
22
            UserOption('s', self.status, 'show selection status of all hunks.'),
 
23
            UserOption('i', self.invert,
 
24
                'invert the current selection status of all hunks.'),
 
25
            UserOption('q', UserInteractor.QUIT, 'quit')
 
26
        ]
 
27
 
33
28
        self.patches = patches
34
29
        self.total_hunks = 0
 
30
 
 
31
        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)
 
35
 
35
36
        for patch in patches:
36
37
            for hunk in patch.hunks:
37
38
                # everything's shelved by default
38
39
                hunk.selected = True
39
40
                self.total_hunks += 1
40
 
 
41
 
    def __get_option(self, char):
42
 
        for opt in self.standard_options:
43
 
            if opt.char == char:
44
 
                return opt
45
 
        raise Exception('Option "%s" not found!' % char)
46
 
 
47
 
    def __select_loop(self):
48
 
        j = 0
49
 
        for patch in self.patches:
50
 
            i = 0
51
 
            lasti = -1
52
 
            while i < len(patch.hunks):
53
 
                hunk = patch.hunks[i]
54
 
                if lasti != i:
55
 
                    print patch.get_header(), hunk
56
 
                    j += 1
57
 
                lasti = i
58
 
 
59
 
                prompt = 'Keep this change? (%d of %d)' \
60
 
                            % (j, self.total_hunks)
61
 
 
62
 
                if hunk.selected:
63
 
                    self.__get_option('n').default = True
64
 
                    self.__get_option('y').default = False
65
 
                else:
66
 
                    self.__get_option('n').default = False
67
 
                    self.__get_option('y').default = True
68
 
 
69
 
                action = self.__ask_user(prompt, self.standard_options)
70
 
 
71
 
                if action == 'keep':
72
 
                    hunk.selected = False
73
 
                elif action == 'shelve':
74
 
                    hunk.selected = True
75
 
                elif action == 'done':
76
 
                    return True
77
 
                elif action == 'invert':
78
 
                    self.__invert_selection()
79
 
                    self.__show_status()
80
 
                    continue
81
 
                elif action == 'status':
82
 
                    self.__show_status()
83
 
                    continue
84
 
                elif action == 'quit':
85
 
                    return False
86
 
 
87
 
                i += 1
88
 
        return True
89
 
 
90
 
    def select(self):
91
 
        if self.total_hunks == 0:
92
 
            return []
93
 
 
94
 
        done = False
95
 
        while not done:
96
 
            if not self.__select_loop():
97
 
                return []
98
 
 
99
 
            while True:
100
 
                self.__show_status()
101
 
                prompt = "Shelve these changes, or restart?"
102
 
                action = self.__ask_user(prompt, self.end_options)
103
 
 
104
 
                if action == 'continue':
105
 
                    done = True
106
 
                    break
107
 
                elif action == 'quit':
108
 
                    return []
109
 
                elif action == 'status':
110
 
                    self.__show_status()
111
 
                elif action == 'invert':
112
 
                    self.__invert_selection()
113
 
                elif action == 'restart':
114
 
                    break
115
 
 
116
 
 
117
 
        for patch in self.patches:
118
 
            tmp = []
119
 
            for hunk in patch.hunks:
120
 
                if hunk.selected:
121
 
                    tmp.append(hunk)
122
 
            patch.hunks = tmp
123
 
 
124
 
        tmp = []
125
 
        for patch in self.patches:
126
 
            if len(patch.hunks):
127
 
                tmp.append(patch)
128
 
        self.patches = tmp
129
 
 
130
 
        return self.patches
131
 
 
132
 
    def __invert_selection(self):
 
41
                # we need a back pointer in the callbacks
 
42
                hunk.patch = patch
 
43
                self.interactor.add_item(hunk)
 
44
 
 
45
    # Called at the start of the main loop
 
46
    def start_callback(self):
 
47
        self.last_printed = -1
 
48
        self.interactor.set_prompt('Shelve this change? ' \
 
49
            '(%(count)d of %(total)d)')
 
50
        self.interactor.set_options(self.standard_options)
 
51
 
 
52
    # Called at the end of the item loop, return False to indicate that the
 
53
    # 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?")
 
57
        self.interactor.set_options(self.end_options)
 
58
        return False
 
59
 
 
60
    # Called once for each hunk
 
61
    def hunk_callback(self, hunk, count):
 
62
        if self.last_printed != count:
 
63
            print hunk.patch.get_header(), hunk
 
64
            self.last_printed = count
 
65
 
 
66
        if hunk.selected:
 
67
            self.interactor.get_option('y').default = True
 
68
            self.interactor.get_option('n').default = False
 
69
        else:
 
70
            self.interactor.get_option('y').default = False
 
71
            self.interactor.get_option('n').default = True
 
72
 
 
73
    # The user chooses to shelve a hunk
 
74
    def shelve(self, hunk):
 
75
        hunk.selected = True
 
76
        return True
 
77
 
 
78
    # The user chooses to keep a hunk
 
79
    def keep(self, hunk):
 
80
        hunk.selected = False
 
81
        return True
 
82
 
 
83
    # The user chooses to invert the selection
 
84
    def invert(self, hunk):
133
85
        for patch in self.patches:
134
86
            for hunk in patch.hunks:
135
87
                if hunk.__dict__.has_key('selected'):
136
88
                    hunk.selected = not hunk.selected
137
89
                else:
138
90
                    hunk.selected = True
 
91
        self.status()
 
92
        return False
139
93
 
140
 
    def __show_status(self):
 
94
    # The user wants to see the status
 
95
    def status(self, hunk=None):
141
96
        print '\nStatus:'
142
97
        for patch in self.patches:
143
98
            print '  %s' % patch.oldname
153
108
            print '    %d hunks to be kept' % keep
154
109
            print
155
110
 
156
 
    if sys.platform == "win32":
157
 
        import msvcrt
158
 
        def __getchar(self):
159
 
            return msvcrt.getche()
160
 
    else:
161
 
        def __getchar(self):
162
 
            import tty
163
 
            import termios
164
 
            fd = sys.stdin.fileno()
165
 
            settings = termios.tcgetattr(fd)
166
 
            try:
167
 
                tty.setraw(fd)
168
 
                ch = sys.stdin.read(1)
169
 
            finally:
170
 
                termios.tcsetattr(fd, termios.TCSADRAIN, settings)
171
 
            return ch
172
 
 
173
 
    def __ask_user(self, prompt, options):
174
 
        while True:
175
 
            sys.stdout.write(prompt)
176
 
            sys.stdout.write(' [')
177
 
            for opt in options:
178
 
                if opt.default:
179
 
                    default = opt
180
 
                sys.stdout.write(opt.char)
181
 
            sys.stdout.write('?] (%s): ' % default.char)
182
 
 
183
 
            response = self.__getchar()
184
 
 
185
 
            # default, which we see as newline, is 'n'
186
 
            if response in ['\n', '\r', '\r\n']:
187
 
                response = default.char
188
 
 
189
 
            print response # because echo is off
190
 
 
191
 
            for opt in options:
192
 
                if opt.char == response:
193
 
                    return opt.action
194
 
 
195
 
            for opt in options:
196
 
                print '  %s - %s' % (opt.char, opt.help)
 
111
        # Tell the interactor we're not done with this item
 
112
        return False
 
113
 
 
114
    def select(self):
 
115
        if self.total_hunks == 0:
 
116
            return []
 
117
 
 
118
        if not self.interactor.interact():
 
119
            # False from interact means they quit
 
120
            return []
 
121
 
 
122
        for patch in self.patches:
 
123
            tmp = []
 
124
            for hunk in patch.hunks:
 
125
                if hunk.selected:
 
126
                    tmp.append(hunk)
 
127
            patch.hunks = tmp
 
128
 
 
129
        tmp = []
 
130
        for patch in self.patches:
 
131
            if len(patch.hunks):
 
132
                tmp.append(patch)
 
133
        self.patches = tmp
 
134
 
 
135
        return self.patches