Skip to content

Commit 58f0f42

Browse files
committed
Added settable called save_scripted_commands which determines whether to save commands
run in scripts and pyscripts to history.
1 parent 6960092 commit 58f0f42

File tree

7 files changed

+107
-40
lines changed

7 files changed

+107
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
add output to the operating system clipboard
1111
* Updated unit tests to be Python 3.12 compliant.
1212
* Fall back to bz2 compression of history file when lzma is not installed.
13+
* Added settable called `save_scripted_commands` which determines whether to save commands
14+
run in scripts and pyscripts to history.
1315
* Deletions (potentially breaking changes)
1416
* Removed `apply_style` from `Cmd.pwarning()`.
1517

cmd2/cmd2.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def __init__(
322322
self.editor = Cmd.DEFAULT_EDITOR
323323
self.feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing)
324324
self.quiet = False # Do not suppress nonessential output
325+
self.save_scripted_commands = True # Save commands run in scripts and pyscripts to history
325326
self.timing = False # Prints elapsed time for each command
326327

327328
# The maximum number of CompletionItems to display during tab completion. If the number of completion
@@ -1084,12 +1085,7 @@ def allow_style_type(value: str) -> ansi.AllowStyle:
10841085
)
10851086

10861087
self.add_settable(
1087-
Settable(
1088-
'always_show_hint',
1089-
bool,
1090-
'Display tab completion hint even when completion suggestions print',
1091-
self,
1092-
)
1088+
Settable('always_show_hint', bool, 'Display tab completion hint even when completion suggestions print', self)
10931089
)
10941090
self.add_settable(Settable('debug', bool, "Show full traceback on exception", self))
10951091
self.add_settable(Settable('echo', bool, "Echo command issued into output", self))
@@ -1099,6 +1095,9 @@ def allow_style_type(value: str) -> ansi.AllowStyle:
10991095
Settable('max_completion_items', int, "Maximum number of CompletionItems to display during tab completion", self)
11001096
)
11011097
self.add_settable(Settable('quiet', bool, "Don't print nonessential feedback", self))
1098+
self.add_settable(
1099+
Settable('save_scripted_commands', bool, 'Save commands run in scripts and pyscripts to history', self)
1100+
)
11021101
self.add_settable(Settable('timing', bool, "Report execution times", self))
11031102

11041103
# ----- Methods related to presenting output to the user -----
@@ -4955,7 +4954,13 @@ def _persist_history(self) -> None:
49554954
except OSError as ex:
49564955
self.perror(f"Cannot write persistent history file '{self.persistent_history_file}': {ex}")
49574956

4958-
def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], transcript_file: str) -> None:
4957+
def _generate_transcript(
4958+
self,
4959+
history: Union[List[HistoryItem], List[str]],
4960+
transcript_file: str,
4961+
*,
4962+
add_to_history: bool = True,
4963+
) -> None:
49594964
"""Generate a transcript file from a given history of commands"""
49604965
self.last_result = False
49614966

@@ -5005,7 +5010,11 @@ def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], tra
50055010

50065011
# then run the command and let the output go into our buffer
50075012
try:
5008-
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True)
5013+
stop = self.onecmd_plus_hooks(
5014+
history_item,
5015+
add_to_history=add_to_history,
5016+
raise_keyboard_interrupt=True,
5017+
)
50095018
except KeyboardInterrupt as ex:
50105019
self.perror(ex)
50115020
stop = True
@@ -5149,9 +5158,17 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
51495158

51505159
if args.transcript:
51515160
# self.last_resort will be set by _generate_transcript()
5152-
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
5161+
self._generate_transcript(
5162+
script_commands,
5163+
os.path.expanduser(args.transcript),
5164+
add_to_history=self.save_scripted_commands,
5165+
)
51535166
else:
5154-
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True)
5167+
stop = self.runcmds_plus_hooks(
5168+
script_commands,
5169+
add_to_history=self.save_scripted_commands,
5170+
stop_on_keyboard_interrupt=True,
5171+
)
51555172
self.last_result = True
51565173
return stop
51575174

cmd2/py_bridge.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ def __call__(self, command: str, *, echo: Optional[bool] = None) -> CommandResul
126126
self._cmd2_app.stdout = cast(TextIO, copy_cmd_stdout)
127127
with redirect_stdout(cast(IO[str], copy_cmd_stdout)):
128128
with redirect_stderr(cast(IO[str], copy_stderr)):
129-
stop = self._cmd2_app.onecmd_plus_hooks(command, py_bridge_call=True)
129+
stop = self._cmd2_app.onecmd_plus_hooks(
130+
command,
131+
add_to_history=self._cmd2_app.save_scripted_commands,
132+
py_bridge_call=True,
133+
)
130134
finally:
131135
with self._cmd2_app.sigint_protection:
132136
self._cmd2_app.stdout = cast(IO[str], copy_cmd_stdout.inner_stream)

