Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
- History regular expressions containing range separators are now handled as regex searches.
Other selectors are treated as numbers or ranges only when they match the supported syntax;
strings such as URLs, `a:b`, `+1`, and `1_0` remain literal searches.
- Fixed `History.truncate()` leaving the current session's start index unchanged. Code calling
it directly would then have retained current-session entries hidden from unbounded ranges and
searches. Interactive `history` use was unaffected, since `cmd2` only truncates when
persisting history at exit.

## 4.3.1 (October 24, 2026)

Expand Down
11 changes: 8 additions & 3 deletions cmd2/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,21 @@ def isin(hi: HistoryItem) -> bool:
def truncate(self, max_length: int) -> None:
"""Truncate the length of the history, dropping the oldest items if necessary.

:param max_length: the maximum length of the history, if negative, all history
The current session's start index is shifted so it still marks the first retained
current-session item. If any current-session items are dropped, all retained items
belong to the current session.

:param max_length: the maximum length of the history, if zero or negative, all history
items will be deleted
:return: nothing
"""
if max_length <= 0:
# remove all history
del self[:]
self.clear()
elif len(self) > max_length:
last_element = len(self) - max_length
del self[0:last_element]
# keep the session boundary pointing at the same retained items
self.session_start_index = max(0, self.session_start_index - last_element)

def _build_result_dictionary(
self, start: int, end: int, filter_func: Callable[[HistoryItem], bool] | None = None
Expand Down
48 changes: 48 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import cmd2
from cmd2.history import HistoryFormatError, HistoryIndexError
from cmd2.parsing import Statement

from .conftest import (
normalize,
Expand Down Expand Up @@ -399,6 +400,53 @@ def test_history_max_length(hist) -> None:
assert hist.get(2).statement.raw == "fourth"


def test_history_truncate_shifts_session_start(hist) -> None:
hist.start_session()
hist.append(Statement("", raw="new-1"))
hist.append(Statement("", raw="new-2"))

hist.truncate(3)
assert hist.session_start_index == 1

expected = ["new-1", "new-2"]
assert [item.raw for item in hist.span(":").values()] == expected
assert [item.raw for item in hist.str_search("new").values()] == expected
assert [item.raw for item in hist.regex_search("/new/").values()] == expected


@pytest.mark.parametrize(
("max_length", "expected"),
[
# Drops exactly the persisted items
(2, ["new-1", "new-2"]),
# Drops into the current session
(1, ["new-2"]),
],
)
def test_history_truncate_at_or_into_current_session(hist, max_length, expected) -> None:
hist.start_session()
hist.append(Statement("", raw="new-1"))
hist.append(Statement("", raw="new-2"))

hist.truncate(max_length)
assert hist.session_start_index == 0
assert [item.raw for item in hist.span(":").values()] == expected
assert [item.raw for item in hist.str_search("new").values()] == expected
assert [item.raw for item in hist.regex_search("/new/").values()] == expected


@pytest.mark.parametrize("max_length", [0, -1])
def test_history_truncate_all_resets_session_start(hist, max_length) -> None:
hist.start_session()
hist.truncate(max_length)
assert hist.session_start_index == 0

hist.append(Statement("", raw="new-1"))
assert [item.raw for item in hist.span(":").values()] == ["new-1"]
assert [item.raw for item in hist.str_search("new").values()] == ["new-1"]
assert [item.raw for item in hist.regex_search("/new/").values()] == ["new-1"]


def test_history_to_json(hist) -> None:
assert hist_json == hist.to_json()

Expand Down
Loading