~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Michael Ellerman
  • Date: 2005-11-28 08:45:46 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-20051128084546-372d14c519162bbd
Move UserInteractor into a seperate file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
import sys
4
 
 
5
 
class UserOption:
6
 
    def __init__(self, char, action, help, default=False):
7
 
        self.char = char
8
 
        self.action = action
9
 
        self.default = default
10
 
        self.help = help
11
 
 
12
 
class UserInteractor(object):
13
 
    # Special actions
14
 
    RESTART = 0
15
 
    QUIT = 1
16
 
    FINISH = 2
17
 
 
18
 
    def __init__(self):
19
 
        self.items = []
20
 
 
21
 
    def add_item(self, item):
22
 
        self.items.append(item)
23
 
        self.__total_items = len(self.items)
24
 
 
25
 
    def set_items(self, item_list):
26
 
        self.items = item_list
27
 
        self.__total_items = len(self.items)
28
 
 
29
 
    def set_item_callback(self, cb):
30
 
        self.item_callback = cb
31
 
 
32
 
    def set_start_callback(self, cb):
33
 
        self.start_callback = cb
34
 
 
35
 
    def set_end_callback(self, cb):
36
 
        self.end_callback = cb
37
 
 
38
 
    def set_prompt(self, prompt):
39
 
        self.prompt = prompt
40
 
 
41
 
    def set_options(self, opt_list):
42
 
        self._options = {}
43
 
        for option in opt_list:
44
 
            self.add_option(option)
45
 
 
46
 
    def add_option(self, option):
47
 
        self._options[option.char] = option
48
 
 
49
 
    def get_option(self, char):
50
 
        return self._options[char]
51
 
 
52
 
    def __do_action(self, action, item):
53
 
        if type(action) is int:
54
 
            if action == self.QUIT:
55
 
                self.__quit = True
56
 
                self.__finished = True
57
 
            elif action == self.RESTART:
58
 
                self.__restart = True
59
 
            elif action == self.FINISH:
60
 
                self.__finished = True
61
 
            return True
62
 
        else:
63
 
            return action(item)
64
 
 
65
 
    def __select_loop(self):
66
 
        i = 0
67
 
        self.start_callback()
68
 
        while i < len(self.items):
69
 
                item = self.items[i]
70
 
 
71
 
                self.item_callback(item, i + 1)
72
 
 
73
 
                if self.__ask_once(item, i + 1):
74
 
                    i += 1
75
 
 
76
 
                if self.__quit or self.__finished:
77
 
                    break
78
 
 
79
 
    def interact(self):
80
 
        self.__quit = False
81
 
        self.__finished = False
82
 
 
83
 
        while not self.__quit and not self.__finished:
84
 
            self.__restart = False
85
 
 
86
 
            self.__select_loop()
87
 
            if self.__quit:
88
 
                break
89
 
 
90
 
            if self.end_callback():
91
 
                break
92
 
 
93
 
            self.__ask_once(None, self.__total_items)
94
 
            while not self.__finished and not self.__restart:
95
 
                self.__ask_once(None, -1)
96
 
 
97
 
        return not self.__quit
98
 
 
99
 
    def __ask_once(self, item, count):
100
 
        args = {'count': count, 'total' : self.__total_items}
101
 
 
102
 
        while True:
103
 
            sys.stdout.write(self.prompt % args)
104
 
            sys.stdout.write(' [')
105
 
            for char, opt in self._options.iteritems():
106
 
                if opt.default:
107
 
                    default = opt
108
 
                sys.stdout.write(opt.char)
109
 
            sys.stdout.write('?] (%s): ' % default.char)
110
 
 
111
 
            response = self.__getchar()
112
 
 
113
 
            # default, which we see as newline, is 'n'
114
 
            if response in ['\n', '\r', '\r\n']:
115
 
                response = default.char
116
 
 
117
 
            print response # because echo is off
118
 
 
119
 
            for char, opt in self._options.iteritems():
120
 
                if opt.char == response:
121
 
                    return self.__do_action(opt.action, item)
122
 
 
123
 
            self.__show_help()
124
 
 
125
 
    def __show_help(self):
126
 
        for char, opt in self._options.iteritems():
127
 
            print '  %s - %s' % (char, opt.help)
128
 
 
129
 
    if sys.platform == "win32":
130
 
        import msvcrt
131
 
        def __getchar(self):
132
 
            return msvcrt.getche()
133
 
    else:
134
 
        def __getchar(self):
135
 
            import tty
136
 
            import termios
137
 
            fd = sys.stdin.fileno()
138
 
            settings = termios.tcgetattr(fd)
139
 
            try:
140
 
                tty.setraw(fd)
141
 
                ch = sys.stdin.read(1)
142
 
            finally:
143
 
                termios.tcsetattr(fd, termios.TCSADRAIN, settings)
144
 
            return ch
 
3
from userinteractor import UserInteractor, UserOption
145
4
 
146
5
class HunkSelector:
147
6
    def __init__(self, patches):
274
133
        self.patches = tmp
275
134
 
276
135
        return self.patches
277