tests/conftest.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,21 @@ def verify_help_text(
9898

9999
# Output from the set command
100100
SET_TXT = (
101-
"Name Value Description \n"
102-
"==================================================================================================================\n"
103-
"allow_style Terminal Allow ANSI text style sequences in output (valid values: \n"
104-
" Always, Never, Terminal) \n"
105-
"always_show_hint False Display tab completion hint even when completion suggestions\n"
106-
" print \n"
107-
"debug False Show full traceback on exception \n"
108-
"echo False Echo command issued into output \n"
109-
"editor vim Program used by 'edit' \n"
110-
"feedback_to_output False Include nonessentials in '|', '>' results \n"
111-
"max_completion_items 50 Maximum number of CompletionItems to display during tab \n"
112-
" completion \n"
113-
"quiet False Don't print nonessential feedback \n"
114-
"timing False Report execution times \n"
101+
"Name Value Description \n"
102+
"====================================================================================================================\n"
103+
"allow_style Terminal Allow ANSI text style sequences in output (valid values: \n"
104+
" Always, Never, Terminal) \n"
105+
"always_show_hint False Display tab completion hint even when completion suggestions\n"
106+
" print \n"
107+
"debug False Show full traceback on exception \n"
108+
"echo False Echo command issued into output \n"
109+
"editor vim Program used by 'edit' \n"
110+
"feedback_to_output False Include nonessentials in '|', '>' results \n"
111+
"max_completion_items 50 Maximum number of CompletionItems to display during tab \n"
112+
" completion \n"
113+
"quiet False Don't print nonessential feedback \n"
114+
"save_scripted_commands True Save commands run in scripts and pyscripts to history \n"
115+
"timing False Report execution times \n"
115116
)
116117

117118

tests/test_cmd2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,27 @@ def test_run_script_with_utf8_file(base_app, request):
442442
assert script_err == manual_err
443443

444444

445+
def test_run_script_save_scripted_commands(base_app, request):
446+
test_dir = os.path.dirname(request.module.__file__)
447+
filename = os.path.join(test_dir, 'scripts', 'help.txt')
448+
command = f'run_script {filename}'
449+
450+
# Save scripted commands
451+
base_app.save_scripted_commands = True
452+
base_app.history.clear()
453+
run_cmd(base_app, command)
454+
assert len(base_app.history) == 2
455+
assert base_app.history.get(1).raw == command
456+
assert base_app.history.get(2).raw == 'help -v'
457+
458+
# Do not save scripted commands
459+
base_app.save_scripted_commands = False
460+
base_app.history.clear()
461+
run_cmd(base_app, command)
462+
assert len(base_app.history) == 1
463+
assert base_app.history.get(1).raw == command
464+
465+
445466
def test_run_script_nested_run_scripts(base_app, request):
446467
# Verify that running a script with nested run_script commands works correctly,
447468
# and runs the nested script commands in the correct order.

tests/test_run_pyscript.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ def test_run_pyscript_help(base_app, request):
107107
assert out1 and out1 == out2
108108

109109

110+
def test_run_pyscript_save_scripted_commands(base_app, request):
111+
test_dir = os.path.dirname(request.module.__file__)
112+
python_script = os.path.join(test_dir, 'pyscript', 'help.py')
113+
command = f'run_pyscript {python_script}'
114+
115+
# Save scripted commands
116+
base_app.save_scripted_commands = True
117+
base_app.history.clear()
118+
run_cmd(base_app, command)
119+
assert len(base_app.history) == 2
120+
assert base_app.history.get(1).raw == command
121+
assert base_app.history.get(2).raw == 'help'
122+
123+
# Do not save scripted commands
124+
base_app.save_scripted_commands = False
125+
base_app.history.clear()
126+
run_cmd(base_app, command)
127+
assert len(base_app.history) == 1
128+
assert base_app.history.get(1).raw == command
129+
130+
110131
def test_run_pyscript_dir(base_app, request):
111132
test_dir = os.path.dirname(request.module.__file__)
112133
python_script = os.path.join(test_dir, 'pyscript', 'pyscript_dir.py')

tests/transcripts/regex_set.txt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ now: 'Terminal'
1010
editor - was: '/.*/'
1111
now: 'vim'
1212
(Cmd) set
13-
Name Value Description/ +/
14-
==================================================================================================================
15-
allow_style Terminal Allow ANSI text style sequences in output (valid values:/ +/
16-
Always, Never, Terminal)/ +/
17-
always_show_hint False Display tab completion hint even when completion suggestions
18-
print/ +/
19-
debug False Show full traceback on exception/ +/
20-
echo False Echo command issued into output/ +/
21-
editor vim Program used by 'edit'/ +/
22-
feedback_to_output False Include nonessentials in '|', '>' results/ +/
23-
max_completion_items 50 Maximum number of CompletionItems to display during tab/ +/
24-
completion/ +/
25-
maxrepeats 3 Max number of `--repeat`s allowed/ +/
26-
quiet False Don't print nonessential feedback/ +/
27-
timing False Report execution times/ +/
13+
Name Value Description/ +/
14+
====================================================================================================================
15+
allow_style Terminal Allow ANSI text style sequences in output (valid values:/ +/
16+
Always, Never, Terminal)/ +/
17+
always_show_hint False Display tab completion hint even when completion suggestions
18+
print/ +/
19+
debug False Show full traceback on exception/ +/
20+
echo False Echo command issued into output/ +/
21+
editor vim Program used by 'edit'/ +/
22+
feedback_to_output False Include nonessentials in '|', '>' results/ +/
23+
max_completion_items 50 Maximum number of CompletionItems to display during tab/ +/
24+
completion/ +/
25+
maxrepeats 3 Max number of `--repeat`s allowed/ +/
26+
quiet False Don't print nonessential feedback/ +/
27+
save_scripted_commands True Save commands run in scripts and pyscripts to history/ +/
28+
timing False Report execution times/ +/

0 commit comments

Comments
 (0)