This commit is contained in:
ChiaraW1 2026-06-03 10:56:31 +02:00
commit ef9422fcc5
4 changed files with 184 additions and 63 deletions

View file

@ -13,7 +13,7 @@ from datetime import datetime
from prompt_toolkit import Application
from prompt_toolkit.completion import WordCompleter
class hafasClient:
class HafasClient:
def __init__(self):
self.session = requests.session()
self.clientInfo = {"id": "VAO", "name": "webapp", "type": "WEB"}
@ -90,5 +90,5 @@ class hafasClient:
if __name__ == '__main__':
client = hafasClient()
client = HafasClient()
client.runTests()

87
main.py
View file

@ -6,3 +6,90 @@ 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.
"""
from backend import HafasClient
import route_planning
import station_monitor
from prompt_toolkit import Application
from prompt_toolkit.layout import Layout, HSplit, VSplit, Window
from prompt_toolkit.widgets import Button, Box
from prompt_toolkit.key_binding import KeyBindings
def pk_menu(client: HafasClient):
"""Display a centered menu with two buttons selectable by arrow keys.
The function runs a full-screen prompt_toolkit application. Left/right
arrows move focus between the buttons; Enter activates the focused
button. The function returns the chosen mode as a string or None when
the user quits.
"""
choice = {'value': None}
def on_rp():
choice['value'] = 'rp'
app.exit()
def on_sm():
choice['value'] = 'sm'
app.exit()
btn_rp = Button("Route Planning", handler=on_rp, width=20)
btn_sm = Button("Station Monitor", handler=on_sm, width=20)
buttons = [btn_rp, btn_sm]
index = {'i': 0}
kb = KeyBindings()
def focus_current():
app.layout.focus(buttons[index['i']])
@kb.add('left')
def _left(event):
index['i'] = max(0, index['i'] - 1)
focus_current()
@kb.add('right')
def _right(event):
index['i'] = min(len(buttons) - 1, index['i'] + 1)
focus_current()
@kb.add('q')
@kb.add('c-q')
@kb.add('c-c')
def _quit(event):
app.exit()
root = HSplit(children=[
Window(),
Box(VSplit(children=[
Window(),
Box(VSplit(children=buttons, padding=6), padding=2, style="bg:#2A71D5 fg:#FFFFFF"),
Window()
])),
Window()
], align='CENTER')
app = Application(layout=Layout(root, focused_element=btn_rp), key_bindings=kb, full_screen=True)
app.run()
return choice['value']
def main():
client = HafasClient()
while True:
result = pk_menu(client)
if result == 'rp':
route_planning.run_route_planning(client)
elif result == 'sm':
station_monitor.run_station_monitor(client)
else:
print("Exiting.")
break
if __name__ == '__main__':
main()

View file

@ -5,10 +5,16 @@ haTerm - v0.1
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.
"""
#routenplanung mit promt tool kit
import prompt_toolkit as pk
Route planning UI.
Wrapped in a function so it can be launched from `main.py` with a shared
`HafasClient` instance from `backend.py`.
Note: This module is a minimal TUI stub. Many features are not implemented
yet (station lookup, route rendering). Add TODOs where appropriate.
"""
import time
from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
@ -17,25 +23,39 @@ from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.key_binding import KeyBindings
kb = KeyBindings()
startBuffer = Buffer()
endBuffer = Buffer()
import backend
root_container = HSplit(children=[
HSplit(children=[
Window(content=BufferControl(buffer=startBuffer, focusable=True)),
Window(width=1, char='-', style="fg:#2A71D5"),
def run_route_planning(hafas: backend.HafasClient | None = None):
"""Run the route planning TUI.
Window(content=BufferControl(buffer=endBuffer, focusable=True))
]),
Window(height=1, char=' ', style="bg:#2A71D5 fg:black"),
#Window(content=BufferControl(buffer=userBuffer),height=4),
])
Parameters:
- hafas: optional shared HafasClient instance. If omitted, a new one is
created. Prefer passing the shared client from `main.py`.
"""
if hafas is None:
hafas = backend.HafasClient()
layout = Layout(root_container)
# TODO: integrate actual station search + routing using `hafas`.
kb = KeyBindings()
startBuffer = Buffer()
endBuffer = Buffer()
app = Application(layout=layout, key_bindings=kb, full_screen=True)
root_container = HSplit(children=[
HSplit(children=[
Window(content=BufferControl(buffer=startBuffer, focusable=True)),
Window(width=1, char='-', style="fg:#2A71D5"),
Window(content=BufferControl(buffer=endBuffer, focusable=True))
]),
Window(height=1, char=' ', style="bg:#2A71D5 fg:black"),
])
app.run()
layout = Layout(root_container)
app = Application(layout=layout, key_bindings=kb, full_screen=True)
try:
app.run()
except KeyboardInterrupt:
# graceful exit on Ctrl-C
return

View file

@ -7,6 +7,7 @@ Prompt_toolkit (https://github.com/prompt-toolkit/python-prompt-toolkit) will be
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
@ -17,59 +18,72 @@ import threading
import time
keyB=KeyBindings()
inputBuffer=Buffer()
resultBuffer=Buffer()
stop_event=threading.Event()
app_state="MONITOR"
def run_station_monitor(hafas: backend.HafasClient | None = None):
"""Run the station monitor TUI.
inputBuffer.text = "Haltestelle eingeben: "
inputBuffer.cursor_position = len(inputBuffer.text)
Parameters:
- hafas: optional shared HafasClient instance. If omitted, a new one is
created. Prefer passing the shared client from `main.py`.
resultBuffer.text = "Ausgabe: "
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"
root_container= HSplit(children=[
VSplit(children=[
Window(content=BufferControl(buffer=inputBuffer, focusable=True)),
inputBuffer.text = "Haltestelle eingeben: "
inputBuffer.cursor_position = len(inputBuffer.text)
resultBuffer.text = "Ausgabe: "
Window(width=1, char='', style="fg:#9D1D75"),
Window(content=BufferControl(buffer=resultBuffer, focusable=True)),
]),
Window(height=1, char='-', style="bg:#9D1D75 fg:#FFFFFF"),
])
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)
layout = Layout(root_container, focused_element=inputBuffer)
@keyB.add("enter")
def handle_enter(event):
global app_state
@keyB.add("enter")
def handle_enter(event):
global app_state
user_input = inputBuffer.text.replace("Haltestelle eingeben: ", "").strip()
user_input = inputBuffer.text.replace("Haltestelle eingeben: ", "").strip()
if user_input:
app_state = "RESULTS"
resultBuffer.text = f"Ergebnisse für: {user_input}\n\n"
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."
result = hafas.getArrDep(station, arrdep="ARR", count=3)
inputBuffer.insert_line_below()
for entry in result:
resultBuffer.insert_text(f"\n {result[0]}")
inputBuffer.text = "Haltestelle eingeben: "
inputBuffer.cursor_position = len(inputBuffer.text)
else:
app_state = "MONITOR"
resultBuffer.text = "MONITOR\n\n"
else:
app_state = "MONITOR"
inputBuffer.text = "Haltestelle eingeben: "
inputBuffer.cursor_position = len(inputBuffer.text)
@keyB.add("c-q")
def exit_(event):
stop_event.set()
event.app.exit()
@keyB.add("c-q")
def exit_(event):
stop_event.set()
event.app.exit()
app = Application(layout=layout, full_screen=True, key_bindings=keyB)
app = Application(layout=layout, full_screen=True, key_bindings=keyB)
try:
app.run()
except KeyboardInterrupt:
pass
finally:
stop_event.set()
try:
app.run()
except KeyboardInterrupt:
pass
finally:
stop_event.set()