Skip to content

Commit

Permalink
Dev (#11)
Browse files Browse the repository at this point in the history
* update (#10)

* removed overlap in different models

* implemented showing message after closing table with selection

* added quit option "escape"

quits table with any additional info

* updated to more correct color name lol

* show detailed information for a departure

* fixed import path

* Update README.md

added installation options

* Dev (#8)

* fixed import path

* added autobuild

* Update README.md (#9)

added installation options

* created const

* cleanup and refactor

* corrected if-else block
  • Loading branch information
Kaya-Sem authored Aug 16, 2024
1 parent ecfab8c commit 56e94d3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 64 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 6 additions & 21 deletions cmd/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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&timesel=departure&format=json&lang=en&typeOfTransport=automatic&alerts=false&results=6", stationFrom, stationTo)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/irail-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 9 additions & 36 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":", "")
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/charmbracelet/bubbles/table"
)

const Version = "0.0.0"

func main() {
args := cmd.ShiftArgs(os.Args)

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

0 comments on commit 56e94d3

Please sign in to comment.