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

58 lines
1.8 KiB
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.
"""
2021-02-18 01:40:05 +01:00
def __init__(self, limit):
2021-02-18 01:40:05 +01:00
self.urls = []
self.backlist = []
self.limit = limit
2021-02-18 01:40:05 +01:00
def push(self, url):
"""Add an URL to the history."""
# Append url to our URLs, bubbling it up if it's already there.
try:
self.urls.remove(url)
except ValueError:
pass
self.urls.append(url)
if len(self.urls) > self.limit:
self.urls.pop(0)
# Also simply push it to the backlist.
self.backlist.append(url)
if len(self.backlist) > self.limit:
self.backlist.pop(0)
def get_previous(self, actual_previous=False):
"""Return previous URL, or None if there is only one or zero URL."""
try:
if actual_previous:
return self.backlist[-1]
# The backlist should be populated with the first link visited and
# never completely emptied afterwards, or we end up in situation
# where you can't get away from internal pages.
if len(self.backlist) > 1:
self.backlist.pop()
return self.backlist[-1]
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.
Present a page that follows the "by last visited" behaviour of Firefox
for the lack of a better idea, avoiding duplicated entries.
"""
urls = []
seen = set()
for url in reversed(self.urls):
if url in seen:
continue
urls.append(url)
seen.add(url)
2021-05-09 02:03:45 +02:00
return "# History\n\n" + "\n".join("=> " + url for url in urls)