117 lines
3.6 KiB
Python
117 lines
3.6 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()
|
|
|
|
prefixBuffer = Buffer()
|
|
prefixBuffer.text = "Haltestelle eingeben: "
|
|
prefixBuffer.read_only = True
|
|
|
|
userinputBuffer = Buffer()
|
|
|
|
arrivalBuffer = Buffer()
|
|
departureBuffer = Buffer()
|
|
stop_event = threading.Event()
|
|
app_state = "MONITOR"
|
|
|
|
arrivalBuffer.text = "Ankünfte:\n"
|
|
departureBuffer.text = "Abfahrten:\n"
|
|
|
|
|
|
|
|
root_container = HSplit(children=[
|
|
VSplit(children=[
|
|
Window(content=BufferControl(buffer=prefixBuffer, focusable=False),width=22),
|
|
Window(content=BufferControl(buffer=userinputBuffer, focusable=True)),
|
|
Window(width=1, char='│', style="fg:#9D1D75"),
|
|
|
|
HSplit(children=[
|
|
Window(content=BufferControl(buffer=arrivalBuffer, focusable=True)),
|
|
Window(height=1, char='-', style="fg:#9D1D75"),
|
|
Window(content=BufferControl(buffer=departureBuffer, focusable=True)),
|
|
]),
|
|
]),
|
|
Window(height=1, char='-', style="bg:#9D1D75 fg:#FFFFFF"),
|
|
])
|
|
|
|
layout = Layout(root_container, focused_element=userinputBuffer)
|
|
|
|
@keyB.add("enter")
|
|
def handle_enter(event):
|
|
global app_state
|
|
|
|
user_input = userinputBuffer.text.strip()
|
|
|
|
if user_input:
|
|
app_state = "RESULTS"
|
|
results = hafas.getStationNames(user_input)
|
|
|
|
if not results:
|
|
arrivalBuffer.text = f"Keine Stationen gefunden für: {user_input}"
|
|
departureBuffer.text= ""
|
|
userinputBuffer.text=""
|
|
app_state = "MONITOR"
|
|
|
|
else:
|
|
station_name = results[0][1]
|
|
station_id = results[0][0]
|
|
|
|
arrivals = hafas.getArrDep(station_id, arrdep="ARR", count=3)
|
|
arrivalBuffer.text = f"Ankünfte für {station_name}:\n"
|
|
for arrival in arrivals:
|
|
arrivalBuffer.insert_text(f"\n {arrival}")
|
|
|
|
departures = hafas.getArrDep(station_id, arrdep="DEP", count=3)
|
|
departureBuffer.text = f"Abfahrten für {station_name}:\n"
|
|
for departure in departures:
|
|
departureBuffer.insert_text(f"\n {departure}")
|
|
|
|
|
|
userinputBuffer.text = ""
|
|
|
|
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()
|