mirror of
https://github.com/onyx-and-iris/vban-tui.git
synced 2026-04-16 14:03:41 +00:00
initial commit
This commit is contained in:
89
src/vban_tui/tui.py
Normal file
89
src/vban_tui/tui.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import vban_cmd
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.containers import Grid
|
||||
from textual.widgets import RichLog
|
||||
|
||||
from .hybrid import LabelInput
|
||||
from .settings import Settings
|
||||
|
||||
|
||||
class VbanTui(App):
|
||||
"""A Textual App to display VBAN data."""
|
||||
|
||||
CSS_PATH = 'tui.tcss'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._settings = Settings()
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Create child widgets for the app."""
|
||||
yield Grid(
|
||||
LabelInput(
|
||||
'VBAN Host:',
|
||||
'localhost',
|
||||
id='host-labelinput',
|
||||
label_id='host-label',
|
||||
input_id='host-input',
|
||||
),
|
||||
LabelInput(
|
||||
'VBAN Port:',
|
||||
'6980',
|
||||
id='port-labelinput',
|
||||
label_id='port-label',
|
||||
input_id='port-input',
|
||||
),
|
||||
LabelInput(
|
||||
'VBAN Stream Name:',
|
||||
'Command1',
|
||||
id='streamname-labelinput',
|
||||
label_id='streamname-label',
|
||||
input_id='streamname-input',
|
||||
),
|
||||
LabelInput(
|
||||
'VBAN Command:',
|
||||
'Enter request',
|
||||
id='request-labelinput',
|
||||
label_id='request-label',
|
||||
input_id='request-input',
|
||||
),
|
||||
RichLog(id='response-log'),
|
||||
id='main-grid',
|
||||
)
|
||||
|
||||
def on_mount(self):
|
||||
"""Focus the request input on mount."""
|
||||
self.query_one('#host-input').value = self._settings.host
|
||||
self.query_one('#port-input').value = str(self._settings.port)
|
||||
self.query_one('#streamname-input').value = self._settings.streamname
|
||||
|
||||
self.query_one('#request-input').focus()
|
||||
|
||||
def on_key(self, event):
|
||||
"""Handle key events."""
|
||||
match event.key:
|
||||
case 'q':
|
||||
self.exit()
|
||||
case 'enter' if self.query_one('#request-input').has_focus:
|
||||
self.query_one('#response-log').clear()
|
||||
request_input = self.query_one('#request-input')
|
||||
request_input.add_class('request-sent')
|
||||
self.send_request()
|
||||
self.set_timer(0.5, lambda: request_input.remove_class('request-sent'))
|
||||
|
||||
def send_request(self):
|
||||
with vban_cmd.api(
|
||||
'potato',
|
||||
host=self.query_one('#host-input').value,
|
||||
port=int(self.query_one('#port-input').value),
|
||||
streamname=self.query_one('#streamname-input').value,
|
||||
disable_rt_listeners=True,
|
||||
) as vban:
|
||||
if response := vban.sendtext(self.query_one('#request-input').value):
|
||||
self.query_one('#response-log').write(response)
|
||||
|
||||
|
||||
def main():
|
||||
"""Run the VBAN TUI application."""
|
||||
app = VbanTui()
|
||||
app.run()
|
||||
Reference in New Issue
Block a user