diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-01-31 16:47:58 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-01-31 16:47:58 -0500 |
commit | 22aa56ba3c2e670d707bb4f96c90340c1b55964e (patch) | |
tree | 4e4808d3c10afa841f36a573b56d9a64ed9e55d4 /cmd2/cmd2.py | |
parent | 434a01f44e7d2302b4deef8f2e8069cbc26df560 (diff) | |
parent | 918200c02d392c17862fff81bbf58820ed15c725 (diff) | |
download | cmd2-git-22aa56ba3c2e670d707bb4f96c90340c1b55964e.tar.gz |
Merge branch 'master' into 2.0
# Conflicts:
# CHANGELOG.md
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 27746323..e1d15131 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -4061,36 +4061,11 @@ class Cmd(cmd.Cmd): readline.clear_history() return - # If an argument was supplied, then retrieve partial contents of the history - cowardly_refuse_to_run = False - if args.arg: - # If a character indicating a slice is present, retrieve - # a slice of the history - arg = args.arg - arg_is_int = False - try: - int(arg) - arg_is_int = True - except ValueError: - pass - - if '..' in arg or ':' in arg: - # Get a slice of history - history = self.history.span(arg, args.all) - elif arg_is_int: - history = [self.history.get(arg)] - elif arg.startswith(r'/') and arg.endswith(r'/'): - history = self.history.regex_search(arg, args.all) - else: - history = self.history.str_search(arg, args.all) - else: - # If no arg given, then retrieve the entire history - cowardly_refuse_to_run = True - # Get a copy of the history so it doesn't get mutated while we are using it - history = self.history.span(':', args.all) + # If an argument was supplied, then retrieve partial contents of the history, otherwise retrieve it all + history = self._get_history(args) if args.run: - if cowardly_refuse_to_run: + if not args.arg: self.perror("Cowardly refusing to run all previously entered commands.") self.perror("If this is what you want to do, specify '1:' as the range of history.") else: @@ -4130,6 +4105,32 @@ class Cmd(cmd.Cmd): for hi in history: self.poutput(hi.pr(script=args.script, expanded=args.expanded, verbose=args.verbose)) + def _get_history(self, args: argparse.Namespace) -> List[HistoryItem]: + """If an argument was supplied, then retrieve partial contents of the history; otherwise retrieve entire history.""" + if args.arg: + # If a character indicating a slice is present, retrieve a slice of the history + arg = args.arg + arg_is_int = False + try: + int(arg) + arg_is_int = True + except ValueError: + pass + + if '..' in arg or ':' in arg: + # Get a slice of history + history = self.history.span(arg, args.all) + elif arg_is_int: + history = [self.history.get(arg)] + elif arg.startswith(r'/') and arg.endswith(r'/'): + history = self.history.regex_search(arg, args.all) + else: + history = self.history.str_search(arg, args.all) + else: + # Get a copy of the history so it doesn't get mutated while we are using it + history = self.history.span(':', args.all) + return history + def _initialize_history(self, hist_file): """Initialize history using history related attributes |