~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Robert Collins
  • Date: 2006-05-04 08:39:32 UTC
  • mto: (1697.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1701.
  • Revision ID: robertc@robertcollins.net-20060504083932-009bfa89993005aa
Add bzrlib.ui.ui_factory.get_boolean().

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
displays no output.
29
29
"""
30
30
 
 
31
import sys
31
32
 
32
33
import bzrlib.progress
33
34
from bzrlib.symbol_versioning import *
76
77
        cursor at the leftmost position."""
77
78
        raise NotImplementedError(self.clear_term)
78
79
 
79
 
 
80
 
class SilentUIFactory(UIFactory):
 
80
    def get_boolean(self, prompt):
 
81
        """Get a boolean question answered from the user. 
 
82
 
 
83
        :param prompt: a message to prompt the user with. Should be a single
 
84
        line without terminating \n.
 
85
        :return: True or False for y/yes or n/no.
 
86
        """
 
87
        raise NotImplementedError(self.get_boolean)
 
88
 
 
89
 
 
90
class CLIUIFactory(UIFactory):
 
91
    """Common behaviour for command line UI factories."""
 
92
 
 
93
    def __init__(self):
 
94
        super(CLIUIFactory, self).__init__()
 
95
        self.stdin = sys.stdin
 
96
 
 
97
    def get_boolean(self, prompt):
 
98
        self.clear_term()
 
99
        # FIXME: make a regexp and handle case variations as well.
 
100
        while True:
 
101
            self.prompt(prompt)
 
102
            line = self.stdin.readline()
 
103
            if line in ('y\n', 'yes\n'):
 
104
                return True
 
105
            if line in ('n\n', 'no\n'):
 
106
                return False
 
107
 
 
108
    def prompt(self, prompt):
 
109
        """Emit prompt on the CLI."""
 
110
 
 
111
 
 
112
class SilentUIFactory(CLIUIFactory):
81
113
    """A UI Factory which never prints anything.
82
114
 
83
115
    This is the default UI, if another one is never registered.