diff options
-rwxr-xr-x | cmd2/cmd2.py | 23 | ||||
-rw-r--r-- | tests/test_transcript.py | 3 | ||||
-rw-r--r-- | tests/transcripts/expected_history.txt | 8 |
3 files changed, 22 insertions, 12 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f4f30bd4..c9596edf 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3002,27 +3002,38 @@ a..b, a:b, a:, ..b items by indices (inclusive) except Exception as e: self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False) elif args.transcript: + membuf = io.StringIO() + # Make sure echo is on so commands print to standard out saved_echo = self.echo - self.echo = True + self.echo = False # Redirect stdout to the transcript file saved_self_stdout = self.stdout - self.stdout = open(args.transcript, 'w') + self.stdout = membuf # Run all of the commands in the history with output redirected to transcript and echo on - self.runcmds_plus_hooks(history) + for history_item in history: + # write the command to the output stream + first = True + for line in history_item.splitlines(): + if first: + self.stdout.write('{}{}\n'.format(self.prompt, line)) + first = False + else: + self.stdout.write('{}{}\n'.format(self.continuation_prompt, line)) + self.onecmd_plus_hooks(history_item) # Restore stdout to its original state - self.stdout.close() + #self.stdout.close() self.stdout = saved_self_stdout # Set echo back to its original state self.echo = saved_echo # Post-process the file to escape un-escaped "/" regex escapes - with open(args.transcript, 'r') as fin: - data = fin.read() + membuf.seek(0) + data = membuf.read() post_processed_data = data.replace('/', '\/') with open(args.transcript, 'w') as fout: fout.write(post_processed_data) diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 3a8eee66..6e2b1cac 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -130,8 +130,7 @@ def test_history_transcript(request, capsys): app = CmdLineApp() app.stdout = StdOut() run_cmd(app, 'help') - run_cmd(app, 'speak lots of wierd [ /tmp ]: chars?') - run_cmd(app, 'speak /this is not a regex/') + run_cmd(app, 'orate this is\na multiline\ncommand;\n') # Get location of the expected transcript test_dir = os.path.dirname(request.module.__file__) diff --git a/tests/transcripts/expected_history.txt b/tests/transcripts/expected_history.txt index cfd1409d..55eceb81 100644 --- a/tests/transcripts/expected_history.txt +++ b/tests/transcripts/expected_history.txt @@ -12,7 +12,7 @@ Documented commands (type help <topic>): alias help load orate pyscript say shell speak edit history mumble py quit set shortcuts unalias -(Cmd) speak lots of wierd [ /tmp ]: chars? -lots of wierd [ \/tmp ]: chars? -(Cmd) speak /this is not a regex/ -\/this is not a regex\/ +(Cmd) orate this is +> a multiline +> command; +this is a multiline command |