/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 breezy/ui/text.py

  • Committer: Breezy landing bot
  • Author(s): Martin
  • Date: 2018-03-24 17:25:27 UTC
  • mfrom: (6910.3.2 ui_enter)
  • Revision ID: breezy.the.bot@gmail.com-20180324172527-yczskdtrucxhpycd
Further work on sane ui factory interface and fix choose in char mode

Merged from https://code.launchpad.net/~gz/brz/ui_enter/+merge/342043

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
        variable is set to 'line-based', or if there is no controlling
66
66
        terminal.
67
67
        """
68
 
        if os.environ.get('BRZ_TEXTUI_INPUT') != 'line-based' and \
69
 
           self.ui.stdin == sys.stdin and self.ui.stdin.isatty():
 
68
        is_tty = self.ui.raw_stdin.isatty()
 
69
        if (os.environ.get('BRZ_TEXTUI_INPUT') != 'line-based' and
 
70
                self.ui.raw_stdin == sys.stdin and is_tty):
70
71
            self.line_based = False
71
72
            self.echo_back = True
72
73
        else:
73
74
            self.line_based = True
74
 
            self.echo_back = not self.ui.stdin.isatty()
 
75
            self.echo_back = not is_tty
75
76
 
76
77
    def _build_alternatives(self, msg, choices, default):
77
78
        """Parse choices string.
126
127
            raise KeyboardInterrupt
127
128
        if char == chr(4): # EOF (^d, C-d)
128
129
            raise EOFError
129
 
        return char.decode("ascii", "replace")
 
130
        if isinstance(char, bytes):
 
131
            return char.decode('ascii', 'replace')
 
132
        return char
130
133
 
131
134
    def interact(self):
132
135
        """Keep asking the user until a valid choice is made.
173
176
        self.stdin = stdin
174
177
        self.stdout = stdout
175
178
        self.stderr = stderr
 
179
        self._progress_view = NullProgressView()
 
180
 
 
181
    def __enter__(self):
 
182
        # Choose default encoding and handle py2/3 differences
 
183
        self._setup_streams()
176
184
        # paints progress, network activity, etc
177
185
        self._progress_view = self.make_progress_view()
 
186
        return self
 
187
 
 
188
    def _setup_streams(self):
 
189
        self.raw_stdin = _unwrap_stream(self.stdin)
 
190
        self.stdin = _wrap_in_stream(self.raw_stdin)
 
191
        self.raw_stdout = _unwrap_stream(self.stdout)
 
192
        self.stdout = _wrap_out_stream(self.raw_stdout)
 
193
        self.raw_stderr = _unwrap_stream(self.stderr)
 
194
        self.stderr = _wrap_out_stream(self.raw_stderr)
178
195
 
179
196
    def choose(self, msg, choices, default=None):
180
197
        """Prompt the user for a list of alternatives.
642
659
def _unwrap_stream(stream):
643
660
    inner = getattr(stream, "buffer", None)
644
661
    if inner is None:
645
 
        inner = getattr(stream, "stream", None)
 
662
        inner = getattr(stream, "stream", stream)
646
663
    return inner
647
664
 
648
665