This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Bebop/bebop/history.py
dece 397a143695 navigation: rework entirely URL management
oh and fix the whole history mess a bit
2021-05-08 22:47:34 +02:00

36 lines
975 B
Python

"""History management."""
class History:
"""Basic browsing history manager.
The history follows the "by last visited" behaviour of Firefox for the lack
of a better idea. Links are pushed as they are visited. If a link is visited
again, it bubbles up to the top of the history.
"""
def __init__(self):
self.urls = []
def push(self, url):
"""Add an URL to the history.
If the URL is already in the list, it is moved to the top.
"""
try:
self.urls.remove(url)
except ValueError:
pass
self.urls.append(url)
def get_previous(self):
"""Return previous URL, or None if there is only one or zero URL."""
try:
return self.urls[-2]
except IndexError:
return None
def to_gemtext(self):
"""Generate a simple Gemtext page of the current history."""
return "\n".join("=> " + url for url in self.urls)