51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class HistoryItem:
|
|
path: Path
|
|
fragment: str = ""
|
|
|
|
|
|
class NavigationHistory:
|
|
def __init__(self) -> None:
|
|
self._items: list[HistoryItem] = []
|
|
self._index = -1
|
|
|
|
def push(self, item: HistoryItem) -> None:
|
|
if self.current == item:
|
|
return
|
|
del self._items[self._index + 1 :]
|
|
self._items.append(item)
|
|
self._index = len(self._items) - 1
|
|
|
|
@property
|
|
def current(self) -> HistoryItem | None:
|
|
if 0 <= self._index < len(self._items):
|
|
return self._items[self._index]
|
|
return None
|
|
|
|
@property
|
|
def can_go_back(self) -> bool:
|
|
return self._index > 0
|
|
|
|
@property
|
|
def can_go_forward(self) -> bool:
|
|
return self._index + 1 < len(self._items)
|
|
|
|
def back(self) -> HistoryItem | None:
|
|
if not self.can_go_back:
|
|
return None
|
|
self._index -= 1
|
|
return self.current
|
|
|
|
def forward(self) -> HistoryItem | None:
|
|
if not self.can_go_forward:
|
|
return None
|
|
self._index += 1
|
|
return self.current
|
|
|