From c9609dbb3c954c8b5abc3e4ba35691bcec72d79d Mon Sep 17 00:00:00 2001 From: Kaya-Sem Date: Mon, 19 Aug 2024 18:48:09 +0200 Subject: [PATCH] refactor --- cmd/api/connection.go | 6 ++-- cmd/api/irail-api.go | 2 +- cmd/api/irail-api_test.go | 50 ----------------------------- cmd/api/timetableDepartureStruct.go | 4 +-- cmd/tables/tableUtil.go | 13 +++++--- cmd/util.go | 8 ----- go.mod | 2 +- main.go | 8 ++--- 8 files changed, 19 insertions(+), 74 deletions(-) delete mode 100644 cmd/api/irail-api_test.go diff --git a/cmd/api/connection.go b/cmd/api/connection.go index 18d66f1..1411515 100644 --- a/cmd/api/connection.go +++ b/cmd/api/connection.go @@ -10,7 +10,7 @@ import ( ) // GetConnections fetches the connection data from the API and returns the response body as a byte slice. -func GetConnections(stationFrom string, stationTo string, time string, arrdep string) ([]byte, error) { +func GetConnections(stationFrom string, stationTo string) ([]byte, error) { url := fmt.Sprintf("https://api.irail.be/connections/?from=%s&to=%s×el=departure&format=json&lang=en&typeOfTransport=automatic&alerts=false&results=6", stationFrom, stationTo) resp, err := http.Get(url) @@ -46,7 +46,7 @@ func ParseConnections(body []byte) ([]Connection, error) { func GetDurationInMinutes(c Connection) string { duration, err := strconv.Atoi(c.Duration) if err != nil { - fmt.Printf("Duration could not be parsed: %s", duration) + fmt.Printf("Duration could not be parsed: %d", duration) } duration /= 60 @@ -67,7 +67,7 @@ func (c Connection) GetDelayInSeconds() int { func (c Connection) GetUnixDepartureTime() int { depTime, err := strconv.Atoi(c.Departure.Time) if err != nil { - fmt.Println("Error converting departuretime: %s", c.Departure.Time) + fmt.Printf("Error converting departuretime: %s\n", c.Departure.Time) } return depTime } diff --git a/cmd/api/irail-api.go b/cmd/api/irail-api.go index 21f3cd6..c1d7555 100644 --- a/cmd/api/irail-api.go +++ b/cmd/api/irail-api.go @@ -28,7 +28,7 @@ func makeAPIRequest(url string) ([]byte, error) { } // GetSNCBStationTimeTable fetches the timetable for a specific station -func GetSNCBStationTimeTable(stationName string, time string, arrdep string) ([]byte, error) { +func GetSNCBStationTimeTable(stationName string) ([]byte, error) { url := fmt.Sprintf("https://api.irail.be/liveboard/?station=%s&lang=nl&format=json", stationName) return makeAPIRequest(url) } diff --git a/cmd/api/irail-api_test.go b/cmd/api/irail-api_test.go deleted file mode 100644 index 1a51677..0000000 --- a/cmd/api/irail-api_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package api - -import ( - "reflect" - "testing" -) - -// Test parseiRailTransitPoints with a valid JSON input -func TestParseiRailTransitPoints(t *testing.T) { - jsonData := []byte(`{ - "version": "1.3", - "timestamp": "1723234965", - "station": [ - { - "id": "BE.NMBS.008400319", - "name": "'s Hertogenbosch", - "standardname": "'s Hertogenbosch" - }, - { - "id": "BE.NMBS.008015345", - "name": "Aachen Hbf", - "standardname": "Aachen Hbf" - } - ] - }`) - - expectedTransitPoints := []TransitPoint{ - { - Name: "'s Hertogenbosch", - Id: "BE.NMBS.008400319", - TransitProvider: string(SNCB), - Description: "", - }, - { - Name: "Aachen Hbf", - Id: "BE.NMBS.008015345", - TransitProvider: string(SNCB), - Description: "", - }, - } - - transitPoints, err := parseiRailTransitPoints(jsonData) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - - if !reflect.DeepEqual(transitPoints, expectedTransitPoints) { - t.Errorf("Expected %v, but got %v", expectedTransitPoints, transitPoints) - } -} diff --git a/cmd/api/timetableDepartureStruct.go b/cmd/api/timetableDepartureStruct.go index 5b9eb80..1ed1593 100644 --- a/cmd/api/timetableDepartureStruct.go +++ b/cmd/api/timetableDepartureStruct.go @@ -8,7 +8,7 @@ import ( func (d TimetableDeparture) GetUnixDepartureTime() int { time, err := strconv.Atoi(d.Time) if err != nil { - fmt.Println("Error converting departure time: %s", d.Time) + fmt.Printf("Error converting departure time: %s\n", d.Time) } return time @@ -17,7 +17,7 @@ func (d TimetableDeparture) GetUnixDepartureTime() int { func (d TimetableDeparture) GetDelayInSeconds() int { delay, err := strconv.Atoi(d.Delay) if err != nil { - fmt.Println("Error converting delay: %s", d.Delay) + fmt.Printf("Error converting delay: %s\n", d.Delay) } return delay } diff --git a/cmd/tables/tableUtil.go b/cmd/tables/tableUtil.go index daaae6f..791684f 100644 --- a/cmd/tables/tableUtil.go +++ b/cmd/tables/tableUtil.go @@ -10,7 +10,10 @@ import ( const ( Gray = "240" White = "15" + Green = "2" + Orange = "214" Mauve = "97" + Red = "9" BorderColor = Gray SelectedForeground = White SelectedBackground = Mauve @@ -20,12 +23,12 @@ const ( var DetailsBoxStyle = lipgloss.NewStyle().Padding(1) var ( - baseOccupancyStyle = lipgloss.NewStyle().Italic(true) + OccupancyStyle = lipgloss.NewStyle().Italic(true) - lowOccupancyStyle = baseOccupancyStyle.Copy().Foreground(lipgloss.Color("2")) // green - mediumOccupancyStyle = baseOccupancyStyle.Copy().Foreground(lipgloss.Color("214")) // orange - highOccupancyStyle = baseOccupancyStyle.Copy().Foreground(lipgloss.Color("9")) // red - unknownOccupancyStyle = baseOccupancyStyle.Copy().Faint(true) + lowOccupancyStyle = OccupancyStyle.Copy().Foreground(lipgloss.Color(Green)) + mediumOccupancyStyle = OccupancyStyle.Copy().Foreground(lipgloss.Color(Orange)) + highOccupancyStyle = OccupancyStyle.Copy().Foreground(lipgloss.Color(Red)) + unknownOccupancyStyle = OccupancyStyle.Copy().Faint(true) ) func styleOccupancy(s string) string { diff --git a/cmd/util.go b/cmd/util.go index 2421531..6e8429e 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -3,17 +3,9 @@ package cmd import ( "fmt" "strconv" - "strings" "time" ) -// TODO: needed for flag parsing -func normalizeTime(time string) string { - time = strings.ReplaceAll(time, " ", "") - time = strings.ReplaceAll(time, ":", "") - return time -} - func UnixToHHMM(unixTime string) string { unixTimeInt, err := strconv.ParseInt(unixTime, 10, 64) if err != nil { diff --git a/go.mod b/go.mod index 45f255f..65f0555 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,6 @@ require ( github.com/rivo/uniseg v0.4.6 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/sys v0.12.0 // indirect - golang.org/x/term v0.6.0 // indirect + golang.org/x/term v0.6.0 golang.org/x/text v0.3.8 // indirect ) diff --git a/main.go b/main.go index ebf20c6..5e618f1 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ func handleConnection(stationFrom string, stationTo string) { s := cmd.NewSpinner(" ", " fetching connections...", 1*time.Second) s.Start() - connectionsJSON, err := api.GetConnections(stationFrom, stationTo, "", "") + connectionsJSON, err := api.GetConnections(stationFrom, stationTo) if err != nil { panic(err) } @@ -42,10 +42,10 @@ func handleConnection(stationFrom string, stationTo string) { } columns := []teaTable.Column{ - {Title: "Departure", Width: 7}, + {Title: "Dep", Width: 7}, {Title: "", Width: 4}, {Title: " ⏲", Width: 7}, - {Title: "Arrival", Width: 7}, + {Title: "Arr", Width: 7}, {Title: "Track", Width: 10}, } @@ -88,7 +88,7 @@ func handleTimetable(stationName string) { s := cmd.NewSpinner(" ", " fetching timetable...", 1*time.Second) s.Start() - timetableJSON, err := api.GetSNCBStationTimeTable(stationName, "", "departure") + timetableJSON, err := api.GetSNCBStationTimeTable(stationName) if err != nil { panic(err) }