Compare commits

...

2 Commits

1 changed files with 27 additions and 18 deletions

45
nex.py
View File

@ -1,9 +1,9 @@
import sys import sys
import socket import socket
import pytermgui as ptg
from typing import Tuple
import re import re
import logging import logging
import pytermgui as ptg
from typing import Tuple
PORT = 1900 PORT = 1900
@ -58,22 +58,28 @@ class Page:
@property @property
def title(self): def title(self):
return f'nex://{self.url}' prefix = 'nex://'
if self.url.startswith(prefix):
return self.url[len(prefix):]
return self.url
def _parse_links(self): def _parse_links(self):
pattern = re.compile(r'=>\s*([\w/:\.~-]*)\s*') pattern = re.compile(r'=>\s*([\w/:\.~-]*)\s*')
self.links = pattern.findall(self.raw_content) self.links = pattern.findall(self.raw_content)
landing_page = Page('') def create_landing_page() -> Page:
landing_page.raw_content = 'Welcome to PyNex' landing_page = Page('about:home')
landing_page.content = landing_page.raw_content landing_page.raw_content = 'Welcome to PyNex'
landing_page.content = landing_page.raw_content
return landing_page
class Session: class Session:
def __init__(self): def __init__(self):
self.pages = {'': landing_page} home_page = create_landing_page()
self.history = [''] self.pages = {home_page.url: home_page}
self.history = [home_page.url]
self.curr_index = 0 self.curr_index = 0
@property @property
@ -107,17 +113,17 @@ class Browser(ptg.WindowManager):
super().__init__() super().__init__()
self.layout = ptg.Layout() self.layout = ptg.Layout()
self.session = session self.session = session
self.page_container = ptg.Container(self.session.current_page.content) self.page_container = ptg.Container(self.session.current_page.content)
self.body = ptg.Window(self.page_container, self.body = ptg.Window(self.page_container,
overflow=ptg.Overflow.SCROLL, overflow=ptg.Overflow.SCROLL,
vertical_align=ptg.VerticalAlignment.TOP) vertical_align=ptg.VerticalAlignment.TOP)
self.footer = ptg.Window( self.footer = ptg.Window(
ptg.Splitter( ptg.Splitter(
ptg.KeyboardButton("Back", self.back, bound='b'), ["(B)ack", self.back],
ptg.KeyboardButton("Forward", self.forward, bound='f'), ["(F)orward", self.forward],
ptg.KeyboardButton("Reload", self.reload, bound='r'), ["(R)eload", self.reload],
ptg.KeyboardButton("Quit", self.stop, bound='q') ["(Q)uit", lambda *_: self.stop()]
), ),
box="EMPTY", box="EMPTY",
is_persistant=True is_persistant=True
@ -128,10 +134,13 @@ class Browser(ptg.WindowManager):
self.update() self.update()
def _create_key_bindings(self): def _create_key_bindings(self):
self.bind('j', lambda *_: self.body.scroll(5))
self.bind(ptg.keys.DOWN, lambda *_: self.body.scroll(5)) self.bind(ptg.keys.DOWN, lambda *_: self.body.scroll(5))
self.bind('k', lambda *_: self.body.scroll(-5))
self.bind(ptg.keys.UP, lambda *_: self.body.scroll(-5)) self.bind(ptg.keys.UP, lambda *_: self.body.scroll(-5))
self.bind('j', lambda *_: self.body.scroll(5))
self.bind('k', lambda *_: self.body.scroll(-5))
self.bind('b', self.back)
self.bind('f', self.forward)
self.bind('r', self.reload)
self.bind('q', lambda *_: self.stop()) self.bind('q', lambda *_: self.stop())
for i in range(10): for i in range(10):
@ -142,13 +151,13 @@ class Browser(ptg.WindowManager):
self.layout.add_break() self.layout.add_break()
self.layout.add_slot("Footer", height=1) self.layout.add_slot("Footer", height=1)
# self.add(self.header)
self.add(self.body) self.add(self.body)
self.add(self.footer) self.add(self.footer)
def update(self): def update(self):
self.page_container.set_widgets([self.session.current_page.content]) page = self.session.current_page
self.body.set_title(f'[bold tertiary]{self.session.current_page.title}', 1) self.page_container.set_widgets([page.content])
self.body.set_title(f'[bold tertiary]{page.title}', 1)
def back(self, *args): def back(self, *args):
self.session.prev() self.session.prev()