Skip to content

Commit 6e7fbee

Browse files
committed
Added settable called save_scripted_commands which determines whether to save commands
run in scripts and pyscripts to history.
1 parent 5bcb309 commit 6e7fbee

File tree

5 files changed

+65
-40
lines changed

5 files changed

+65
-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 -----
@@ -4950,7 +4949,13 @@ def _persist_history(self) -> None:
49504949
except OSError as ex:
49514950
self.perror(f"Cannot write persistent history file '{self.persistent_history_file}': {ex}")
49524951

4953-
def _generate_transcript(self, history: Union[List[HistoryItem], List[str]], transcript_file: str) -> None:
4952+
def _generate_transcript(
4953+
self,
4954+
history: Union[List[HistoryItem], List[str]],
4955+
transcript_file: str,
4956+
*,
4957+
add_to_history: bool = True,
4958+
) -> None:
49544959
"""Generate a transcript file from a given history of commands"""
49554960
self.last_result = False
49564961

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

50015006
# then run the command and let the output go into our buffer
50025007
try:
5003-
stop = self.onecmd_plus_hooks(history_item, raise_keyboard_interrupt=True)
5008+
stop = self.onecmd_plus_hooks(
5009+
history_item,
5010+
add_to_history=add_to_history,
5011+
raise_keyboard_interrupt=True,
5012+
)
50045013
except KeyboardInterrupt as ex:
50055014
self.perror(ex)
50065015
stop = True
@@ -5144,9 +5153,17 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
51445153

51455154
if args.transcript:
51465155
# self.last_resort will be set by _generate_transcript()
5147-
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
5156+
self._generate_transcript(
5157+
script_commands,
5158+
os.path.expanduser(args.transcript),
5159+
add_to_history=self.save_scripted_commands,
5160+
)
51485161
else:
5149-
stop = self.runcmds_plus_hooks(script_commands, stop_on_keyboard_interrupt=True)
5162+
stop = self.runcmds_plus_hooks(
5163+
script_commands,
5164+
add_to_history=self.save_scripted_commands,
5165+
stop_on_keyboard_interrupt=True,
5166+
)
51505167
self.last_result = True
51515168
return stop
51525169

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/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)