History API¶
The history module manages browsing session history.
Overview¶
Astronomo maintains an in-memory history of visited pages during a session. Each history entry stores:
- The page URL
- The page content
- Scroll position
- Selected link index
- Response metadata
History is not persisted between sessions.
API Reference¶
history
¶
History management for browsing navigation.
This module provides history tracking for the Gemini browser, enabling back/forward navigation with cached content to avoid re-fetching pages.
HistoryEntry
dataclass
¶
HistoryEntry(
url: str,
content: list[GemtextLine],
scroll_position: float = 0,
link_index: int = 0,
timestamp: datetime = datetime.now(),
status: int = 20,
meta: str = "",
mime_type: str = "text/gemini",
)
Represents a single entry in the browsing history.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
The URL of the page |
content |
list[GemtextLine]
|
Parsed Gemtext content as list of GemtextLine objects |
scroll_position |
float
|
Vertical scroll position in the viewer |
link_index |
int
|
Currently selected link index |
timestamp |
datetime
|
When the page was visited |
status |
int
|
Gemini response status code |
meta |
str
|
Response metadata string |
mime_type |
str
|
Content MIME type |
HistoryManager
¶
Manages browsing history with back/forward navigation.
Implements standard browser history behavior: - Each navigation creates a new entry - Navigating back/forward moves through the history stack - New navigation from a non-head position clears forward history - Maximum size enforced with LRU eviction (oldest entries removed first)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum number of history entries to keep (default: 100) |
100
|
Initialize history manager with specified maximum size.
Source code in src/astronomo/history.py
__len__
¶
can_go_back
¶
Check if back navigation is possible.
Returns:
| Type | Description |
|---|---|
bool
|
True if there are entries before the current position |
can_go_forward
¶
Check if forward navigation is possible.
Returns:
| Type | Description |
|---|---|
bool
|
True if there are entries after the current position |
clear
¶
current
¶
Get current history entry.
Returns:
| Type | Description |
|---|---|
Optional[HistoryEntry]
|
The current HistoryEntry, or None if history is empty |
Source code in src/astronomo/history.py
get_all_entries
¶
Return all history entries as a list.
Returns:
| Type | Description |
|---|---|
list[HistoryEntry]
|
List of all HistoryEntry objects, oldest first |
go_back
¶
Navigate back and return the entry.
Returns:
| Type | Description |
|---|---|
Optional[HistoryEntry]
|
The previous HistoryEntry, or None if already at the start |
Source code in src/astronomo/history.py
go_forward
¶
Navigate forward and return the entry.
Returns:
| Type | Description |
|---|---|
Optional[HistoryEntry]
|
The next HistoryEntry, or None if already at the end |
Source code in src/astronomo/history.py
push
¶
Add new entry to history, clearing forward history if not at head.
When adding a new entry while not at the head of history (i.e., after navigating back), all forward history entries are removed. This matches standard browser behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry
|
HistoryEntry
|
The HistoryEntry to add |
required |
Source code in src/astronomo/history.py
Example Usage¶
from astronomo.history import HistoryManager, HistoryEntry
# Create a history manager with max 100 entries
history = HistoryManager(max_size=100)
# Add an entry
entry = HistoryEntry(
url="gemini://example.com/",
content="# Example Page",
scroll_position=0,
link_index=0,
)
history.push(entry)
# Navigate back
if history.can_go_back():
previous = history.go_back()
# Navigate forward
if history.can_go_forward():
next_entry = history.go_forward()