~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to userinteractor.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
#!/usr/bin/python
 
2
 
1
3
import sys
2
 
from bzrlib.osutils import getchar
3
 
 
4
4
 
5
5
class UserOption:
6
6
    def __init__(self, char, action, help, default=False):
39
39
        self.prompt = prompt
40
40
 
41
41
    def set_options(self, opt_list):
42
 
        self._options = []
43
 
        self._option_dict = {}
 
42
        self._options = {}
44
43
        for option in opt_list:
45
44
            self.add_option(option)
46
45
 
47
46
    def add_option(self, option):
48
 
        self._options.append(option)
49
 
        self._option_dict[option.char] = option
 
47
        self._options[option.char] = option
50
48
 
51
49
    def get_option(self, char):
52
 
        return self._option_dict[char]
 
50
        return self._options[char]
53
51
 
54
52
    def __do_action(self, action, item):
55
53
        if type(action) is int:
58
56
                self.__finished = True
59
57
            elif action == self.RESTART:
60
58
                self.__restart = True
61
 
                self.__finished = False
62
59
            elif action == self.FINISH:
63
60
                self.__finished = True
64
61
            return True
93
90
            if self.end_callback():
94
91
                break
95
92
 
96
 
            self.__finished = False
97
 
 
98
93
            self.__ask_once(None, self.__total_items)
99
94
            while not self.__finished and not self.__restart:
100
95
                self.__ask_once(None, -1)
107
102
        while True:
108
103
            sys.stdout.write(self.prompt % args)
109
104
            sys.stdout.write(' [')
110
 
            for opt in self._options:
 
105
            for char, opt in self._options.iteritems():
111
106
                if opt.default:
112
107
                    default = opt
113
108
                sys.stdout.write(opt.char)
114
109
            sys.stdout.write('?] (%s): ' % default.char)
115
110
 
116
 
            response = getchar()
 
111
            response = self.__getchar()
117
112
 
118
113
            # default, which we see as newline, is 'n'
119
114
            if response in ['\n', '\r', '\r\n']:
121
116
 
122
117
            print response # because echo is off
123
118
 
124
 
            for opt in self._options:
 
119
            for char, opt in self._options.iteritems():
125
120
                if opt.char == response:
126
121
                    return self.__do_action(opt.action, item)
127
122
 
128
123
            self.__show_help()
129
124
 
130
 
        return False # keep pychecker happy
131
 
 
132
125
    def __show_help(self):
133
 
        for opt in self._options:
134
 
            print '  %s - %s' % (opt.char, opt.help)
 
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