1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#!/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+c,ctrl+q", "app.quit", "Quit", show=True),
]
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("🔙"), # ⏪
Button("🔝", name='../'), # ⏫
Button("🔜"), # ⏩
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)
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)
else:
url = event.button.name
if not ":" in url:
url = GeminiProtocol.relativeURL(url, self.url)
input = self.query_one("#url")
input.value = url
self._do_url(url)
def _do_url(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()
|