~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to userinteractor.py

  • Committer: Aaron Bentley
  • Date: 2006-03-10 20:15:17 UTC
  • mfrom: (147.1.73 bzrtools)
  • Revision ID: abentley@panoramicfeedback.com-20060310201517-958f8d9307a49b2e
Merged the Arch-1 bzrtools import

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
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
 
        self._option_dict = {}
44
 
        for option in opt_list:
45
 
            self.add_option(option)
46
 
 
47
 
    def add_option(self, option):
48
 
        self._options.append(option)
49
 
        self._option_dict[option.char] = option
50
 
 
51
 
    def get_option(self, char):
52
 
        return self._option_dict[char]
53
 
 
54
 
    def __do_action(self, action, item):
55
 
        if type(action) is int:
56
 
            if action == self.QUIT:
57
 
                self.__quit = True
58
 
                self.__finished = True
59
 
            elif action == self.RESTART:
60
 
                self.__restart = True
61
 
            elif action == self.FINISH:
62
 
                self.__finished = True
63
 
            return True
64
 
        else:
65
 
            return action(item)
66
 
 
67
 
    def __select_loop(self):
68
 
        i = 0
69
 
        self.start_callback()
70
 
        while i < len(self.items):
71
 
                item = self.items[i]
72
 
 
73
 
                self.item_callback(item, i + 1)
74
 
 
75
 
                if self.__ask_once(item, i + 1):
76
 
                    i += 1
77
 
 
78
 
                if self.__quit or self.__finished:
79
 
                    break
80
 
 
81
 
    def interact(self):
82
 
        self.__quit = False
83
 
        self.__finished = False
84
 
 
85
 
        while not self.__quit and not self.__finished:
86
 
            self.__restart = False
87
 
 
88
 
            self.__select_loop()
89
 
            if self.__quit:
90
 
                break
91
 
 
92
 
            if self.end_callback():
93
 
                break
94
 
 
95
 
            self.__ask_once(None, self.__total_items)
96
 
            while not self.__finished and not self.__restart:
97
 
                self.__ask_once(None, -1)
98
 
 
99
 
        return not self.__quit
100
 
 
101
 
    def __ask_once(self, item, count):
102
 
        args = {'count': count, 'total' : self.__total_items}
103
 
 
104
 
        while True:
105
 
            sys.stdout.write(self.prompt % args)
106
 
            sys.stdout.write(' [')
107
 
            for opt in self._options:
108
 
                if opt.default:
109
 
                    default = opt
110
 
                sys.stdout.write(opt.char)
111
 
            sys.stdout.write('?] (%s): ' % default.char)
112
 
 
113
 
            response = self.__getchar()
114
 
 
115
 
            # default, which we see as newline, is 'n'
116
 
            if response in ['\n', '\r', '\r\n']:
117
 
                response = default.char
118
 
 
119
 
            print response # because echo is off
120
 
 
121
 
            for opt in self._options:
122
 
                if opt.char == response:
123
 
                    return self.__do_action(opt.action, item)
124
 
 
125
 
            self.__show_help()
126
 
 
127
 
    def __show_help(self):
128
 
        for opt in self._options:
129
 
            print '  %s - %s' % (opt.char, opt.help)
130
 
 
131
 
    if sys.platform == "win32":
132
 
        import msvcrt
133
 
        def __getchar(self):
134
 
            return msvcrt.getche()
135
 
    else:
136
 
        def __getchar(self):
137
 
            import tty
138
 
            import termios
139
 
            fd = sys.stdin.fileno()
140
 
            settings = termios.tcgetattr(fd)
141
 
            try:
142
 
                tty.setraw(fd)
143
 
                ch = sys.stdin.read(1)
144
 
            finally:
145
 
                termios.tcsetattr(fd, termios.TCSADRAIN, settings)
146
 
            return ch