Skip to content

Commit bf72952

Browse files
committed
Fix #384, multiline commands now appear properly in transcripts
1 parent dedd045 commit bf72952

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

cmd2/cmd2.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,27 +3002,38 @@ def do_history(self, args):
30023002
except Exception as e:
30033003
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
30043004
elif args.transcript:
3005+
membuf = io.StringIO()
3006+
30053007
# Make sure echo is on so commands print to standard out
30063008
saved_echo = self.echo
3007-
self.echo = True
3009+
self.echo = False
30083010

30093011
# Redirect stdout to the transcript file
30103012
saved_self_stdout = self.stdout
3011-
self.stdout = open(args.transcript, 'w')
3013+
self.stdout = membuf
30123014

30133015
# Run all of the commands in the history with output redirected to transcript and echo on
3014-
self.runcmds_plus_hooks(history)
3016+
for history_item in history:
3017+
# write the command to the output stream
3018+
first = True
3019+
for line in history_item.splitlines():
3020+
if first:
3021+
self.stdout.write('{}{}\n'.format(self.prompt, line))
3022+
first = False
3023+
else:
3024+
self.stdout.write('{}{}\n'.format(self.continuation_prompt, line))
3025+
self.onecmd_plus_hooks(history_item)
30153026

30163027
# Restore stdout to its original state
3017-
self.stdout.close()
3028+
#self.stdout.close()
30183029
self.stdout = saved_self_stdout
30193030

30203031
# Set echo back to its original state
30213032
self.echo = saved_echo
30223033

30233034
# Post-process the file to escape un-escaped "/" regex escapes
3024-
with open(args.transcript, 'r') as fin:
3025-
data = fin.read()
3035+
membuf.seek(0)
3036+
data = membuf.read()
30263037
post_processed_data = data.replace('/', '\/')
30273038
with open(args.transcript, 'w') as fout:
30283039
fout.write(post_processed_data)

tests/test_transcript.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ def test_history_transcript(request, capsys):
130130
app = CmdLineApp()
131131
app.stdout = StdOut()
132132
run_cmd(app, 'help')
133-
run_cmd(app, 'speak lots of wierd [ /tmp ]: chars?')
134-
run_cmd(app, 'speak /this is not a regex/')
133+
run_cmd(app, 'orate this is\na multiline\ncommand;\n')
135134

136135
# Get location of the expected transcript
137136
test_dir = os.path.dirname(request.module.__file__)

tests/transcripts/expected_history.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Documented commands (type help <topic>):
1212
alias help load orate pyscript say shell speak
1313
edit history mumble py quit set shortcuts unalias
1414

15-
(Cmd) speak lots of wierd [ /tmp ]: chars?
16-
lots of wierd [ \/tmp ]: chars?
17-
(Cmd) speak /this is not a regex/
18-
\/this is not a regex\/
15+
(Cmd) orate this is
16+
> a multiline
17+
> command;
18+
this is a multiline command

0 commit comments

Comments
 (0)