diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-03 10:59:06 -0400 |
|---|---|---|
| committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-03 10:59:06 -0400 |
| commit | ad3d32339f791faa31763fe3efb42d0be469ddef (patch) | |
| tree | 8bedc45871f10cfb1973aadd15abab9d2562c902 | |
| parent | a0d829e4105cd431198feb43534adaf953b090ba (diff) | |
| download | cmd2-git-ad3d32339f791faa31763fe3efb42d0be469ddef.tar.gz | |
Removed redundant list command
The extra functionality of listing a span (slice) of history items present in the list command has been incorporated into the existing history command and the list command has been removed.
Unit tests have been updated accordingly.
In the process a bug was fixed that was causing a crash if and end was supplied to the span but not a start.
| -rwxr-xr-x | cmd2.py | 49 | ||||
| -rw-r--r-- | tests/conftest.py | 7 | ||||
| -rw-r--r-- | tests/test_cmd2.py | 44 | ||||
| -rw-r--r-- | tests/test_transcript.py | 5 | ||||
| -rw-r--r-- | tests/transcript.txt | 5 |
5 files changed, 51 insertions, 59 deletions
@@ -1592,7 +1592,8 @@ class Cmd(cmd.Cmd): | no arg: list all | arg is integer: list one history item, by index - | arg is string: string search + | a..b, a:b, a:, ..b -> list history items by a span of indices (inclusive) + | arg is string: list all commands matching string search | arg is /enclosed in forward-slashes/: regular expression search """ # If arguments are being passed as a list instead of as a string @@ -1602,10 +1603,20 @@ class Cmd(cmd.Cmd): else: arg = '' + # If an argument was supplied, then retrieve partial contents of the history if arg: - history = self.history.get(arg) + # If a character indicating a slice is present, retrieve a slice of the history + if '..' in arg or ':' in arg: + # Get a slice of history + history = self.history.span(arg) + else: + # Get item(s) from history by index or string search + history = self.history.get(arg) else: + # If no arg given, then retrieve the entire history history = self.history + + # Display the history items retrieved for hi in history: if opts.script: self.poutput(hi) @@ -1628,38 +1639,6 @@ class Cmd(cmd.Cmd): except IndexError: return None - def do_list(self, arg): - """list [arg]: lists command(s) from history in a flexible/searchable way. - - :param arg: str - behavior varies as follows: - - * no arg -> list most recent command - * arg is integer -> list one history item, by index - * a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end) - * arg is string -> list all commands matching string search - * arg is /enclosed in forward-slashes/ -> regular expression search - """ - try: - history = self.history.span(arg or '-1') - except IndexError: - history = self.history.search(arg) - for hi in history: - self.poutput(hi.pr()) - - def help_list(self): - """Print help for do_list().""" - help_str = """Lists command(s) from history in a flexible/searchable way. - - Usage: list [arg] - - Where arg is: - no arg -> list most recent command - arg is integer -> list one history item, by index - a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end) - arg is string -> list all commands matching string search - arg is /enclosed in forward-slashes/ -> regular expression search""" - self.stdout.write("{}\n".format(help_str)) - def do_edit(self, arg): """Edit a file or command in a text editor. @@ -2170,7 +2149,7 @@ class History(list): raise IndexError if not results.group('separator'): return [self[self._to_index(results.group('start'))]] - start = self._to_index(results.group('start')) + start = self._to_index(results.group('start')) or 0 # Ensure start is not None end = self._to_index(results.group('end')) reverse = False if end is not None: diff --git a/tests/conftest.py b/tests/conftest.py index 3977de5f..41bd007a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,8 +15,8 @@ import cmd2 # Help text for base cmd2.Cmd application BASE_HELP = """Documented commands (type help <topic>): ======================================== -_relative_load edit history load py run set shortcuts -cmdenvironment help list pause quit save shell show +_relative_load edit history pause quit save shell show +cmdenvironment help load py run set shortcuts """ # Help text for the history command @@ -24,7 +24,8 @@ HELP_HISTORY = """history [arg]: lists past commands issued | no arg: list all | arg is integer: list one history item, by index - | arg is string: string search + | a..b, a:b, a:, ..b -> list history items by a span of indices (inclusive) + | arg is string: list all commands matching string search | arg is /enclosed in forward-slashes/: regular expression search Usage: history [options] (limit on which commands to include) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index ce47ef47..7d60110e 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -142,48 +142,62 @@ shortcuts """) assert out == expected - -def test_base_list(base_app): +def test_history_with_string_argument(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') - out = run_cmd(base_app, 'list') + run_cmd(base_app, 'help history') + out = run_cmd(base_app, 'history help') expected = normalize(""" --------------------------[2] -shortcuts +-------------------------[1] +help +-------------------------[3] +help history """) assert out == expected -def test_list_with_string_argument(base_app): +def test_history_with_integer_argument(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') - run_cmd(base_app, 'help list') - out = run_cmd(base_app, 'list help') + out = run_cmd(base_app, 'history 1') expected = normalize(""" -------------------------[1] help --------------------------[3] -help list """) assert out == expected -def test_list_with_integer_argument(base_app): +def test_history_with_integer_span(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') - out = run_cmd(base_app, 'list 1') + run_cmd(base_app, 'help history') + out = run_cmd(base_app, 'history 1..2') expected = normalize(""" -------------------------[1] help +-------------------------[2] +shortcuts """) assert out == expected +def test_history_with_span_start(base_app): + run_cmd(base_app, 'help') + run_cmd(base_app, 'shortcuts') + run_cmd(base_app, 'help history') + out = run_cmd(base_app, 'history 2:') + expected = normalize(""" +-------------------------[2] +shortcuts +-------------------------[3] +help history +""") + assert out == expected -def test_list_with_integer_span(base_app): +def test_history_with_span_end(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') - run_cmd(base_app, 'help list') - out = run_cmd(base_app, 'list 1..2') + run_cmd(base_app, 'help history') + out = run_cmd(base_app, 'history :2') expected = normalize(""" -------------------------[1] help diff --git a/tests/test_transcript.py b/tests/test_transcript.py index c31b519d..03fec92a 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -106,9 +106,8 @@ def test_base_with_transcript(_cmdline_app): Documented commands (type help <topic>): ======================================== -_relative_load help load py save shell speak -cmdenvironment history orate quit say shortcuts -edit list pause run set show +_relative_load edit history orate py run say shell show +cmdenvironment help load pause quit save set shortcuts speak (Cmd) help say Repeats what you tell me to. diff --git a/tests/transcript.txt b/tests/transcript.txt index 013e2d0f..d0fd86a6 100644 --- a/tests/transcript.txt +++ b/tests/transcript.txt @@ -2,9 +2,8 @@ Documented commands (type help <topic>): ======================================== -_relative_load help load py save shell speak -cmdenvironment history orate quit say shortcuts -edit list pause run set show +_relative_load edit history orate py run say shell show +cmdenvironment help load pause quit save set shortcuts speak (Cmd) help say Repeats what you tell me to. |
