diff --git a/backend.py b/backend.py index 462b807..d5eb4e5 100644 --- a/backend.py +++ b/backend.py @@ -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() diff --git a/main.py b/main.py index b43be72..b33ec64 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/route_planning.py b/route_planning.py index 64e4a1d..56b2ee4 100644 --- a/route_planning.py +++ b/route_planning.py @@ -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 diff --git a/station_monitor.py b/station_monitor.py index 2286df1..4450f4c 100644 --- a/station_monitor.py +++ b/station_monitor.py @@ -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)), - - Window(width=1, char='│', style="fg:#9D1D75"), - Window(content=BufferControl(buffer=resultBuffer, focusable=True)), - ]), - Window(height=1, char='-', style="bg:#9D1D75 fg:#FFFFFF"), -]) + inputBuffer.text = "Haltestelle eingeben: " + inputBuffer.cursor_position = len(inputBuffer.text) + resultBuffer.text = "Ausgabe: " -layout = Layout(root_container, focused_element=inputBuffer) + 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"), + ]) -@keyB.add("enter") -def handle_enter(event): - global app_state + layout = Layout(root_container, focused_element=inputBuffer) - user_input = inputBuffer.text.replace("Haltestelle eingeben: ", "").strip() + @keyB.add("enter") + def handle_enter(event): + global app_state - if user_input: - app_state = "RESULTS" - resultBuffer.text = f"Ergebnisse für: {user_input}\n\n" + user_input = inputBuffer.text.replace("Haltestelle eingeben: ", "").strip() - inputBuffer.text = "Haltestelle eingeben: " - inputBuffer.cursor_position = len(inputBuffer.text) - else: - app_state = "MONITOR" - resultBuffer.text = "MONITOR\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" -@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() \ No newline at end of file + try: + app.run() + except KeyboardInterrupt: + pass + finally: + stop_event.set()