history: separate smol module

This commit is contained in:
dece 2021-02-18 01:40:05 +01:00
parent 294c7796fa
commit 8830465fbf
2 changed files with 20 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import os
from bebop.colors import ColorPair, init_colors from bebop.colors import ColorPair, init_colors
from bebop.command_line import (CommandLine, EscapeCommandInterrupt, from bebop.command_line import (CommandLine, EscapeCommandInterrupt,
TerminateCommandInterrupt) TerminateCommandInterrupt)
from bebop.history import History
from bebop.mouse import ButtonState from bebop.mouse import ButtonState
from bebop.navigation import join_url, parse_url, sanitize_url, set_parameter from bebop.navigation import join_url, parse_url, sanitize_url, set_parameter
from bebop.page import Page from bebop.page import Page
@ -24,7 +25,7 @@ class Browser:
self.command_line = None self.command_line = None
self.status_data = ("", 0) self.status_data = ("", 0)
self.current_url = "" self.current_url = ""
self.history = [] self.history = History()
@property @property
def h(self): def h(self):
@ -414,5 +415,5 @@ class Browser:
def go_back(self): def go_back(self):
"""Go back in history if possible.""" """Go back in history if possible."""
if self.history: if self.history.has_links():
self.open_gemini_url(self.history.pop(), history=False) self.open_gemini_url(self.history.pop(), history=False)

17
bebop/history.py Normal file
View file

@ -0,0 +1,17 @@
class History:
def __init__(self):
self.urls = []
def has_links(self):
"""Return True if there is at least one URL in the history."""
return bool(self.urls)
def push(self, url):
"""Add an URL to the history."""
if not self.urls or self.urls[-1] != url:
self.urls.append(url)
def pop(self):
"""Return latest URL added to history and remove it."""
return self.urls.pop()