browser: allow browsing local directories
This commit is contained in:
parent
c65c385a20
commit
5b1a544761
|
@ -1,7 +1,5 @@
|
|||
TODO
|
||||
----------------------------------------
|
||||
opt. maintain history between sessions
|
||||
directory view for file scheme
|
||||
search in page (ugh)
|
||||
|
||||
|
||||
|
@ -59,3 +57,4 @@ home page
|
|||
different rendering mode
|
||||
preferences per site
|
||||
basic mouse support
|
||||
basic local browsing
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
"""Local files browser."""
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from bebop.browser.browser import Browser
|
||||
from bebop.page import Page
|
||||
|
||||
|
@ -20,6 +23,12 @@ def open_file(browser: Browser, filepath: str, encoding="utf-8"):
|
|||
Returns:
|
||||
The loaded file URI on success, None otherwise (e.g. file not found).
|
||||
"""
|
||||
path = Path(filepath)
|
||||
if not path.exists():
|
||||
logging.error(f"File {filepath} does not exist.")
|
||||
return None
|
||||
|
||||
if path.is_file():
|
||||
try:
|
||||
with open(filepath, "rt", encoding=encoding) as f:
|
||||
text = f.read()
|
||||
|
@ -27,6 +36,15 @@ def open_file(browser: Browser, filepath: str, encoding="utf-8"):
|
|||
browser.set_status_error(f"Failed to open file: {exc}")
|
||||
return None
|
||||
browser.load_page(Page.from_text(text))
|
||||
elif path.is_dir():
|
||||
gemtext = filepath + "\n\n"
|
||||
for entry in sorted(path.iterdir()):
|
||||
name = entry.name
|
||||
if entry.is_dir():
|
||||
name += "/"
|
||||
gemtext += f"=> {entry} {name}\n"
|
||||
wrap_at = browser.config["text_width"]
|
||||
browser.load_page(Page.from_gemtext(gemtext, wrap_at))
|
||||
file_url = "file://" + filepath
|
||||
browser.current_url = file_url
|
||||
return file_url
|
||||
|
|
Reference in a new issue