Skip to content

Commit

Permalink
Allow ~\d in backward history search to show the full session
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Dec 13, 2024
1 parent 89d672e commit a0c65d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions IPython/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ def extract_hist_ranges(ranges_str: str) -> Iterable[Tuple[int, int, Optional[in
return

for range_str in ranges_str.split():
if re.match(r'~\d+', range_str):
yield (int(range_str.replace("~", "-")), 1, None)
continue
rmatch = range_re.match(range_str)
if not rmatch:
continue
Expand Down
26 changes: 16 additions & 10 deletions IPython/core/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tempfile import TemporaryDirectory
# our own packages
from traitlets.config.loader import Config
import pytest

from IPython.core.history import HistoryAccessor, HistoryManager, extract_hist_ranges

Expand Down Expand Up @@ -152,16 +153,21 @@ def test_history():
ip.history_manager = hist_manager_ori


def test_extract_hist_ranges():
instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
expected = [(0, 1, 2), # 0 == current session
(2, 3, 4),
(-4, 5, 7),
(-4, 7, 10),
(-9, 2, None), # None == to end
(-8, 1, None),
(-7, 1, 6),
(-10, 1, None)]


@pytest.mark.parametrize(
"instr, expected",
[
("1", [(0, 1, 2)]),
("2/3", [(2, 3, 4)]),
("~4/5-6", [(-4, 5, 7)]),
("~4/7-~4/9", [(-4, 7, 10)]),
("~9/2-~7/5", [(-9, 2, None)]),
("~10/", [(-10, 1, None)]),
("~4", [(-4, 1, None)]),
],
)
def test_extract_hist_ranges(instr, expected):
actual = list(extract_hist_ranges(instr))
assert actual == expected

Expand Down

0 comments on commit a0c65d1

Please sign in to comment.