added basic connection between the files, fixed small issues. some parts were achieved using ai.
This commit is contained in:
parent
dd5816b175
commit
45e5cadc62
4 changed files with 184 additions and 63 deletions
|
|
@ -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
87
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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
import backend
|
||||
|
||||
|
||||
def run_route_planning(hafas: backend.HafasClient | None = None):
|
||||
"""Run the route planning TUI.
|
||||
|
||||
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()
|
||||
|
||||
# TODO: integrate actual station search + routing using `hafas`.
|
||||
kb = KeyBindings()
|
||||
startBuffer = Buffer()
|
||||
endBuffer = Buffer()
|
||||
|
||||
|
||||
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"),
|
||||
#Window(content=BufferControl(buffer=userBuffer),height=4),
|
||||
])
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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,22 +18,31 @@ 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 = "Monitor\n\nHaltestelle eingeben: "
|
||||
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)),
|
||||
]),
|
||||
|
|
@ -49,16 +59,20 @@ def handle_enter(event):
|
|||
|
||||
if user_input:
|
||||
app_state = "RESULTS"
|
||||
resultBuffer.text = f"Ergebnisse für: {user_input}\n\n"
|
||||
# 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"
|
||||
|
||||
inputBuffer.text = "Haltestelle eingeben: "
|
||||
inputBuffer.cursor_position = len(inputBuffer.text)
|
||||
|
||||
@keyB.add("c-q")
|
||||
def exit_(event):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue