browser: add a keybind to toggle render modes

This commit is contained in:
dece 2021-05-29 01:26:15 +02:00
parent 4192c2a84d
commit d42a294516
3 changed files with 24 additions and 1 deletions

View file

@ -228,6 +228,8 @@ class Browser:
self.edit_page() self.edit_page()
elif char == ord("y"): elif char == ord("y"):
self.open_history() self.open_history()
elif char == ord("§"):
self.toggle_render_mode()
elif curses.ascii.isdigit(char): elif curses.ascii.isdigit(char):
self.handle_digit_input(char) self.handle_digit_input(char)
elif char == curses.KEY_MOUSE: elif char == curses.KEY_MOUSE:
@ -705,3 +707,21 @@ class Browser:
self.capsule_prefs[url] = prefs self.capsule_prefs[url] = prefs
save_capsule_prefs(self.capsule_prefs, get_capsule_prefs_path()) save_capsule_prefs(self.capsule_prefs, get_capsule_prefs_path())
self.reload_page() 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}'.")

View file

@ -37,6 +37,7 @@ Keybinds using the SHIFT key are written uppercase. Keybinds using the ALT (or M
* y: open history * y: open history
* digits: go to the corresponding link ID * digits: go to the corresponding link ID
* escape: reset status line text * escape: reset status line text
* section sign (§): toggle between render modes for the current page
* C-c: cancel current operation * C-c: cancel current operation
## Commands ## Commands

View file

@ -21,6 +21,7 @@ class Page:
- title: optional page title. - title: optional page title.
- mime: optional MIME type received from the server. - mime: optional MIME type received from the server.
- encoding: optional encoding received from the server. - encoding: optional encoding received from the server.
- render: optional render mode used to create the page from Gemtext.
""" """
source: str source: str
metalines: list = field(default_factory=list) metalines: list = field(default_factory=list)
@ -28,6 +29,7 @@ class Page:
title: str = "" title: str = ""
mime: Optional[MimeType] = None mime: Optional[MimeType] = None
encoding: str = "" encoding: str = ""
render: Optional[str] = None
@staticmethod @staticmethod
def from_gemtext(gemtext: str, wrap_at: int, render: str ="fancy"): def from_gemtext(gemtext: str, wrap_at: int, render: str ="fancy"):
@ -35,7 +37,7 @@ class Page:
dumb_mode = render == "dumb" dumb_mode = render == "dumb"
elements, links, title = parse_gemtext(gemtext, dumb=dumb_mode) elements, links, title = parse_gemtext(gemtext, dumb=dumb_mode)
metalines = generate_metalines(elements, wrap_at, 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 @staticmethod
def from_text(text: str): def from_text(text: str):