2021-03-11 19:16:15 +01:00
|
|
|
"""Main browser logic."""
|
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
import curses
|
|
|
|
import curses.ascii
|
|
|
|
import curses.textpad
|
2021-05-13 01:25:50 +02:00
|
|
|
import logging
|
2021-02-12 19:01:42 +01:00
|
|
|
import os
|
2021-05-09 01:39:33 +02:00
|
|
|
import subprocess
|
2021-04-16 19:30:33 +02:00
|
|
|
import tempfile
|
2021-06-04 16:09:00 +02:00
|
|
|
from importlib import import_module
|
2021-02-18 19:01:28 +01:00
|
|
|
from math import inf
|
2021-05-09 01:39:33 +02:00
|
|
|
from pathlib import Path
|
2021-05-16 01:30:00 +02:00
|
|
|
from typing import Optional, Tuple
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-04-16 19:30:33 +02:00
|
|
|
from bebop.bookmarks import (
|
2021-05-15 18:06:04 +02:00
|
|
|
get_bookmarks_path,
|
|
|
|
get_bookmarks_document,
|
|
|
|
save_bookmark,
|
2021-04-16 19:30:33 +02:00
|
|
|
)
|
2021-05-29 23:59:50 +02:00
|
|
|
from bebop.colors import A_ITALIC, ColorPair, init_colors
|
2021-03-13 20:37:13 +01:00
|
|
|
from bebop.command_line import CommandLine
|
2021-04-17 21:54:11 +02:00
|
|
|
from bebop.external import open_external_program
|
2021-05-29 01:13:43 +02:00
|
|
|
from bebop.fs import get_capsule_prefs_path, get_identities_list_path
|
2021-05-13 01:26:10 +02:00
|
|
|
from bebop.help import get_help
|
2021-02-18 01:40:05 +01:00
|
|
|
from bebop.history import History
|
2021-05-13 01:24:29 +02:00
|
|
|
from bebop.identity import load_identities
|
2021-03-13 16:31:11 +01:00
|
|
|
from bebop.links import Links
|
2021-06-28 00:49:44 +02:00
|
|
|
from bebop.metalines import LineType, RENDER_MODES
|
2021-05-09 01:39:33 +02:00
|
|
|
from bebop.mime import MimeType
|
2021-02-12 19:01:42 +01:00
|
|
|
from bebop.mouse import ButtonState
|
2021-03-28 18:28:35 +02:00
|
|
|
from bebop.navigation import (
|
2021-05-15 18:06:04 +02:00
|
|
|
NO_NETLOC_SCHEMES,
|
|
|
|
get_parent_url,
|
|
|
|
get_root_url,
|
|
|
|
join_url,
|
|
|
|
parse_url,
|
|
|
|
unparse_url,
|
2021-05-08 22:41:42 +02:00
|
|
|
)
|
2021-06-28 00:49:44 +02:00
|
|
|
from bebop.page import Page, get_render_options
|
2021-03-18 01:55:31 +01:00
|
|
|
from bebop.page_pad import PagePad
|
2021-05-29 01:13:43 +02:00
|
|
|
from bebop.preferences import load_capsule_prefs, save_capsule_prefs
|
2021-05-15 19:44:50 +02:00
|
|
|
from bebop.welcome import WELCOME_PAGE
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
|
2021-02-16 21:22:49 +01:00
|
|
|
class Browser:
|
2021-04-18 16:10:48 +02:00
|
|
|
"""Manage the events, inputs and rendering.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
- config: config dict passed to the browser.
|
|
|
|
- stash: certificate stash passed to the browser.
|
|
|
|
- screen: curses stdscr.
|
|
|
|
- dim: current screen dimensions.
|
|
|
|
- page_pad: curses pad containing the current page view.
|
|
|
|
- status_line: curses window used to report current status.
|
|
|
|
- command_line: a CommandLine object for the user to interact with.
|
|
|
|
- running: the browser will continue running while this is true.
|
|
|
|
- status_data: 3-uple of status text, color pair and attributes of the
|
2021-05-13 01:24:29 +02:00
|
|
|
status line, used to reset status after an error.
|
2021-04-18 16:10:48 +02:00
|
|
|
- history: an History object.
|
2021-05-13 01:24:29 +02:00
|
|
|
- cache: a dict containing cached pages.
|
2021-04-18 16:10:48 +02:00
|
|
|
- special_pages: a dict containing page names used with "bebop" scheme;
|
2021-05-13 01:24:29 +02:00
|
|
|
values are dicts as well: the "open" key maps to a callable to use when
|
|
|
|
the page is accessed, and the optional "source" key maps to callable
|
|
|
|
returning the page source path.
|
2021-06-11 18:25:56 +02:00
|
|
|
- last_download: tuple of MimeType (may be None) and path, or None.
|
2021-05-13 01:24:29 +02:00
|
|
|
- identities: identities map.
|
2021-06-04 02:25:15 +02:00
|
|
|
- search_res_lines: list of lines containing results of the last search.
|
2021-04-18 16:10:48 +02:00
|
|
|
"""
|
2021-03-28 18:28:35 +02:00
|
|
|
|
2021-06-04 02:25:15 +02:00
|
|
|
SEARCH_NEXT = 0
|
|
|
|
SEARCH_PREVIOUS = 1
|
|
|
|
|
2021-04-18 02:27:05 +02:00
|
|
|
def __init__(self, config, cert_stash):
|
|
|
|
self.config = config
|
2021-04-19 02:04:18 +02:00
|
|
|
self.stash = cert_stash
|
2021-02-12 19:01:42 +01:00
|
|
|
self.screen = None
|
|
|
|
self.dim = (0, 0)
|
2021-03-13 20:37:13 +01:00
|
|
|
self.page_pad = None
|
2021-02-12 23:29:51 +01:00
|
|
|
self.status_line = None
|
|
|
|
self.command_line = None
|
2021-03-13 20:37:13 +01:00
|
|
|
self.running = True
|
2021-03-09 00:44:54 +01:00
|
|
|
self.status_data = ("", 0, 0)
|
2021-05-09 00:46:26 +02:00
|
|
|
self.history = History(self.config["history_limit"])
|
2021-03-14 00:05:22 +01:00
|
|
|
self.cache = {}
|
2021-04-18 16:10:48 +02:00
|
|
|
self.special_pages = self.setup_special_pages()
|
2021-06-11 18:25:56 +02:00
|
|
|
self.last_download: Optional[Tuple[Optional[MimeType], Path]] = None
|
2021-05-29 01:11:38 +02:00
|
|
|
self.identities = {}
|
2021-06-04 02:25:15 +02:00
|
|
|
self.search_res_lines = []
|
2021-06-04 16:09:00 +02:00
|
|
|
self.plugins = []
|
2021-03-18 01:56:24 +01:00
|
|
|
self._current_url = ""
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def h(self):
|
|
|
|
return self.dim[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def w(self):
|
|
|
|
return self.dim[1]
|
|
|
|
|
2021-03-18 01:56:24 +01:00
|
|
|
@property
|
2021-06-11 22:51:08 +02:00
|
|
|
def current_url(self) -> str:
|
2021-03-18 01:56:24 +01:00
|
|
|
"""Return the current URL."""
|
|
|
|
return self._current_url
|
|
|
|
|
|
|
|
@current_url.setter
|
2021-06-11 22:51:08 +02:00
|
|
|
def current_url(self, url: str):
|
2021-03-18 01:56:24 +01:00
|
|
|
"""Set the current URL and show it in the status line."""
|
|
|
|
self._current_url = url
|
|
|
|
self.set_status(url)
|
|
|
|
|
2021-05-12 22:29:03 +02:00
|
|
|
@property
|
|
|
|
def current_scheme(self):
|
|
|
|
"""Return the scheme of the current URL."""
|
|
|
|
return parse_url(self._current_url)["scheme"] or ""
|
|
|
|
|
2021-06-05 21:47:06 +02:00
|
|
|
@property
|
|
|
|
def current_page(self) -> Optional[Page]:
|
|
|
|
return self.page_pad.current_page
|
|
|
|
|
2021-04-18 16:10:48 +02:00
|
|
|
def setup_special_pages(self):
|
|
|
|
"""Return a dict with the special pages functions."""
|
|
|
|
return {
|
2021-05-15 19:44:50 +02:00
|
|
|
"welcome": { "open": self.open_welcome_page },
|
|
|
|
"help": { "open": self.open_help },
|
|
|
|
"history": { "open": self.open_history },
|
2021-04-18 16:10:48 +02:00
|
|
|
"bookmarks": {
|
|
|
|
"open": self.open_bookmarks,
|
|
|
|
"source": lambda: str(get_bookmarks_path())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
def run(self, *args, **kwargs):
|
|
|
|
"""Use curses' wrapper around _run."""
|
|
|
|
os.environ.setdefault("ESCDELAY", "25")
|
2021-05-13 01:25:50 +02:00
|
|
|
logging.info("Cursing…")
|
2021-02-12 19:01:42 +01:00
|
|
|
curses.wrapper(self._run, *args, **kwargs)
|
|
|
|
|
|
|
|
def _run(self, stdscr, start_url=None):
|
|
|
|
"""Start displaying content and handling events."""
|
2021-06-03 17:14:49 +02:00
|
|
|
# Setup Curses.
|
2021-02-12 19:01:42 +01:00
|
|
|
self.screen = stdscr
|
|
|
|
self.screen.clear()
|
|
|
|
self.screen.refresh()
|
2021-05-31 17:43:37 +02:00
|
|
|
mousemask = curses.mousemask(curses.ALL_MOUSE_EVENTS)
|
|
|
|
if mousemask == 0:
|
|
|
|
logging.error("Could not enable mouse support.")
|
2021-02-12 23:29:51 +01:00
|
|
|
curses.curs_set(0)
|
|
|
|
init_colors()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-06-03 17:14:49 +02:00
|
|
|
# Setup windows and pads.
|
2021-02-12 19:01:42 +01:00
|
|
|
self.dim = self.screen.getmaxyx()
|
2021-03-13 20:37:13 +01:00
|
|
|
self.page_pad = PagePad(self.h - 2)
|
2021-02-12 23:29:51 +01:00
|
|
|
self.status_line = self.screen.subwin(
|
2021-02-12 19:01:42 +01:00
|
|
|
*self.line_dim,
|
2021-02-12 23:29:51 +01:00
|
|
|
*self.status_line_pos,
|
2021-02-12 19:01:42 +01:00
|
|
|
)
|
2021-02-12 23:29:51 +01:00
|
|
|
command_line_window = self.screen.subwin(
|
2021-02-12 19:01:42 +01:00
|
|
|
*self.line_dim,
|
2021-02-12 23:29:51 +01:00
|
|
|
*self.command_line_pos,
|
2021-02-12 19:01:42 +01:00
|
|
|
)
|
2021-04-18 02:27:05 +02:00
|
|
|
self.command_line = CommandLine(
|
|
|
|
command_line_window,
|
|
|
|
self.config["command_editor"]
|
|
|
|
)
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-06-03 17:14:49 +02:00
|
|
|
# Load user data files, record which failed to load to warn the user.
|
2021-05-29 01:11:38 +02:00
|
|
|
failed_to_load = []
|
|
|
|
identities = load_identities(get_identities_list_path())
|
|
|
|
if identities is None:
|
|
|
|
failed_to_load.append("identities")
|
|
|
|
else:
|
|
|
|
self.identities = identities
|
2021-05-29 01:13:43 +02:00
|
|
|
capsule_prefs = load_capsule_prefs(get_capsule_prefs_path())
|
|
|
|
if capsule_prefs is None:
|
|
|
|
failed_to_load.append("capsule preferences")
|
|
|
|
else:
|
|
|
|
self.capsule_prefs = capsule_prefs
|
2021-05-29 01:11:38 +02:00
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
# Load user data files that may not exist (no warning).
|
|
|
|
if self.config["persistent_history"]:
|
|
|
|
if not self.history.load():
|
|
|
|
logging.warning("Could not load history file.")
|
|
|
|
|
2021-06-04 16:09:00 +02:00
|
|
|
# Load plugins.
|
|
|
|
self.load_plugins()
|
|
|
|
|
|
|
|
# If there has been any issue to load user files, show them instead of
|
|
|
|
# automatically moving forward. Else either open the URL requested or
|
|
|
|
# show the home page.
|
2021-05-29 01:11:38 +02:00
|
|
|
if failed_to_load:
|
|
|
|
error_msg = (
|
|
|
|
f"Failed to open some local data: {', '.join(failed_to_load)}. "
|
2021-06-03 16:45:35 +02:00
|
|
|
"These may be replaced if you continue."
|
2021-05-29 01:11:38 +02:00
|
|
|
)
|
|
|
|
self.set_status_error(error_msg)
|
|
|
|
elif start_url:
|
2021-05-15 19:06:44 +02:00
|
|
|
self.open_url(start_url)
|
|
|
|
else:
|
|
|
|
self.open_home()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
# Start listening for inputs.
|
2021-03-08 23:40:03 +01:00
|
|
|
while self.running:
|
2021-03-09 00:44:42 +01:00
|
|
|
try:
|
|
|
|
self.handle_inputs()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.set_status("Cancelled.")
|
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
if self.config["persistent_history"]:
|
|
|
|
self.history.save()
|
|
|
|
|
2021-03-09 00:44:42 +01:00
|
|
|
def handle_inputs(self):
|
|
|
|
char = self.screen.getch()
|
2021-04-18 16:10:48 +02:00
|
|
|
if char == ord("?"):
|
|
|
|
self.open_help()
|
|
|
|
elif char == ord(":"):
|
2021-03-09 00:44:42 +01:00
|
|
|
self.quick_command("")
|
|
|
|
elif char == ord("r"):
|
|
|
|
self.reload_page()
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("h") or char == curses.KEY_LEFT:
|
2021-05-31 18:53:26 +02:00
|
|
|
self.scroll_page_horizontally(-self.config["scroll_step"])
|
2021-03-13 18:58:02 +01:00
|
|
|
elif char == ord("H"):
|
2021-04-17 22:02:06 +02:00
|
|
|
self.scroll_whole_page_left()
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("j") or char == curses.KEY_DOWN:
|
2021-05-31 18:53:26 +02:00
|
|
|
self.scroll_page_vertically(self.config["scroll_step"])
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("J") or char == curses.KEY_NPAGE:
|
2021-03-13 18:58:02 +01:00
|
|
|
self.scroll_whole_page_down()
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("k") or char == curses.KEY_UP:
|
2021-05-31 18:53:26 +02:00
|
|
|
self.scroll_page_vertically(-self.config["scroll_step"])
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("K") or char == curses.KEY_PPAGE:
|
2021-03-13 18:58:02 +01:00
|
|
|
self.scroll_whole_page_up()
|
2021-05-31 16:30:26 +02:00
|
|
|
elif char == ord("l") or char == curses.KEY_RIGHT:
|
2021-05-31 18:53:26 +02:00
|
|
|
self.scroll_page_horizontally(self.config["scroll_step"])
|
2021-03-13 18:58:02 +01:00
|
|
|
elif char == ord("L"):
|
2021-04-17 22:02:06 +02:00
|
|
|
self.scroll_whole_page_right()
|
2021-03-13 18:58:02 +01:00
|
|
|
elif char == ord("^"):
|
2021-04-17 22:02:06 +02:00
|
|
|
self.scroll_page_horizontally(-inf)
|
2021-03-09 00:44:42 +01:00
|
|
|
elif char == ord("g"):
|
2021-02-12 19:01:42 +01:00
|
|
|
char = self.screen.getch()
|
2021-03-09 00:44:42 +01:00
|
|
|
if char == ord("g"):
|
|
|
|
self.scroll_page_vertically(-inf)
|
|
|
|
elif char == ord("G"):
|
|
|
|
self.scroll_page_vertically(inf)
|
2021-03-13 18:58:02 +01:00
|
|
|
elif char == ord("o"):
|
|
|
|
self.quick_command("open")
|
2021-05-09 01:39:33 +02:00
|
|
|
elif char == ord("O"):
|
2021-06-11 22:51:08 +02:00
|
|
|
self.quick_command(f"open {self.current_url}")
|
2021-03-13 18:58:02 +01:00
|
|
|
elif char == ord("p"):
|
|
|
|
self.go_back()
|
2021-03-14 02:05:42 +01:00
|
|
|
elif char == ord("u"):
|
|
|
|
self.go_to_parent_page()
|
2021-03-16 19:38:11 +01:00
|
|
|
elif char == ord("U"):
|
|
|
|
self.go_to_root_page()
|
2021-03-18 01:56:24 +01:00
|
|
|
elif char == ord("b"):
|
|
|
|
self.open_bookmarks()
|
|
|
|
elif char == ord("B"):
|
|
|
|
self.add_bookmark()
|
2021-04-16 19:30:33 +02:00
|
|
|
elif char == ord("e"):
|
|
|
|
self.edit_page()
|
2021-05-08 22:41:42 +02:00
|
|
|
elif char == ord("y"):
|
|
|
|
self.open_history()
|
2021-05-29 01:26:15 +02:00
|
|
|
elif char == ord("§"):
|
|
|
|
self.toggle_render_mode()
|
2021-06-04 02:25:15 +02:00
|
|
|
elif char == ord("/"):
|
|
|
|
self.search_in_page()
|
|
|
|
elif char == ord("n"):
|
|
|
|
self.move_to_search_result(Browser.SEARCH_NEXT)
|
|
|
|
elif char == ord("N"):
|
|
|
|
self.move_to_search_result(Browser.SEARCH_PREVIOUS)
|
2021-03-09 00:44:42 +01:00
|
|
|
elif curses.ascii.isdigit(char):
|
|
|
|
self.handle_digit_input(char)
|
|
|
|
elif char == curses.KEY_MOUSE:
|
2021-05-31 17:43:37 +02:00
|
|
|
try:
|
|
|
|
self.handle_mouse(*curses.getmouse())
|
|
|
|
except curses.error:
|
2021-06-04 15:35:53 +02:00
|
|
|
logging.error("Failed to get mouse information.")
|
2021-03-09 00:44:42 +01:00
|
|
|
elif char == curses.KEY_RESIZE:
|
|
|
|
self.handle_resize()
|
|
|
|
elif char == curses.ascii.ESC: # Can be ESC or ALT char.
|
|
|
|
self.screen.nodelay(True)
|
2021-03-13 18:58:02 +01:00
|
|
|
char = self.screen.getch()
|
2021-05-16 17:24:17 +02:00
|
|
|
self.screen.nodelay(False)
|
2021-03-13 18:58:02 +01:00
|
|
|
if char == -1:
|
2021-03-18 01:56:24 +01:00
|
|
|
self.reset_status()
|
2021-03-13 18:58:02 +01:00
|
|
|
else: # ALT keybinds.
|
|
|
|
if char == ord("h"):
|
|
|
|
self.scroll_page_horizontally(-1)
|
|
|
|
elif char == ord("j"):
|
|
|
|
self.scroll_page_vertically(1)
|
|
|
|
elif char == ord("k"):
|
|
|
|
self.scroll_page_vertically(-1)
|
|
|
|
elif char == ord("l"):
|
|
|
|
self.scroll_page_horizontally(1)
|
2021-06-11 22:51:08 +02:00
|
|
|
elif char == ord("o"):
|
|
|
|
self.open_last_download()
|
2021-03-13 18:58:02 +01:00
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
@property
|
2021-02-12 23:29:51 +01:00
|
|
|
def page_pad_size(self):
|
2021-02-12 19:01:42 +01:00
|
|
|
return self.h - 3, self.w - 1
|
|
|
|
|
|
|
|
@property
|
2021-02-12 23:29:51 +01:00
|
|
|
def status_line_pos(self):
|
2021-02-12 19:01:42 +01:00
|
|
|
return self.h - 2, 0
|
|
|
|
|
|
|
|
@property
|
2021-02-12 23:29:51 +01:00
|
|
|
def command_line_pos(self):
|
2021-02-12 19:01:42 +01:00
|
|
|
return self.h - 1, 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def line_dim(self):
|
|
|
|
return 1, self.w
|
|
|
|
|
|
|
|
def refresh_windows(self):
|
2021-02-16 21:22:49 +01:00
|
|
|
"""Refresh all windows and clear command line."""
|
2021-02-12 23:29:51 +01:00
|
|
|
self.refresh_page()
|
|
|
|
self.refresh_status_line()
|
|
|
|
self.command_line.clear()
|
|
|
|
|
|
|
|
def refresh_page(self):
|
2021-02-16 21:22:49 +01:00
|
|
|
"""Refresh the current page pad; it does not reload the page."""
|
2021-03-13 20:37:13 +01:00
|
|
|
self.page_pad.refresh_content(*self.page_pad_size)
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-02-12 23:29:51 +01:00
|
|
|
def refresh_status_line(self):
|
2021-02-12 19:01:42 +01:00
|
|
|
"""Refresh status line contents."""
|
2021-03-09 00:44:54 +01:00
|
|
|
text, pair, attributes = self.status_data
|
2021-05-13 01:25:50 +02:00
|
|
|
logging.debug("Status: " + text)
|
2021-02-12 19:01:42 +01:00
|
|
|
text = text[:self.w - 1]
|
2021-03-09 00:44:54 +01:00
|
|
|
color = curses.color_pair(pair)
|
|
|
|
self.status_line.addstr(0, 0, text, color | attributes)
|
2021-02-12 23:29:51 +01:00
|
|
|
self.status_line.clrtoeol()
|
|
|
|
self.status_line.refresh()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
def set_status(self, text):
|
|
|
|
"""Set a regular message in the status bar."""
|
2021-05-29 23:59:50 +02:00
|
|
|
self.status_data = text, ColorPair.NORMAL, A_ITALIC
|
2021-02-12 23:29:51 +01:00
|
|
|
self.refresh_status_line()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-03-18 01:56:24 +01:00
|
|
|
def reset_status(self):
|
|
|
|
"""Reset status line, e.g. after a cancelled action."""
|
|
|
|
self.set_status(self.current_url)
|
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
def set_status_error(self, text):
|
|
|
|
"""Set an error message in the status bar."""
|
2021-03-09 00:44:54 +01:00
|
|
|
self.status_data = text, ColorPair.ERROR, 0
|
2021-02-12 23:29:51 +01:00
|
|
|
self.refresh_status_line()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-03-13 16:31:11 +01:00
|
|
|
def quick_command(self, command):
|
|
|
|
"""Shortcut method to take user input with a prefixed command string."""
|
2021-04-19 00:28:20 +02:00
|
|
|
prefix = command + " " if command else ""
|
|
|
|
text = self.command_line.focus(CommandLine.CHAR_COMMAND, prefix=prefix)
|
|
|
|
if not text:
|
2021-03-13 16:31:11 +01:00
|
|
|
return
|
2021-04-19 00:28:20 +02:00
|
|
|
self.process_command(text)
|
2021-03-13 16:31:11 +01:00
|
|
|
|
|
|
|
def process_command(self, command_text: str):
|
|
|
|
"""Handle a client command."""
|
|
|
|
words = command_text.split()
|
|
|
|
num_words = len(words)
|
|
|
|
if num_words == 0:
|
|
|
|
return
|
2021-05-18 16:47:52 +02:00
|
|
|
|
2021-03-13 16:31:11 +01:00
|
|
|
command = words[0]
|
2021-06-08 14:45:28 +02:00
|
|
|
|
|
|
|
# Check for plugin registered commands first.
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if command in map(lambda c: c.name, plugin.commands):
|
|
|
|
plugin.use_command(self, command, command_text)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Then built-in commands without args.
|
2021-03-13 16:31:11 +01:00
|
|
|
if num_words == 1:
|
2021-05-28 13:26:41 +02:00
|
|
|
if command == "help":
|
|
|
|
self.open_help()
|
|
|
|
elif command in ("q", "quit"):
|
2021-03-13 16:31:11 +01:00
|
|
|
self.running = False
|
2021-05-15 19:44:50 +02:00
|
|
|
elif command in ("h", "home"):
|
|
|
|
self.open_home()
|
2021-05-24 20:09:34 +02:00
|
|
|
elif command in ("i", "info"):
|
|
|
|
self.show_page_info()
|
2021-06-27 17:06:23 +02:00
|
|
|
else:
|
|
|
|
self.set_status_error(f"Unknown command '{command}'.")
|
2021-06-08 14:45:28 +02:00
|
|
|
# And commands with one or more args.
|
2021-06-27 17:06:23 +02:00
|
|
|
else:
|
|
|
|
if command in ("o", "open"):
|
|
|
|
self.open_url(words[1])
|
|
|
|
elif command == "forget-certificate":
|
|
|
|
from bebop.browser.gemini import forget_certificate
|
|
|
|
forget_certificate(self, words[1])
|
|
|
|
elif command == "set-render-mode":
|
|
|
|
self.set_render_mode(words[1])
|
|
|
|
else:
|
|
|
|
self.set_status_error(f"Unknown command '{command}'.")
|
2021-03-13 16:31:11 +01:00
|
|
|
|
2021-06-18 01:44:17 +02:00
|
|
|
def get_user_text_input(self, status_text, char, prefix="", strip=False,
|
|
|
|
escape_to_none=False):
|
2021-05-12 22:29:03 +02:00
|
|
|
"""Get user input from the command-line."""
|
|
|
|
self.set_status(status_text)
|
2021-06-18 01:44:17 +02:00
|
|
|
result = self.command_line.focus(
|
|
|
|
char,
|
|
|
|
prefix=prefix,
|
|
|
|
escape_to_none=escape_to_none
|
|
|
|
)
|
2021-05-12 22:29:03 +02:00
|
|
|
self.reset_status()
|
2021-06-18 01:44:17 +02:00
|
|
|
if result is None:
|
|
|
|
return None
|
2021-05-12 22:29:03 +02:00
|
|
|
if strip:
|
|
|
|
result = result.strip()
|
|
|
|
return result
|
|
|
|
|
2021-05-15 19:06:44 +02:00
|
|
|
def open_url(self, url, base_url=None, redirects=0, history=True,
|
|
|
|
use_cache=False):
|
2021-02-12 19:01:42 +01:00
|
|
|
"""Try to open an URL.
|
|
|
|
|
2021-02-16 21:22:49 +01:00
|
|
|
This function assumes that the URL can be from an user and thus tries a
|
|
|
|
few things to make it work.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
- url: an URL string, may not be completely compliant.
|
2021-02-26 17:41:16 +01:00
|
|
|
- base_url: an URL string to use as base in case `url` is relative.
|
2021-02-16 21:22:49 +01:00
|
|
|
- redirections: number of redirections we did yet for the same request.
|
2021-03-18 01:55:31 +01:00
|
|
|
- history: whether the URL should be pushed to history on success.
|
|
|
|
- use_cache: whether we should look for an already cached document.
|
2021-02-12 19:01:42 +01:00
|
|
|
"""
|
2021-02-26 17:41:16 +01:00
|
|
|
if redirects > 5:
|
2021-02-15 19:57:49 +01:00
|
|
|
self.set_status_error(f"Too many redirections ({url}).")
|
2021-02-13 23:34:45 +01:00
|
|
|
return
|
2021-03-28 18:28:35 +02:00
|
|
|
|
2021-05-16 00:40:21 +02:00
|
|
|
# Take the current scheme as the default scheme to use if the URL does
|
|
|
|
# not specify it. If it's the bebop scheme, discard it. If there is no
|
|
|
|
# current scheme available, default to the gemini scheme.
|
|
|
|
current_scheme = self.current_scheme
|
|
|
|
if not current_scheme or current_scheme == "bebop":
|
|
|
|
current_scheme = "gemini"
|
2021-05-15 19:06:44 +02:00
|
|
|
parts = parse_url(url, default_scheme=current_scheme)
|
2021-03-28 18:28:35 +02:00
|
|
|
|
2021-05-16 00:40:21 +02:00
|
|
|
# If there is no netloc part, try to join the URL.
|
2021-05-15 18:06:04 +02:00
|
|
|
if (
|
|
|
|
parts["netloc"] is None
|
|
|
|
and parts["scheme"] == current_scheme
|
|
|
|
and parts["scheme"] not in NO_NETLOC_SCHEMES
|
|
|
|
):
|
2021-05-15 19:06:44 +02:00
|
|
|
url_is_usable = False
|
|
|
|
# Join from either a given base URL, e.g. redirections or following
|
|
|
|
# a relative link. If there is no such reference URL, try to guess
|
|
|
|
# what the user meant to do.
|
2021-05-08 22:41:42 +02:00
|
|
|
if base_url:
|
|
|
|
parts = parse_url(join_url(base_url, url))
|
2021-05-15 19:06:44 +02:00
|
|
|
url_is_usable = True
|
|
|
|
elif parts["scheme"] and parts["path"]:
|
|
|
|
guessed_url = parts["scheme"] + "://" + parts["path"]
|
2021-05-16 00:40:21 +02:00
|
|
|
if self.prompt(f"Do you mean '{guessed_url}'?") == "y":
|
2021-05-15 19:06:44 +02:00
|
|
|
parts = parse_url(guessed_url)
|
|
|
|
url_is_usable = True
|
|
|
|
# If nothing could be done, just give up.
|
|
|
|
if not url_is_usable:
|
2021-05-08 22:41:42 +02:00
|
|
|
self.set_status_error(f"Can't open '{url}'.")
|
|
|
|
return
|
|
|
|
|
2021-05-12 22:29:03 +02:00
|
|
|
# Replace URL passed as parameter by a sanitized one.
|
2021-05-08 22:41:42 +02:00
|
|
|
url = unparse_url(parts)
|
|
|
|
|
2021-05-12 22:29:03 +02:00
|
|
|
scheme = parts["scheme"]
|
2021-05-08 22:41:42 +02:00
|
|
|
if scheme == "gemini":
|
2021-03-28 18:28:35 +02:00
|
|
|
from bebop.browser.gemini import open_gemini_url
|
2021-05-08 22:41:42 +02:00
|
|
|
success = open_gemini_url(
|
2021-03-28 18:28:35 +02:00
|
|
|
self,
|
2021-05-08 22:41:42 +02:00
|
|
|
url,
|
2021-03-28 18:28:35 +02:00
|
|
|
redirects=redirects,
|
|
|
|
use_cache=use_cache
|
|
|
|
)
|
2021-05-08 22:41:42 +02:00
|
|
|
if history and success:
|
|
|
|
self.history.push(url)
|
|
|
|
|
|
|
|
elif scheme.startswith("http"):
|
2021-03-28 18:28:35 +02:00
|
|
|
from bebop.browser.web import open_web_url
|
|
|
|
open_web_url(self, url)
|
2021-05-08 22:41:42 +02:00
|
|
|
|
|
|
|
elif scheme == "file":
|
2021-03-28 18:28:35 +02:00
|
|
|
from bebop.browser.file import open_file
|
2021-05-08 22:41:42 +02:00
|
|
|
file_url = open_file(self, parts["path"])
|
|
|
|
if history and file_url:
|
|
|
|
self.history.push(file_url)
|
|
|
|
|
|
|
|
elif scheme == "bebop":
|
|
|
|
special_page = self.special_pages.get(parts["path"])
|
2021-04-18 16:10:48 +02:00
|
|
|
if special_page:
|
|
|
|
special_page["open"]()
|
|
|
|
else:
|
|
|
|
self.set_status_error("Unknown page.")
|
2021-05-08 22:41:42 +02:00
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
else:
|
2021-06-04 16:09:00 +02:00
|
|
|
from bebop.plugins import SchemePlugin
|
|
|
|
plugins = (p for p in self.plugins if isinstance(p, SchemePlugin))
|
|
|
|
plugin = next(filter(lambda p: p.scheme == scheme, plugins), None)
|
|
|
|
if plugin:
|
|
|
|
result_url = plugin.open_url(self, url)
|
|
|
|
if history and result_url:
|
|
|
|
self.history.push(result_url)
|
|
|
|
else:
|
|
|
|
self.set_status_error(f"Protocol '{scheme}' not supported.")
|
2021-02-12 19:01:42 +01:00
|
|
|
|
2021-03-13 20:37:13 +01:00
|
|
|
def load_page(self, page: Page):
|
2021-05-29 01:13:43 +02:00
|
|
|
"""Set this page as the current page and refresh appropriate windows."""
|
2021-03-13 20:37:13 +01:00
|
|
|
old_pad_height = self.page_pad.dim[0]
|
|
|
|
self.page_pad.show_page(page)
|
|
|
|
if self.page_pad.dim[0] < old_pad_height:
|
2021-02-12 23:29:51 +01:00
|
|
|
self.screen.clear()
|
|
|
|
self.screen.refresh()
|
|
|
|
self.refresh_windows()
|
|
|
|
else:
|
|
|
|
self.refresh_page()
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
def handle_digit_input(self, init_char: int):
|
2021-03-13 16:31:11 +01:00
|
|
|
"""Focus command-line to select the link ID to follow."""
|
2021-06-05 21:47:06 +02:00
|
|
|
if self.current_page is None:
|
2021-02-18 01:41:14 +01:00
|
|
|
return
|
2021-06-05 21:47:06 +02:00
|
|
|
links = self.current_page.links
|
2021-04-16 19:30:14 +02:00
|
|
|
if links is None:
|
|
|
|
return
|
2021-03-13 16:31:11 +01:00
|
|
|
err, val = self.command_line.focus_for_link_navigation(init_char, links)
|
|
|
|
if err == 0:
|
|
|
|
self.open_link(links, val) # type: ignore
|
|
|
|
elif err == 2:
|
|
|
|
self.set_status_error(val)
|
|
|
|
|
|
|
|
def open_link(self, links: Links, link_id: int):
|
2021-02-12 19:01:42 +01:00
|
|
|
"""Open the link with this link ID."""
|
2021-02-12 23:29:51 +01:00
|
|
|
if not link_id in links:
|
2021-02-15 19:57:49 +01:00
|
|
|
self.set_status_error(f"Unknown link ID {link_id}.")
|
2021-02-12 19:01:42 +01:00
|
|
|
return
|
2021-05-15 19:06:44 +02:00
|
|
|
self.open_url(links[link_id], base_url=self.current_url)
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
def handle_mouse(self, mouse_id: int, x: int, y: int, z: int, bstate: int):
|
|
|
|
"""Handle mouse events.
|
|
|
|
|
2021-06-02 00:33:19 +02:00
|
|
|
Vertical scrolling is handled, and clicking on links.
|
2021-02-12 19:01:42 +01:00
|
|
|
"""
|
|
|
|
if bstate & ButtonState.SCROLL_UP:
|
2021-02-15 18:51:45 +01:00
|
|
|
self.scroll_page_vertically(-3)
|
2021-02-12 19:01:42 +01:00
|
|
|
elif bstate & ButtonState.SCROLL_DOWN:
|
2021-02-15 18:51:45 +01:00
|
|
|
self.scroll_page_vertically(3)
|
2021-06-02 00:33:19 +02:00
|
|
|
elif bstate & ButtonState.LEFT_CLICKED:
|
|
|
|
self.handle_mouse_click(x, y)
|
|
|
|
|
|
|
|
def handle_mouse_click(self, x: int, y: int):
|
|
|
|
"""Handle a mouse click.
|
|
|
|
|
|
|
|
If the click is on a link (appropriate line and columns), open it.
|
|
|
|
"""
|
2021-06-05 21:47:06 +02:00
|
|
|
if not self.current_page:
|
2021-06-04 02:25:15 +02:00
|
|
|
return
|
2021-06-02 00:33:19 +02:00
|
|
|
px, py = self.page_pad.current_column, self.page_pad.current_line
|
|
|
|
line_pos = y + py
|
2021-06-05 21:47:06 +02:00
|
|
|
if line_pos >= len(self.current_page.metalines):
|
2021-06-02 00:33:19 +02:00
|
|
|
return
|
2021-06-05 21:47:06 +02:00
|
|
|
meta, line = self.current_page.metalines[line_pos]
|
2021-06-02 00:33:19 +02:00
|
|
|
if meta["type"] != LineType.LINK:
|
|
|
|
return
|
|
|
|
# "url" key is contained only in the first line of the link if its text
|
|
|
|
# is wrapped, so if the user did not click on the first line, rewind to
|
|
|
|
# get the URL.
|
|
|
|
while "url" not in meta:
|
|
|
|
line_pos -= 1
|
2021-06-05 21:47:06 +02:00
|
|
|
meta, line = self.current_page.metalines[line_pos]
|
2021-06-02 00:33:19 +02:00
|
|
|
url = meta["url"]
|
|
|
|
# The click is valid if it is on the link itself or the dimmed preview.
|
|
|
|
col_pos = x + px
|
|
|
|
if col_pos > len(line):
|
|
|
|
ch = self.page_pad.pad.instr(line_pos, col_pos, 1)
|
|
|
|
if ch == b' ':
|
|
|
|
return
|
|
|
|
self.open_url(url, base_url=self.current_url)
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
def handle_resize(self):
|
|
|
|
"""Try to not make everything collapse on resizes."""
|
|
|
|
# Refresh the whole screen before changing windows to avoid random
|
|
|
|
# blank screens.
|
|
|
|
self.screen.refresh()
|
|
|
|
old_dim = self.dim
|
|
|
|
self.dim = self.screen.getmaxyx()
|
|
|
|
# Avoid work if the resizing does not impact us.
|
|
|
|
if self.dim == old_dim:
|
|
|
|
return
|
|
|
|
# Resize windows to fit the new dimensions. Content pad will be updated
|
|
|
|
# on its own at the end of the function.
|
2021-02-12 23:29:51 +01:00
|
|
|
self.status_line.resize(*self.line_dim)
|
|
|
|
self.command_line.window.resize(*self.line_dim)
|
2021-02-12 19:01:42 +01:00
|
|
|
# Move the windows to their new position if that's still possible.
|
2021-02-12 23:29:51 +01:00
|
|
|
if self.status_line_pos[0] >= 0:
|
|
|
|
self.status_line.mvwin(*self.status_line_pos)
|
|
|
|
if self.command_line_pos[0] >= 0:
|
|
|
|
self.command_line.window.mvwin(*self.command_line_pos)
|
2021-02-12 19:01:42 +01:00
|
|
|
# If the content pad does not fit its whole place, we have to clean the
|
|
|
|
# gap between it and the status line. Refresh all screen.
|
2021-03-13 20:37:13 +01:00
|
|
|
if self.page_pad.dim[0] < self.h - 2:
|
2021-02-12 19:01:42 +01:00
|
|
|
self.screen.clear()
|
|
|
|
self.screen.refresh()
|
|
|
|
self.refresh_windows()
|
2021-02-13 23:58:49 +01:00
|
|
|
|
2021-02-18 19:01:28 +01:00
|
|
|
def scroll_page_vertically(self, by_lines):
|
2021-03-13 16:31:11 +01:00
|
|
|
"""Scroll page vertically.
|
|
|
|
|
|
|
|
If `by_lines` is an integer (positive or negative), scroll the page by
|
|
|
|
this amount of lines. If `by_lines` is one of the floats inf and -inf,
|
|
|
|
go to the end of file and beginning of file, respectively.
|
|
|
|
"""
|
2021-02-18 19:01:28 +01:00
|
|
|
window_height = self.h - 2
|
|
|
|
require_refresh = False
|
|
|
|
if by_lines == inf:
|
2021-03-13 20:37:13 +01:00
|
|
|
require_refresh = self.page_pad.go_to_end(window_height)
|
2021-02-18 19:01:28 +01:00
|
|
|
elif by_lines == -inf:
|
2021-03-13 20:37:13 +01:00
|
|
|
require_refresh = self.page_pad.go_to_beginning()
|
2021-02-18 19:01:28 +01:00
|
|
|
else:
|
2021-03-13 20:37:13 +01:00
|
|
|
require_refresh = self.page_pad.scroll_v(by_lines, window_height)
|
2021-02-18 19:01:28 +01:00
|
|
|
if require_refresh:
|
2021-02-15 18:51:45 +01:00
|
|
|
self.refresh_page()
|
|
|
|
|
2021-03-13 18:58:02 +01:00
|
|
|
def scroll_whole_page_down(self):
|
|
|
|
"""Scroll down by a whole page."""
|
|
|
|
self.scroll_page_vertically(self.page_pad_size[0])
|
|
|
|
|
|
|
|
def scroll_whole_page_up(self):
|
|
|
|
"""Scroll up by a whole page."""
|
|
|
|
self.scroll_page_vertically(-self.page_pad_size[0])
|
|
|
|
|
2021-02-18 19:01:28 +01:00
|
|
|
def scroll_page_horizontally(self, by_columns):
|
2021-04-17 22:02:06 +02:00
|
|
|
"""Scroll page horizontally.
|
|
|
|
|
|
|
|
If `by_lines` is an integer (positive or negative), scroll the page by
|
|
|
|
this amount of columns. If `by_lines` is -inf, scroll back to the first
|
|
|
|
column. Scrolling to the right-most column is not supported.
|
|
|
|
"""
|
|
|
|
if by_columns == -inf:
|
|
|
|
require_refresh = self.page_pad.go_to_first_column()
|
|
|
|
else:
|
|
|
|
require_refresh = self.page_pad.scroll_h(by_columns, self.w)
|
|
|
|
if require_refresh:
|
2021-02-15 18:51:45 +01:00
|
|
|
self.refresh_page()
|
|
|
|
|
2021-04-17 22:02:06 +02:00
|
|
|
def scroll_whole_page_left(self):
|
|
|
|
"""Scroll left by a whole page."""
|
|
|
|
self.scroll_page_horizontally(-self.page_pad_size[1])
|
|
|
|
|
|
|
|
def scroll_whole_page_right(self):
|
|
|
|
"""Scroll right by a whole page."""
|
|
|
|
self.scroll_page_horizontally(self.page_pad_size[1])
|
|
|
|
|
2021-02-18 01:41:02 +01:00
|
|
|
def reload_page(self):
|
2021-03-13 16:31:11 +01:00
|
|
|
"""Reload the page, if one has been previously loaded."""
|
2021-02-18 01:41:02 +01:00
|
|
|
if self.current_url:
|
2021-03-28 18:28:35 +02:00
|
|
|
self.open_url(self.current_url, history=False, use_cache=False)
|
2021-02-18 01:41:02 +01:00
|
|
|
|
2021-02-13 23:58:49 +01:00
|
|
|
def go_back(self):
|
|
|
|
"""Go back in history if possible."""
|
2021-05-09 00:46:26 +02:00
|
|
|
if self.current_url.startswith("bebop:"):
|
|
|
|
previous_url = self.history.get_previous(actual_previous=True)
|
|
|
|
else:
|
|
|
|
previous_url = self.history.get_previous()
|
2021-05-08 22:41:42 +02:00
|
|
|
if previous_url:
|
2021-05-14 23:15:22 +02:00
|
|
|
self.open_url(previous_url, history=False, use_cache=True)
|
2021-02-26 15:36:56 +01:00
|
|
|
|
2021-03-14 02:05:42 +01:00
|
|
|
def go_to_parent_page(self):
|
|
|
|
"""Go to the parent URL if possible."""
|
|
|
|
if self.current_url:
|
2021-03-28 18:28:35 +02:00
|
|
|
self.open_url(get_parent_url(self.current_url))
|
2021-03-14 02:05:42 +01:00
|
|
|
|
2021-03-16 19:38:11 +01:00
|
|
|
def go_to_root_page(self):
|
|
|
|
"""Go to the root URL if possible."""
|
|
|
|
if self.current_url:
|
2021-03-28 18:28:35 +02:00
|
|
|
self.open_url(get_root_url(self.current_url))
|
2021-03-18 01:56:24 +01:00
|
|
|
|
2021-05-09 00:46:26 +02:00
|
|
|
def open_internal_page(self, name, gemtext):
|
|
|
|
"""Open some content corresponding to a "bebop:" internal URL."""
|
2021-06-28 00:49:44 +02:00
|
|
|
page = Page.from_gemtext(gemtext, get_render_options(self.config))
|
2021-05-09 00:46:26 +02:00
|
|
|
self.load_page(page)
|
|
|
|
self.current_url = "bebop:" + name
|
|
|
|
|
2021-03-18 01:56:24 +01:00
|
|
|
def open_bookmarks(self):
|
|
|
|
"""Open bookmarks."""
|
|
|
|
content = get_bookmarks_document()
|
|
|
|
if content is None:
|
|
|
|
self.set_status_error("Failed to open bookmarks.")
|
|
|
|
return
|
2021-05-09 00:46:26 +02:00
|
|
|
self.open_internal_page("bookmarks", content)
|
2021-03-18 01:56:24 +01:00
|
|
|
|
|
|
|
def add_bookmark(self):
|
|
|
|
"""Add the current URL as bookmark."""
|
|
|
|
if not self.current_url:
|
|
|
|
return
|
2021-06-05 21:47:06 +02:00
|
|
|
current_title = self.current_page.title or ""
|
2021-05-12 22:29:03 +02:00
|
|
|
title = self.get_user_text_input(
|
|
|
|
"Bookmark title?",
|
2021-04-19 00:28:20 +02:00
|
|
|
CommandLine.CHAR_TEXT,
|
2021-05-12 22:29:03 +02:00
|
|
|
prefix=current_title,
|
|
|
|
strip=True,
|
2021-04-19 00:28:20 +02:00
|
|
|
)
|
2021-03-18 01:56:24 +01:00
|
|
|
if title:
|
2021-05-12 22:29:03 +02:00
|
|
|
save_bookmark(self.current_url, title)
|
2021-04-16 19:30:33 +02:00
|
|
|
|
|
|
|
def edit_page(self):
|
|
|
|
"""Open a text editor to edit the page source.
|
|
|
|
|
|
|
|
For external pages, the source is written in a temporary file, opened in
|
|
|
|
its editor of choice and so it's up to the user to save it where she
|
|
|
|
needs it, if needed. Internal pages, e.g. the bookmarks page, are loaded
|
|
|
|
directly from their location on disk.
|
|
|
|
"""
|
|
|
|
delete_source_after = False
|
2021-05-08 22:41:42 +02:00
|
|
|
parts = parse_url(self.current_url)
|
|
|
|
if parts["scheme"] == "bebop":
|
|
|
|
page_name = parts["path"]
|
2021-04-18 16:10:48 +02:00
|
|
|
special_pages_functions = self.special_pages.get(page_name)
|
|
|
|
if not special_pages_functions:
|
|
|
|
return
|
|
|
|
get_source = special_pages_functions.get("source")
|
|
|
|
source_filename = get_source() if get_source else None
|
2021-04-16 19:30:33 +02:00
|
|
|
else:
|
2021-06-05 21:47:06 +02:00
|
|
|
if not self.current_page:
|
2021-04-16 19:30:33 +02:00
|
|
|
return
|
2021-06-05 21:47:06 +02:00
|
|
|
source = self.current_page.source
|
2021-04-16 19:30:33 +02:00
|
|
|
with tempfile.NamedTemporaryFile("wt", delete=False) as source_file:
|
|
|
|
source_file.write(source)
|
|
|
|
source_filename = source_file.name
|
|
|
|
delete_source_after = True
|
|
|
|
|
2021-04-18 16:10:48 +02:00
|
|
|
if not source_filename:
|
|
|
|
return
|
|
|
|
|
2021-04-18 02:27:05 +02:00
|
|
|
command = self.config["source_editor"] + [source_filename]
|
2021-06-03 19:08:54 +02:00
|
|
|
success = open_external_program(command)
|
|
|
|
if not success:
|
|
|
|
self.set_status_error("Could not open editor.")
|
|
|
|
|
2021-04-16 19:30:33 +02:00
|
|
|
if delete_source_after:
|
|
|
|
os.unlink(source_filename)
|
2021-04-17 21:54:11 +02:00
|
|
|
self.refresh_windows()
|
2021-04-18 16:10:48 +02:00
|
|
|
|
|
|
|
def open_help(self):
|
|
|
|
"""Show the help page."""
|
2021-06-11 15:48:49 +02:00
|
|
|
self.open_internal_page("help", get_help(self.config, self.plugins))
|
2021-04-19 00:28:20 +02:00
|
|
|
|
2021-05-16 00:40:21 +02:00
|
|
|
def prompt(self, text: str, keys: str ="yn"):
|
2021-04-19 00:28:20 +02:00
|
|
|
"""Display the text and allow it to type one of the given keys."""
|
2021-05-15 19:06:44 +02:00
|
|
|
choice = "/".join(keys)
|
|
|
|
self.set_status(f"{text} [{choice}]")
|
2021-04-19 00:28:20 +02:00
|
|
|
return self.command_line.prompt_key(keys)
|
2021-05-08 22:41:42 +02:00
|
|
|
|
|
|
|
def open_history(self):
|
|
|
|
"""Show a generated history of visited pages."""
|
2021-05-09 00:46:26 +02:00
|
|
|
self.open_internal_page("history", self.history.to_gemtext())
|
2021-05-09 01:39:33 +02:00
|
|
|
|
|
|
|
def open_last_download(self):
|
|
|
|
"""Open the last downloaded file."""
|
|
|
|
if not self.last_download:
|
|
|
|
return
|
|
|
|
mime_type, path = self.last_download
|
2021-06-11 18:25:56 +02:00
|
|
|
main_type = mime_type.main_type if mime_type else ""
|
|
|
|
command = self.config["external_commands"].get(main_type)
|
2021-05-09 01:39:33 +02:00
|
|
|
if not command:
|
|
|
|
command = self.config["external_command_default"]
|
|
|
|
command = command + [str(path)]
|
|
|
|
self.set_status(f"Running '{' '.join(command)}'...")
|
|
|
|
try:
|
|
|
|
subprocess.Popen(
|
|
|
|
command,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
start_new_session=True
|
|
|
|
)
|
|
|
|
except FileNotFoundError as exc:
|
|
|
|
self.set_status_error(f"Failed to run command: {exc}")
|
2021-05-15 19:44:50 +02:00
|
|
|
|
|
|
|
def open_home(self):
|
|
|
|
"""Open the home page."""
|
|
|
|
self.open_url(self.config["home"])
|
|
|
|
|
|
|
|
def open_welcome_page(self):
|
|
|
|
"""Open the default welcome page."""
|
|
|
|
self.open_internal_page("welcome", WELCOME_PAGE)
|
2021-05-24 20:09:34 +02:00
|
|
|
|
|
|
|
def show_page_info(self):
|
|
|
|
"""Show some page informations in the status bar."""
|
2021-06-05 21:47:06 +02:00
|
|
|
page = self.current_page
|
2021-06-04 02:25:15 +02:00
|
|
|
if not page:
|
|
|
|
return
|
2021-05-24 20:09:34 +02:00
|
|
|
mime = page.mime.short if page.mime else "(unknown MIME type)"
|
|
|
|
encoding = page.encoding or "(unknown encoding)"
|
|
|
|
size = f"{len(page.source)} chars"
|
2021-06-04 02:25:15 +02:00
|
|
|
lines = f"{len(page.metalines)} lines"
|
|
|
|
info = f"{mime} {encoding} {size} {lines}"
|
2021-05-24 20:09:34 +02:00
|
|
|
self.set_status(info)
|
2021-05-29 01:13:43 +02:00
|
|
|
|
|
|
|
def set_render_mode(self, mode):
|
|
|
|
"""Set the render mode for the current path or capsule."""
|
|
|
|
if mode not in RENDER_MODES:
|
|
|
|
valid_modes = ", ".join(RENDER_MODES)
|
|
|
|
self.set_status_error("Valid render modes are: " + valid_modes)
|
|
|
|
return
|
|
|
|
url = self.get_user_text_input(
|
|
|
|
f"Set '{mode}' render mode for which URL (includes children)?",
|
|
|
|
CommandLine.CHAR_TEXT,
|
|
|
|
prefix=self.current_url,
|
|
|
|
strip=True
|
|
|
|
)
|
|
|
|
if not url:
|
|
|
|
return
|
|
|
|
prefs = self.capsule_prefs.get(url, {})
|
|
|
|
prefs["render_mode"] = mode
|
|
|
|
self.capsule_prefs[url] = prefs
|
|
|
|
save_capsule_prefs(self.capsule_prefs, get_capsule_prefs_path())
|
|
|
|
self.reload_page()
|
2021-05-29 01:26:15 +02:00
|
|
|
|
|
|
|
def toggle_render_mode(self):
|
|
|
|
"""Switch to the next render mode for the current page."""
|
2021-06-05 21:47:06 +02:00
|
|
|
page = self.current_page
|
2021-06-28 00:49:44 +02:00
|
|
|
if not page or page.render_opts is None:
|
2021-06-04 02:25:15 +02:00
|
|
|
return
|
2021-06-28 00:49:44 +02:00
|
|
|
render_opts = page.render_opts
|
|
|
|
current_mode = render_opts.mode
|
|
|
|
if current_mode not in RENDER_MODES:
|
2021-05-29 01:26:15 +02:00
|
|
|
next_mode = RENDER_MODES[0]
|
|
|
|
else:
|
2021-06-28 00:49:44 +02:00
|
|
|
cur_mod_index = RENDER_MODES.index(current_mode)
|
2021-05-29 01:26:15 +02:00
|
|
|
next_mode = RENDER_MODES[(cur_mod_index + 1) % len(RENDER_MODES)]
|
2021-06-28 00:49:44 +02:00
|
|
|
render_opts.mode = next_mode
|
|
|
|
new_page = Page.from_gemtext(page.source, render_opts)
|
2021-05-29 01:26:15 +02:00
|
|
|
self.load_page(new_page)
|
|
|
|
self.set_status(f"Using render mode '{next_mode}'.")
|
2021-06-04 02:25:15 +02:00
|
|
|
|
|
|
|
def search_in_page(self):
|
|
|
|
"""Search for words in the page."""
|
2021-06-05 21:47:06 +02:00
|
|
|
if not self.current_page:
|
2021-06-04 02:25:15 +02:00
|
|
|
return
|
|
|
|
search = self.get_user_text_input("Search", CommandLine.CHAR_TEXT)
|
|
|
|
if not search:
|
|
|
|
return
|
|
|
|
self.search_res_lines = []
|
2021-06-05 21:47:06 +02:00
|
|
|
for index, (_, line) in enumerate(self.current_page.metalines):
|
2021-06-04 02:25:15 +02:00
|
|
|
if search in line:
|
|
|
|
self.search_res_lines.append(index)
|
|
|
|
if self.search_res_lines:
|
|
|
|
self.move_to_search_result(Browser.SEARCH_NEXT)
|
|
|
|
else:
|
|
|
|
self.set_status(f"'{search}' not found.")
|
|
|
|
|
|
|
|
def move_to_search_result(self, prev_or_next: int):
|
|
|
|
"""Move to the next or previous search result."""
|
|
|
|
current_line = self.page_pad.current_line
|
|
|
|
next_line = None
|
|
|
|
index = 1
|
|
|
|
max_index = len(self.search_res_lines)
|
|
|
|
if prev_or_next == Browser.SEARCH_NEXT:
|
|
|
|
for line in self.search_res_lines:
|
|
|
|
if line > current_line:
|
|
|
|
next_line = line
|
|
|
|
break
|
|
|
|
index += 1
|
|
|
|
elif prev_or_next == Browser.SEARCH_PREVIOUS:
|
|
|
|
index = max_index
|
|
|
|
for line in reversed(self.search_res_lines):
|
|
|
|
if line < current_line:
|
|
|
|
next_line = line
|
|
|
|
break
|
|
|
|
index -= 1
|
|
|
|
if next_line is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.set_status(f"Result {index}/{max_index}")
|
2021-06-04 19:00:13 +02:00
|
|
|
max_line = self.page_pad.get_max_line(self.h)
|
|
|
|
self.page_pad.current_line = min(next_line, max_line)
|
2021-06-04 02:25:15 +02:00
|
|
|
self.refresh_windows()
|
2021-06-04 16:09:00 +02:00
|
|
|
|
|
|
|
def load_plugins(self):
|
|
|
|
"""Load installed and configured plugins."""
|
|
|
|
for plugin_name in self.config["enabled_plugins"]:
|
|
|
|
module_name = f"bebop_{plugin_name}"
|
|
|
|
|
|
|
|
try:
|
|
|
|
module = import_module(module_name)
|
|
|
|
except ImportError as exc:
|
|
|
|
logging.error(f"Could not load module {module_name}: {exc}")
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.plugins.append(module.plugin) # type: ignore
|
|
|
|
except AttributeError:
|
|
|
|
logging.error(f"Module {module_name} does not export a plugin.")
|
|
|
|
continue
|
|
|
|
|
|
|
|
logging.info(f"Loaded plugin {plugin_name}.")
|