Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaya-Sem committed Aug 19, 2024
1 parent 2f46f78 commit c9609db
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 74 deletions.
6 changes: 3 additions & 3 deletions cmd/api/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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&timesel=departure&format=json&lang=en&typeOfTransport=automatic&alerts=false&results=6", stationFrom, stationTo)

resp, err := http.Get(url)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/irail-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
50 changes: 0 additions & 50 deletions cmd/api/irail-api_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/api/timetableDepartureStruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/tables/tableUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
const (
Gray = "240"
White = "15"
Green = "2"
Orange = "214"
Mauve = "97"
Red = "9"
BorderColor = Gray
SelectedForeground = White
SelectedBackground = Mauve
Expand All @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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},
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c9609db

Please sign in to comment.