diff --git a/README.md b/README.md index 70f982a..e6eaa3f 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ This will download and install commandtrein and make it avaible in your `$GOPATH If you prefer to build commandtrein from source, follow these steps: ```bash + git clone https://github.com/Kaya-Sem/commandtrein.git cd commandtrein go build -o commandtrein diff --git a/cmd/connection.go b/cmd/connection.go index fd0449e..bbe5170 100644 --- a/cmd/connection.go +++ b/cmd/connection.go @@ -3,30 +3,10 @@ package cmd import ( "encoding/json" "fmt" - "github.com/cheynewallace/tabby" "io" "net/http" - "strconv" ) -func PrintConnection(conns []Connection) { - - t := tabby.New() - t.AddHeader("Departure", "Duration", "Arrival", "Platform") - - for _, conn := range conns { - departureTime := UnixToHHMM(conn.Departure.Time) - arrivalTime := UnixToHHMM(conn.Arrival.Time) - durationInt, _ := strconv.ParseInt(conn.Duration, 10, 32) - - duration := strconv.FormatInt(durationInt/60, 10) + "m" - track := conn.Departure.Platform - t.AddLine(departureTime, duration, arrivalTime, track) - } - - t.Print() -} - // 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) { 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) @@ -35,7 +15,12 @@ func GetConnections(stationFrom string, stationTo string, time string, arrdep st if err != nil { return nil, err } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Println(fmt.Errorf("couldn't close response body: %v", err)) + } + }(resp.Body) body, err := io.ReadAll(resp.Body) if err != nil { diff --git a/cmd/irail-api.go b/cmd/irail-api.go index c80f189..6b63299 100644 --- a/cmd/irail-api.go +++ b/cmd/irail-api.go @@ -40,7 +40,7 @@ func ParseiRailDepartures(jsonData []byte) ([]timetableDeparture, error) { } func GetSNCBStationsJSON() []byte { - url := "https://api.irail.be/stations/?format=json&lang=nl" + const url string = "https://api.irail.be/stations/?format=json&lang=nl" req, err := http.NewRequest("GET", url, nil) if err != nil { fmt.Println("Error creating request:", err) diff --git a/cmd/util.go b/cmd/util.go index 4201b53..3ee58b6 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -7,34 +7,6 @@ import ( "time" ) -const ( - StatusOK = 200 - StatusInternalServerError = 500 -) - -const ( - ErrCli = 1 - ErrFileRead = 2 - ErrUnmarshal = 5 - ErrFileExists = 3 -) - -// TODO: let users know when a bad response was given by the server -var StatusCodes = map[int]string{ - StatusOK: "\033[32m200 OK\033[0m", // green - StatusInternalServerError: "\033[31m500 Internal Server Error\033[0m", // red -} - -func replaceSpacesWithURLCode(input string) string { - return strings.ReplaceAll(input, " ", "%20") -} - -func getCurrentTimeHHMM() string { - hours, minutes, _ := time.Now().Clock() - return fmt.Sprintf("%d%02d", hours, minutes) - -} - func normalizeTime(time string) string { time = strings.ReplaceAll(time, " ", "") time = strings.ReplaceAll(time, ":", "") @@ -67,7 +39,7 @@ func ShiftArgs(args []string) []string { return args[1:] } -// used for calucating human readable "from now" time. E.g 'in 20 minutes' +// CalculateHumanRelativeTime used for calucating human-readable "from now" time. E.g 'in 20 minutes' func CalculateHumanRelativeTime(departureTime string) string { now := time.Now() @@ -98,12 +70,13 @@ func CalculateHumanRelativeTime(departureTime string) string { return "1 hour" } return fmt.Sprintf("1 hour %d min", minutes) - } else { - hours := int(duration.Hours()) - minutes := int(duration.Minutes()) % 60 - if minutes == 0 { - return fmt.Sprintf("%d hours", hours) - } - return fmt.Sprintf("%d hours %d min", hours, minutes) } + + hours := int(duration.Hours()) + minutes := int(duration.Minutes()) % 60 + if minutes == 0 { + return fmt.Sprintf("%d hours", hours) + } + + return fmt.Sprintf("%d hours %d min", hours, minutes) } diff --git a/main.go b/main.go index 78ca631..db8290e 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,6 @@ import ( "github.com/charmbracelet/bubbles/table" ) -const Version = "0.0.0" - func main() { args := cmd.ShiftArgs(os.Args) @@ -83,10 +81,14 @@ func handleTimetable(stationName string) { {Title: "Track", Width: 10}, } - var rows []table.Row - for _, departure := range departures { - row := table.Row{cmd.UnixToHHMM(departure.Time), departure.Station, departure.Platform} - rows = append(rows, row) + rows := make([]table.Row, len(departures)) + + for i, departure := range departures { + rows[i] = table.Row{ + cmd.UnixToHHMM(departure.Time), + departure.Station, + departure.Platform, + } } s.Stop()