Skip to content

Commit a4ff1a4

Browse files
committed
Allow both history indices to be negative
1 parent 0ff0dfe commit a4ff1a4

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

cmd2/history.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ def span(self, span: str) -> List[HistoryItem]:
167167
end = int(end)
168168

169169
if start is not None and end is not None:
170-
# we have both start and end, return a slice of history, unless both are negative
171-
if start < 0 and end < 0:
172-
raise ValueError
170+
# we have both start and end, return a slice of history
173171
result = self[start:end]
174172
elif start is not None and sep is not None:
175173
# take a slice of the array

docs/freefeatures.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ last number, it will continue to the end::
310310
3 alias create three !echo three
311311
4 alias create four !echo four
312312

313-
You can use negative numbers as either the first or second number of the range
314-
(but not both). If you want to display the last three commands entered::
313+
If you want to display the last three commands entered::
315314

316315
(Cmd) history -- -3:
317316
2 alias create two !echo two

tests/test_history.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ def test_history_class_span(hist):
7777

7878
assert hist.span('1..3') == ['first', 'second', 'third']
7979
assert hist.span('1:3') == ['first', 'second', 'third']
80+
assert hist.span('2:-1') == ['second', 'third']
81+
assert hist.span('-3:4') == ['second', 'third','fourth']
82+
assert hist.span('-4:-2') == ['first', 'second']
8083

8184
assert hist.span(':-2') == ['first', 'second']
8285
assert hist.span('..-2') == ['first', 'second']
8386

84-
value_errors = ['fred', 'fred:joe', 'a..b', '2 ..', '1 : 3', '-2..-3' ]
87+
value_errors = ['fred', 'fred:joe', 'a..b', '2 ..', '1 : 3']
8588
for tryit in value_errors:
8689
with pytest.raises(ValueError):
8790
hist.span(tryit)

0 commit comments

Comments
 (0)