#!/usr/bin/env python3 from textual.app import App, ComposeResult from textual.widgets import Input, Button, Static, Footer from textual.containers import Container from rich.markdown import Markdown from textual.binding import Binding from gemtext import Gemtext from protocol.gemini import GeminiProtocol class Browset(App): url = "" CSS_PATH = "browset.css" BINDINGS = [ Binding("ctrl+q,ctrl+c", "app.quit", "Quit", show=True), Binding("ctrl+left", "back()", "Back", show=False), Binding("ctrl+right", "soon()", "Soon", show=False), ] history = [] fistory = [] # forward history content = ["#h1", "## hey [b]Is this unformatted?[/b]", "```startpre","in preformatted text this hsould have a scrollbar if it is too wide oh yeah tonight is gonna be great.", "``` (ended pre)", "afterward"] def compose(self) -> ComposeResult: yield Footer() yield Container( Button("🔙", variant='primary', name='back'), # ⏪ Button("🔝", name='../'), # ⏫ Button("🔜", variant='primary', name='soon'), # ⏩ Button("🔄", variant='primary', name='refresh'), # 🔁 Input(placeholder="Enter URI", id="url"), id="toolbar" ) yield Gemtext(fp=self.content, id="content") async def on_input_submitted(self, message: Input.Submitted) -> None: self._do_url(message.value, setbar=False) async def on_button_pressed(self, event: Button.Pressed) -> None: if event.button.variant == 'primary': if event.button.name == 'refresh': self._do_url(self.url) elif event.button.name == 'back': self.action_back() elif event.button.name == 'soon': self.action_soon() else: url = event.button.name if not ":" in url: url = GeminiProtocol.relativeURL(url, self.url) self._do_url(url) def action_back(self): if len(self.history): self.fistory.append(self.url) url = self.history.pop() self._do_url(url, histore=False, clearF=False) def action_soon(self): if len(self.fistory): url = self.fistory.pop() self._do_url(url, clearF=False) def _do_url(self, url, histore=True, setbar=True, clearF=True): if clearF: self.fistory = [] if setbar: input = self.query_one("#url") input.value = url if histore: self.history.append(self.url) self.url = url (mime, fp) = GeminiProtocol.get(url) self.query_one("#content").remove() self.mount(Gemtext(fp=fp, id="content")) if __name__ == "__main__": app = Browset() app.run()