diff --git a/CHANGELOG.md b/CHANGELOG.md index a463c146d..b0ac68c3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/cmd2/history.py b/cmd2/history.py index abd168028..8856bb610 100644 --- a/cmd2/history.py +++ b/cmd2/history.py @@ -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 diff --git a/tests/test_history.py b/tests/test_history.py index 7ff3fca27..369f95022 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -14,6 +14,7 @@ import cmd2 from cmd2.history import HistoryFormatError, HistoryIndexError +from cmd2.parsing import Statement from .conftest import ( normalize, @@ -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()