Skip to content

Commit

Permalink
Add 'history_includes_scripts' settable
Browse files Browse the repository at this point in the history
At least in most of my use cases, the individual commands of a script
executed by the command line should not end up in the history.  If I go
back in history, I want to find the commands I typed (like the
run_script), and not the commands contained in the script I executed,
or even the scripts that this script might execute itself.

This patch introduces a new settable 'history_includes_scripts'.  It is
kept as 'true' by default to preserve the legacy behavior of existing
cmd2 versions.  However, it can be set to 'false' to make run_script
not add to the history by default.  Individual run_script executions
can still use the --history option to make them override the default
and add to the history.

Signed-off-by: Harald Welte <[email protected]>
  • Loading branch information
laf0rge committed Jan 31, 2024
1 parent de90590 commit efd3eb0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def __init__(
self.feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing)
self.quiet = False # Do not suppress nonessential output
self.timing = False # Prints elapsed time for each command
self.history_includes_scripts = True # Should the history contain commands issued by scripts?

# The maximum number of CompletionItems to display during tab completion. If the number of completion
# suggestions exceeds this number, they will be displayed in the typical columnized format and will
Expand Down Expand Up @@ -1103,6 +1104,7 @@ def allow_style_type(value: str) -> ansi.AllowStyle:
)
self.add_settable(Settable('quiet', bool, "Don't print nonessential feedback", self))
self.add_settable(Settable('timing', bool, "Report execution times", self))
self.add_settable(Settable('history_includes_scripts', bool, "History should contain commands issued by scripts", self))

# ----- Methods related to presenting output to the user -----

Expand Down Expand Up @@ -5048,8 +5050,8 @@ def _current_script_dir(self) -> Optional[str]:
completer=path_complete,
)
run_script_parser.add_argument(
'--no-history',
action='store_true',
'--history',
action=argparse.BooleanOptionalAction,
help="Don't add commands issued by script to the history",
)
run_script_parser.add_argument('script_path', help="path to the script file", completer=path_complete)
Expand Down Expand Up @@ -5090,7 +5092,10 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
return None

orig_script_dir_count = len(self._script_dir)
add_to_history = not args.no_history
if args.history == None:
add_to_history = self.history_includes_scripts
else:
add_to_history = args.history

Check warning on line 5098 in cmd2/cmd2.py

View check run for this annotation

Codecov / codecov/patch

cmd2/cmd2.py#L5098

Added line #L5098 was not covered by tests

try:
self._script_dir.append(os.path.dirname(expanded_path))
Expand Down

0 comments on commit efd3eb0

Please sign in to comment.