/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/shelf_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-25 17:54:52 UTC
  • mfrom: (6303 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6321.
  • Revision ID: jelmer@samba.org-20111125175452-v0uwwxqcp97tzuzv
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
from cStringIO import StringIO
19
 
import os
20
19
import shutil
21
20
import sys
22
21
import tempfile
252
251
        diff_file.seek(0)
253
252
        return patches.parse_patch(diff_file)
254
253
 
255
 
    def _char_based(self):
256
 
        # FIXME: A bit hackish to use INSIDE_EMACS here, but there is another
257
 
        # work in progress moving this method (and more importantly prompt()
258
 
        # below) into the ui area and address the issue in better ways.
259
 
        # -- vila 2011-09-28
260
 
        return os.environ.get('INSIDE_EMACS', None) is None
261
 
 
262
 
    def prompt(self, message):
263
 
        """Prompt the user for a character.
264
 
 
265
 
        :param message: The message to prompt a user with.
266
 
        :return: A character.
267
 
        """
268
 
        char_based = self._char_based()
269
 
        if char_based and not sys.stdin.isatty():
270
 
            # Since there is no controlling terminal we will hang when
271
 
            # trying to prompt the user, better abort now.  See
272
 
            # https://code.launchpad.net/~bialix/bzr/shelve-no-tty/+merge/14905
273
 
            # for more context.
274
 
            raise errors.BzrError(gettext("You need a controlling terminal."))
275
 
        sys.stdout.write(message)
276
 
        if char_based:
277
 
            # We peek one char at a time which requires a real term here
278
 
            char = osutils.getchar()
279
 
        else:
280
 
            # While running tests (or under emacs) the input is line buffered
281
 
            # so we must not use osutils.getchar(). Instead we switch to a mode
282
 
            # where each line is terminated by a new line
283
 
            line = sys.stdin.readline()
284
 
            if line:
285
 
                # XXX: Warn if more than one char is typed ?
286
 
                char = line[0]
287
 
            else:
288
 
                # Empty input, callers handle it as enter
289
 
                char = ''
290
 
        sys.stdout.write("\r" + ' ' * len(message) + '\r')
291
 
        sys.stdout.flush()
292
 
        return char
293
 
 
294
 
    def prompt_bool(self, question, long=False, allow_editor=False):
 
254
    def prompt(self, message, choices, default):
 
255
        return ui.ui_factory.choose(message, choices, default=default)
 
256
 
 
257
    def prompt_bool(self, question, allow_editor=False):
295
258
        """Prompt the user with a yes/no question.
296
259
 
297
260
        This may be overridden by self.auto.  It may also *set* self.auto.  It
301
264
        """
302
265
        if self.auto:
303
266
            return True
304
 
        editor_string = ''
305
 
        if long:
306
 
            if allow_editor:
307
 
                editor_string = '(E)dit manually, '
308
 
            prompt = ' [(y)es, (N)o, %s(f)inish, or (q)uit]' % editor_string
 
267
        alternatives_chars = 'yn'
 
268
        alternatives = '&yes\n&No'
 
269
        if allow_editor:
 
270
            alternatives_chars += 'e'
 
271
            alternatives += '\n&edit manually'
 
272
        alternatives_chars += 'fq'
 
273
        alternatives += '\n&finish\n&quit'
 
274
        choice = self.prompt(question, alternatives, 1)
 
275
        if choice is None:
 
276
            # EOF.
 
277
            char = 'n'
309
278
        else:
310
 
            if allow_editor:
311
 
                editor_string = 'e'
312
 
            prompt = ' [yN%sfq?]' % editor_string
313
 
        char = self.prompt(question + prompt)
 
279
            char = alternatives_chars[choice]
314
280
        if char == 'y':
315
281
            return True
316
282
        elif char == 'e' and allow_editor:
318
284
        elif char == 'f':
319
285
            self.auto = True
320
286
            return True
321
 
        elif char == '?':
322
 
            return self.prompt_bool(question, long=True)
323
287
        if char == 'q':
324
288
            raise errors.UserAbort()
325
289
        else: