-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
formatting and updating of column formatting connections
- Loading branch information
Showing
1 changed file
with
111 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Kaya-Sem/commandtrein/cmd" | ||
"github.com/Kaya-Sem/commandtrein/cmd/api" | ||
table "github.com/Kaya-Sem/commandtrein/cmd/tables" | ||
"os" | ||
"time" | ||
|
||
teaTable "github.com/charmbracelet/bubbles/table" | ||
"fmt" | ||
"github.com/Kaya-Sem/commandtrein/cmd" | ||
"github.com/Kaya-Sem/commandtrein/cmd/api" | ||
table "github.com/Kaya-Sem/commandtrein/cmd/tables" | ||
"os" | ||
"time" | ||
|
||
teaTable "github.com/charmbracelet/bubbles/table" | ||
) | ||
|
||
func main() { | ||
// TODO: allow flags for time and arrdep | ||
args := cmd.ShiftArgs(os.Args) | ||
|
||
if len(args) == 1 { | ||
if args[0] == "search" { | ||
handleSearch() | ||
} else { | ||
handleTimetable(args[0]) | ||
} | ||
|
||
} else if len(args) == 3 { | ||
handleConnection(args[0], args[2]) | ||
} | ||
// TODO: allow flags for time and arrdep | ||
args := cmd.ShiftArgs(os.Args) | ||
|
||
if len(args) == 1 { | ||
if args[0] == "search" { | ||
handleSearch() | ||
} else { | ||
handleTimetable(args[0]) | ||
} | ||
|
||
} else if len(args) == 3 { | ||
handleConnection(args[0], args[2]) | ||
} | ||
} | ||
|
||
func handleConnection(stationFrom string, stationTo string) { | ||
s := cmd.NewSpinner("", " fetching connections...", 1*time.Second) | ||
s.Start() | ||
|
||
connectionsJSON, err := api.GetConnections(stationFrom, stationTo) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
connections, err := api.ParseConnections(connectionsJSON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
columns := []teaTable.Column{ | ||
{Title: "Dep", Width: 7}, | ||
{Title: "", Width: 4}, | ||
{Title: " ⏲", Width: 7}, | ||
{Title: "Arr", Width: 7}, | ||
{Title: "Track", Width: 10}, | ||
} | ||
|
||
rows := make([]teaTable.Row, len(connections)) | ||
|
||
for i, conn := range connections { | ||
var delay string | ||
if conn.Departure.Delay == "0" { | ||
delay = "" | ||
} else { | ||
delay = cmd.FormatDelay(conn.Departure.Delay) | ||
} | ||
rows[i] = teaTable.Row{ | ||
cmd.UnixToHHMM(conn.Departure.Time), | ||
delay, | ||
api.GetDurationInMinutes(conn), | ||
cmd.UnixToHHMM(conn.Arrival.Time), | ||
conn.Departure.Platform, | ||
} | ||
} | ||
|
||
s.Stop() | ||
table.RenderTable(columns, rows, connections) | ||
s := cmd.NewSpinner("", " fetching connections...", 1*time.Second) | ||
s.Start() | ||
|
||
connectionsJSON, err := api.GetConnections(stationFrom, stationTo) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
connections, err := api.ParseConnections(connectionsJSON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
columns := []teaTable.Column{ | ||
{Title: "D", Width: 9}, | ||
{Title: "🕑", Width: 7}, | ||
{Title: "A", Width: 7}, | ||
{Title: "T", Width: 10}, | ||
} | ||
|
||
rows := make([]teaTable.Row, len(connections)) | ||
|
||
for i, conn := range connections { | ||
// Append the formatted delay to the departure time | ||
departureTimeWithDelay := cmd.UnixToHHMM(conn.Departure.Time) | ||
delay := cmd.FormatDelay(conn.Departure.Delay) | ||
if delay != "" { | ||
departureTimeWithDelay += delay // Append delay to the time | ||
} | ||
|
||
// Populate the row | ||
rows[i] = teaTable.Row{ | ||
departureTimeWithDelay, | ||
api.GetDurationInMinutes(conn), | ||
cmd.UnixToHHMM(conn.Arrival.Time), | ||
conn.Departure.Platform, | ||
} | ||
} | ||
|
||
s.Stop() | ||
table.RenderTable(columns, rows, connections) | ||
|
||
} | ||
|
||
func handleSearch() { | ||
stationsJSON, err := api.GetSNCBStationsJSON() | ||
stations, err := api.ParseStations(stationsJSON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, station := range stations { | ||
fmt.Printf("%s %s\n", station.ID, station.Name) | ||
} | ||
stationsJSON, err := api.GetSNCBStationsJSON() | ||
stations, err := api.ParseStations(stationsJSON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, station := range stations { | ||
fmt.Printf("%s %s\n", station.ID, station.Name) | ||
} | ||
} | ||
|
||
func handleTimetable(stationName string) { | ||
s := cmd.NewSpinner("", " fetching timetable...", 1*time.Second) | ||
s.Start() | ||
|
||
timetableJSON, err := api.GetSNCBStationTimeTable(stationName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
departures, err := api.ParseiRailDepartures(timetableJSON) | ||
if err != nil { | ||
fmt.Printf("failed to parse iRail departures JSON: %v", err) | ||
} | ||
|
||
columns := []teaTable.Column{ | ||
{Title: "", Width: 5}, | ||
{Title: "", Width: 5}, | ||
{Title: "Destination", Width: 20}, | ||
{Title: "Track", Width: 10}, | ||
} | ||
|
||
rows := make([]teaTable.Row, len(departures)) | ||
|
||
for i, departure := range departures { | ||
var delay string | ||
if departure.Delay == "0" { | ||
delay = "" | ||
} else { | ||
delay = cmd.FormatDelay(departure.Delay) | ||
} | ||
rows[i] = teaTable.Row{ | ||
cmd.UnixToHHMM(departure.Time), | ||
delay, | ||
departure.Station, | ||
departure.Platform, | ||
} | ||
} | ||
|
||
s.Stop() | ||
|
||
table.RenderTable(columns, rows, departures) | ||
s := cmd.NewSpinner("", " fetching timetable...", 1*time.Second) | ||
s.Start() | ||
|
||
timetableJSON, err := api.GetSNCBStationTimeTable(stationName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
departures, err := api.ParseiRailDepartures(timetableJSON) | ||
if err != nil { | ||
fmt.Printf("failed to parse iRail departures JSON: %v", err) | ||
} | ||
|
||
columns := []teaTable.Column{ | ||
{Title: "", Width: 5}, | ||
{Title: "", Width: 5}, | ||
{Title: "Destination", Width: 20}, | ||
{Title: "Track", Width: 10}, | ||
} | ||
|
||
rows := make([]teaTable.Row, len(departures)) | ||
|
||
for i, departure := range departures { | ||
var delay string | ||
if departure.Delay == "0" { | ||
delay = "" | ||
} else { | ||
delay = cmd.FormatDelay(departure.Delay) | ||
} | ||
rows[i] = teaTable.Row{ | ||
cmd.UnixToHHMM(departure.Time), | ||
delay, | ||
departure.Station, | ||
departure.Platform, | ||
} | ||
} | ||
|
||
s.Stop() | ||
|
||
table.RenderTable(columns, rows, departures) | ||
} |