#!/bin/sh print_usage() { cat < 0 then "\($h)h " else "" end) + "\($m)min"; def print_trip: # Prints a trip "station(time) → station(time)" (.duration // delta(.arrivalTime; .departureTime)) as $delta | "\(.origin)(\(green(.departureTime | time)))" + " → \(.destination)(\(.arrivalTime | time)) " + blue($delta); def print_train: # Prints the train name and its trip "\(.train.description // .train.trainCategory): " + print_trip; def print_summary: (.trains | length) as $ntrain | (if $ntrain > 2 then (", " + red("\($ntrain-1) cambi")) elif $ntrain == 2 then (", " + red("1 cambio")) else "" end) as $switch | "- " + print_trip + $switch + "\n " + (.nodes | map(print_train) | join("\n ")); .solutions | map(.solution | print_summary) | join("\n\n")' } handle_search() { # Get station IDs start_id=$(station_search "$1") stop_id=$(station_search "$2") shift 2 while :; do case "$1" in -d|--date) date=$2; shift ;; -i|--interactive) interactive=1; shift ;; -?*) print_usage ;; *) break esac done results=$(route_search "$start_id" "$stop_id" "${date:-now}") printf '%s\n\n' "$results" if test -n "$interactive"; then # Ask user to select a train info=$(printf '%s' "$results" | fzf --ansi --no-sort --disabled | cut -d: -f1) # Print the train status train_id=$(printf '%s' "$info" | cut -d: -f1 | rev | cut -d' ' -f 1 | rev) # convert between different API conventions station_id=$(printf '%s' "$start_id" | sed 's/^8300/S/g') train_status "$train_id" --board "id:$station_id" fi } test $# -eq 0 && print_usage cmd="$1"; shift case "$cmd" in search) handle_search "$@" ;; status) train_status "$@" ;; *) print_usage esac