hafas-terminal-app/route_planning.py

126 lines
3.8 KiB
Python

"""
haTerm - v0.1
(c) Sophia Schmidhofer
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.
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 datetime
import time
import json
from prompt_toolkit import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.layout.containers import HSplit, VSplit, Window
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.shortcuts import message_dialog
from prompt_toolkit.shortcuts import yes_no_dialog
from prompt_toolkit.shortcuts import input_dialog
from prompt_toolkit import prompt
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()
start = prompt("Start: ")
end = prompt("Ende: ")
station1 = hafas.getStationNames(start)
station2 = hafas.getStationNames(end)
startBuffer = Buffer()
startBuffer.text = start
#departure = #current time, muss noch abgesprochen werden.
etdBuffer = Buffer()
etdBuffer.text = "etd" #muss noch alles definiert werden
endBuffer = Buffer()
endBuffer.text = end
etaBuffer = Buffer()
etaBuffer.text = "eta" #--//--
drivetimeBuffer = Buffer()
#drivetimeBuffer.text = Fahrzeit von Hafas.
infoBuffer = Buffer()
infoBuffer.text = f"Routenplanung von {station1.name} nach {station2.name}"
def get_clock_text():
now = datetime.datetime.now()
return HTML(
f'<b> 🕐 {now.strftime("%H:%M:%S")} </b>'
f'<style fg="#aaddff"> {now.strftime("%A, %d. %B %Y")} </style>'
)
root_container = HSplit(children=[
HSplit(children=[
Window(height=2, content=BufferControl(buffer=infoBuffer, focusable=False), style="fg:#2A71D5"), #informationen über Route
Window(width=1, height = 1, char='-', style="fg:#2A71D5"), #Trennlinie
Window(content=BufferControl(buffer=startBuffer, focusable=True)), #start Station
Window(content=BufferControl(buffer=etdBuffer, focusable=False)), #abfahrtszeit
Window(width = 1, height = 2, char= ".", style= "fg:#A86FD6"), #Trennlinie
Window(content=BufferControl(buffer=endBuffer, focusable=True)), #end Station
Window(content=BufferControl(buffer=etaBuffer, focusable=False)), #ankunftszeit
]),
Window(height=1, char=' ', style="bg:#2A71D5 fg:black"),
Window(
content=FormattedTextControl(get_clock_text),
height=1,
style="bg:#163D7A fg:#aaddff",),
])
@kb.add('c-e')
def _(event):
result = yes_no_dialog(
title='Programm beenden',
text='Fenster schließen?').run()
if result == True:
event.app.exit()
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