diff options
| author | Jared Crapo <jared@kotfu.net> | 2017-08-23 15:40:57 -0600 |
|---|---|---|
| committer | Jared Crapo <jared@kotfu.net> | 2017-08-23 15:40:57 -0600 |
| commit | dde0b697b15cda29538251467c0f384c819778af (patch) | |
| tree | e59dcce6a9bd4afc95725057ebeed52f0b89f885 /cmd2.py | |
| parent | 64e19334cf6aac8edeecf8e7f8725d00da758ee5 (diff) | |
| download | cmd2-git-dde0b697b15cda29538251467c0f384c819778af.tar.gz | |
Prompt and line now properly echoed on tty and pipe
if we are on a pipe, we have to echo the prompt only after we read and are not at EOF.
Diffstat (limited to 'cmd2.py')
| -rwxr-xr-x | cmd2.py | 38 |
1 files changed, 29 insertions, 9 deletions
@@ -970,25 +970,45 @@ class Cmd(cmd.Cmd): return result def pseudo_raw_input(self, prompt): - """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout""" + """ + began life as a copy of cmd's cmdloop; like raw_input but + + - accounts for changed stdin, stdout + - if input is a pipe (instead of a tty), look at self.echo + to decide whether to print the prompt and the input + """ # Deal with the vagaries of readline and ANSI escape codes safe_prompt = self._surround_ansi_escapes(prompt) if self.use_rawinput: try: - line = sm.input(safe_prompt) + if sys.stdin.isatty(): + sys.stdout.write(safe_prompt) + line = sm.input() + else: + line = sm.input() + if self.echo: + sys.stdout.write('{}{}\n'.format(safe_prompt,line)) except EOFError: line = 'eof' else: - self.poutput(safe_prompt, end='') - self.stdout.flush() - line = self.stdin.readline() - if not len(line): - line = 'eof' + if self.stdin.isatty(): + # on a tty, print the prompt first, then read the line + self.poutput(safe_prompt, end='') + self.stdout.flush() + line = self.stdin.readline() else: - line = line.rstrip('\r\n') - + # we are reading from a pipe, read the line to see if there is + # anything there, if so, then decide whether to print the + # prompt or not + line = self.stdin.readline() + if len(line): + # we read something, output the prompt and the something + if self.echo: + self.poutput('{}{}'.format(safe_prompt, line)) + else: + line = 'eof' return line.strip() def _cmdloop(self): |
