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

36 lines
975 B
Python
Raw Normal View History

2021-03-11 19:16:15 +01:00
"""History management."""
2021-02-18 01:40:05 +01:00
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.
"""
2021-02-18 01:40:05 +01:00
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
2021-02-18 01:40:05 +01:00
def to_gemtext(self):
"""Generate a simple Gemtext page of the current history."""
return "\n".join("=> " + url for url in self.urls)