summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py38
1 files changed, 29 insertions, 9 deletions
diff --git a/cmd2.py b/cmd2.py
index 2b99a7ab..7fde6d14 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -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):