Skip to content

Commit 009cb87

Browse files
committed
Potential fixes for outstanding multi-line issues in history command
1 parent 46df1c1 commit 009cb87

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

cmd2/cmd2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class Cmd(cmd.Cmd):
302302
# Attributes used to configure the StatementParser, best not to change these at runtime
303303
multiline_commands = []
304304
shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
305-
terminators = [';']
305+
terminators = [constants.MULTILINE_TERMINATOR]
306306

307307
# Attributes which are NOT dynamically settable at runtime
308308
allow_cli_args = True # Should arguments passed on the command-line be processed as commands?
@@ -3256,7 +3256,10 @@ def do_history(self, args: argparse.Namespace) -> None:
32563256
fd, fname = tempfile.mkstemp(suffix='.txt', text=True)
32573257
with os.fdopen(fd, 'w') as fobj:
32583258
for command in history:
3259-
fobj.write('{}\n'.format(command))
3259+
if command.statement.multiline_command:
3260+
fobj.write('{}\n'.format(command.expanded.rstrip()))
3261+
else:
3262+
fobj.write('{}\n'.format(command))
32603263
try:
32613264
self.do_edit(fname)
32623265
self.do_load(fname)
@@ -3268,7 +3271,10 @@ def do_history(self, args: argparse.Namespace) -> None:
32683271
try:
32693272
with open(os.path.expanduser(args.output_file), 'w') as fobj:
32703273
for command in history:
3271-
fobj.write('{}\n'.format(command))
3274+
if command.statement.multiline_command:
3275+
fobj.write('{}\n'.format(command.expanded.rstrip()))
3276+
else:
3277+
fobj.write('{}\n'.format(command))
32723278
plural = 's' if len(history) > 1 else ''
32733279
self.pfeedback('{} command{} saved to {}'.format(len(history), plural, args.output_file))
32743280
except Exception as e:

cmd2/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
REDIRECTION_APPEND = '>>'
1313
REDIRECTION_CHARS = [REDIRECTION_PIPE, REDIRECTION_OUTPUT]
1414
REDIRECTION_TOKENS = [REDIRECTION_PIPE, REDIRECTION_OUTPUT, REDIRECTION_APPEND]
15+
MULTILINE_TERMINATOR = ';'
1516

1617
# Regular expression to match ANSI escape codes
1718
ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m')

cmd2/history.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def pr(self, script=False, expanded=False, verbose=False) -> str:
4646
else:
4747
if script:
4848
# display without entry numbers
49-
if expanded:
49+
if expanded or self.statement.multiline_command:
5050
ret_str = self.expanded.rstrip()
5151
else:
5252
ret_str = str(self)
5353
else:
5454
# display a numbered list
55-
if expanded:
55+
if expanded or self.statement.multiline_command:
5656
ret_str = self.listformat.format(self.idx, self.expanded.rstrip())
5757
else:
5858
ret_str = self.listformat.format(self.idx, str(self).rstrip())

cmd2/parsing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def command_and_args(self) -> str:
191191
def expanded_command_line(self) -> str:
192192
"""Contains command_and_args plus any ending terminator, suffix, and redirection chars"""
193193
rtn = self.command_and_args
194-
if self.terminator:
195-
rtn += self.terminator
194+
if self.multiline_command:
195+
rtn += constants.MULTILINE_TERMINATOR
196196

197197
if self.suffix:
198198
rtn += ' ' + self.suffix
@@ -240,7 +240,7 @@ def __init__(
240240
):
241241
self.allow_redirection = allow_redirection
242242
if terminators is None:
243-
self.terminators = [';']
243+
self.terminators = [constants.MULTILINE_TERMINATOR]
244244
else:
245245
self.terminators = terminators
246246
if multiline_commands is None:

0 commit comments

Comments
 (0)