From d42a294516b2ddd4a13a6f5161179ee3867167f6 Mon Sep 17 00:00:00 2001 From: dece Date: Sat, 29 May 2021 01:26:15 +0200 Subject: [PATCH] browser: add a keybind to toggle render modes --- bebop/browser/browser.py | 20 ++++++++++++++++++++ bebop/help.py | 1 + bebop/page.py | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/bebop/browser/browser.py b/bebop/browser/browser.py index 08cad97..16f79d6 100644 --- a/bebop/browser/browser.py +++ b/bebop/browser/browser.py @@ -228,6 +228,8 @@ class Browser: self.edit_page() elif char == ord("y"): self.open_history() + elif char == ord("§"): + self.toggle_render_mode() elif curses.ascii.isdigit(char): self.handle_digit_input(char) elif char == curses.KEY_MOUSE: @@ -705,3 +707,21 @@ class Browser: self.capsule_prefs[url] = prefs save_capsule_prefs(self.capsule_prefs, get_capsule_prefs_path()) self.reload_page() + + def toggle_render_mode(self): + """Switch to the next render mode for the current page.""" + if not self.page_pad or not self.page_pad.current_page: + return + page = self.page_pad.current_page + if page.render is None or page.render not in RENDER_MODES: + next_mode = RENDER_MODES[0] + else: + cur_mod_index = RENDER_MODES.index(page.render) + next_mode = RENDER_MODES[(cur_mod_index + 1) % len(RENDER_MODES)] + new_page = Page.from_gemtext( + page.source, + wrap_at=self.config["text_width"], + render=next_mode + ) + self.load_page(new_page) + self.set_status(f"Using render mode '{next_mode}'.") diff --git a/bebop/help.py b/bebop/help.py index ab16e74..a8feea2 100644 --- a/bebop/help.py +++ b/bebop/help.py @@ -37,6 +37,7 @@ Keybinds using the SHIFT key are written uppercase. Keybinds using the ALT (or M * y: open history * digits: go to the corresponding link ID * escape: reset status line text +* section sign (§): toggle between render modes for the current page * C-c: cancel current operation ## Commands diff --git a/bebop/page.py b/bebop/page.py index 7c58b4f..f75ed3e 100644 --- a/bebop/page.py +++ b/bebop/page.py @@ -21,6 +21,7 @@ class Page: - title: optional page title. - mime: optional MIME type received from the server. - encoding: optional encoding received from the server. + - render: optional render mode used to create the page from Gemtext. """ source: str metalines: list = field(default_factory=list) @@ -28,6 +29,7 @@ class Page: title: str = "" mime: Optional[MimeType] = None encoding: str = "" + render: Optional[str] = None @staticmethod def from_gemtext(gemtext: str, wrap_at: int, render: str ="fancy"): @@ -35,7 +37,7 @@ class Page: dumb_mode = render == "dumb" elements, links, title = parse_gemtext(gemtext, dumb=dumb_mode) metalines = generate_metalines(elements, wrap_at, dumb=dumb_mode) - return Page(gemtext, metalines, links, title) + return Page(gemtext, metalines, links, title, render=render) @staticmethod def from_text(text: str):