89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""
|
|
haTerm - v0.1
|
|
(c) Chiara Wohlwend
|
|
|
|
haTerm is a Terminal Based hafas client, using the ivb endpoint.
|
|
Prompt_toolkit (https://github.com/prompt-toolkit/python-prompt-toolkit) will be used to render a terminal user interface (TUI).
|
|
The application will provide routing information and station departures/arrivals.
|
|
"""
|
|
|
|
import backend
|
|
from prompt_toolkit import Application
|
|
from prompt_toolkit.buffer import Buffer
|
|
from prompt_toolkit.layout.layout import Layout
|
|
from prompt_toolkit.layout.containers import Window, VSplit, HSplit
|
|
from prompt_toolkit.layout.controls import BufferControl
|
|
from prompt_toolkit.key_binding import KeyBindings
|
|
import threading
|
|
import time
|
|
|
|
|
|
def run_station_monitor(hafas: backend.HafasClient | None = None):
|
|
"""Run the station monitor TUI.
|
|
|
|
Parameters:
|
|
- hafas: optional shared HafasClient instance. If omitted, a new one is
|
|
created. Prefer passing the shared client from `main.py`.
|
|
|
|
Note: Some parts of this monitor are incomplete; see TODO markers.
|
|
"""
|
|
if hafas is None:
|
|
hafas = backend.HafasClient()
|
|
|
|
keyB = KeyBindings()
|
|
inputBuffer = Buffer()
|
|
resultBuffer = Buffer()
|
|
stop_event = threading.Event()
|
|
app_state = "MONITOR"
|
|
|
|
inputBuffer.text = "Haltestelle eingeben: "
|
|
inputBuffer.cursor_position = len(inputBuffer.text)
|
|
resultBuffer.text = "Ausgabe: "
|
|
|
|
root_container = HSplit(children=[
|
|
VSplit(children=[
|
|
Window(content=BufferControl(buffer=inputBuffer, focusable=True)),
|
|
Window(width=1, char='│', style="fg:#9D1D75"),
|
|
Window(content=BufferControl(buffer=resultBuffer, focusable=True)),
|
|
]),
|
|
Window(height=1, char='-', style="bg:#9D1D75 fg:#FFFFFF"),
|
|
])
|
|
|
|
layout = Layout(root_container, focused_element=inputBuffer)
|
|
|
|
@keyB.add("enter")
|
|
def handle_enter(event):
|
|
global app_state
|
|
|
|
user_input = inputBuffer.text.replace("Haltestelle eingeben: ", "").strip()
|
|
|
|
if user_input:
|
|
app_state = "RESULTS"
|
|
# TODO: handle case of no results gracefully
|
|
results = hafas.getStationNames(user_input)
|
|
inputBuffer.insert_line_below()
|
|
for station in results:
|
|
inputBuffer.insert_text(f"\n {station[0]}")
|
|
resultBuffer.text = f"Ergebnisse für: {results[0][1]}\n\n"
|
|
station = results[0][1] if results else "Keine Ergebnisse gefunden."
|
|
departures = hafas.getArrDep(station, arrdep="ARR", count=3)
|
|
resultBuffer.text = "\n".join(
|
|
[resultBuffer.text.rstrip()] + [f" {entry['departure_time']} {entry['line']} {entry['name']}" for entry in departures]
|
|
)
|
|
|
|
else:
|
|
app_state = "MONITOR"
|
|
|
|
@keyB.add("c-q")
|
|
def exit_(event):
|
|
stop_event.set()
|
|
event.app.exit()
|
|
|
|
app = Application(layout=layout, full_screen=True, key_bindings=keyB)
|
|
|
|
try:
|
|
app.run()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
stop_event.set()
|