navigation: add a "go to root" function

This commit is contained in:
dece 2021-03-16 19:38:11 +01:00
parent cf6b67f78b
commit b508ab8128
2 changed files with 13 additions and 0 deletions

View file

@ -113,6 +113,8 @@ class Browser:
self.go_back()
elif char == ord("u"):
self.go_to_parent_page()
elif char == ord("U"):
self.go_to_root_page()
elif curses.ascii.isdigit(char):
self.handle_digit_input(char)
elif char == curses.KEY_MOUSE:
@ -470,6 +472,11 @@ class Browser:
if self.current_url:
self.open_gemini_url(get_parent_url(self.current_url))
def go_to_root_page(self):
"""Go to the root URL if possible."""
if self.current_url:
self.open_gemini_url(get_root_url(self.current_url))
def open_web_url(self, url):
"""Open a Web URL. Currently relies in Python's webbrowser module."""
self.set_status(f"Opening {url}")

View file

@ -58,3 +58,9 @@ def get_parent_url(url: str) -> str:
if last_slash > -1:
path = path[:last_slash + 1]
return urllib.parse.urlunparse((scheme, netloc, path, params, query, frag))
def get_root_url(url: str) -> str:
"""Return the root URL (basically discards path)."""
scheme, netloc, _, _, _, _ = parse_url(url)
return urllib.parse.urlunparse((scheme, netloc, "/", "", "", ""))