Skip to content

Commit

Permalink
cleanup (#12)
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

* refactor of project structure
  • Loading branch information
Kaya-Sem authored Aug 16, 2024
1 parent 56e94d3 commit df8b809
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/connection.go → cmd/api/connection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package api

import (
"encoding/json"
Expand Down
4 changes: 2 additions & 2 deletions cmd/irail-api.go → cmd/api/irail-api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package api

// https://docs.irail.be/

Expand Down Expand Up @@ -29,7 +29,7 @@ func GetSNCBStationTimeTable(stationName string, time string, arrdep string) ([]
}

// ParseiRailDepartures handles fetching of timetable departures
func ParseiRailDepartures(jsonData []byte) ([]timetableDeparture, error) {
func ParseiRailDepartures(jsonData []byte) ([]TimetableDeparture, error) {
var response StationTimetableResponse
err := json.Unmarshal(jsonData, &response)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/irail-api_test.go → cmd/api/irail-api_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package api

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package api

type StationTimetableResponse struct {
Version string `json:"version"`
Expand All @@ -18,10 +18,10 @@ type StationInfo struct {

type Departures struct {
Number string `json:"number"`
Departure []timetableDeparture `json:"departure"`
Departure []TimetableDeparture `json:"departure"`
}

type timetableDeparture struct {
type TimetableDeparture struct {
ID string `json:"id"`
Station string `json:"station"`
StationInfo StationInfo `json:"stationinfo"`
Expand Down
11 changes: 7 additions & 4 deletions cmd/departure-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"os"

"github.com/charmbracelet/bubbles/table"
Expand All @@ -24,12 +25,12 @@ type tableModel struct {
relativeTime string
showMessage bool
message string
departures []timetableDeparture
departures []api.TimetableDeparture
}

func (m tableModel) Init() tea.Cmd { return nil }

func getDetailedDepartureInfo(d timetableDeparture) string {
func getDetailedDepartureInfo(d api.TimetableDeparture) string {
return fmt.Sprintf(`
Detailed info:
Destination: %s
Expand Down Expand Up @@ -80,6 +81,8 @@ func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

var italicStyle = lipgloss.NewStyle().Italic(true)

func (m tableModel) View() string {
if m.showMessage {
// Show the message instead of the table if the flag is set
Expand All @@ -88,15 +91,15 @@ func (m tableModel) View() string {

// Add the relative time to the view only if there is a selected row
if m.relativeTime != "" {
return baseStyle1.Render(m.table.View()) + "\n\n" + "Departure in: " + m.relativeTime + "\n"
return baseStyle1.Render(m.table.View()) + "\n\n" + "Departure in: " + italicStyle.Render(m.relativeTime) + "\n"
}
return baseStyle1.Render(m.table.View()) + "\n"
}

func RenderTable(
columnItems []table.Column,
rowItems []table.Row,
departures []timetableDeparture,
departures []api.TimetableDeparture,
) {
fmt.Println()

Expand Down
14 changes: 6 additions & 8 deletions cmd/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"os"
"strconv"

Expand All @@ -10,9 +11,6 @@ import (
"github.com/charmbracelet/lipgloss"
)

var baseStyle = lipgloss.NewStyle().
BorderForeground(lipgloss.Color("9"))

type model struct {
table table.Model
relativeTime string
Expand All @@ -27,10 +25,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
// case "enter":
// return m, tea.Batch(
// tea.Printf("Let's go to %s!", m.table.SelectedRow()[0]),
// )
case "enter":
return m, tea.Batch(
tea.Printf("detailed info here"),
)
}
}

Expand All @@ -57,7 +55,7 @@ func (m model) View() string {
return baseStyle1.Render(m.table.View()) + "\n"
}

func PrintDepartureTable(connections []Connection) {
func PrintDepartureTable(connections []api.Connection) {
fmt.Println()
columns := []table.Column{
{Title: "Departure", Width: 10},
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"os"
"time"

Expand Down Expand Up @@ -32,12 +33,12 @@ func handleConnection(stationFrom string, stationTo string) {
s.Start()
time.Sleep(1 * time.Second)

connectionsJSON, err := cmd.GetConnections(stationFrom, stationTo, "", "")
connectionsJSON, err := api.GetConnections(stationFrom, stationTo, "", "")
if err != nil {
panic(err)
}

connections, err := cmd.ParseConnections(connectionsJSON)
connections, err := api.ParseConnections(connectionsJSON)
if err != nil {
panic(err)
}
Expand All @@ -47,8 +48,8 @@ func handleConnection(stationFrom string, stationTo string) {
}

func handleSearch() {
stationsJSON := cmd.GetSNCBStationsJSON()
stations, err := cmd.ParseStations(stationsJSON)
stationsJSON := api.GetSNCBStationsJSON()
stations, err := api.ParseStations(stationsJSON)
if err != nil {
panic(err)
}
Expand All @@ -65,12 +66,12 @@ func handleTimetable(stationName string) {
s.Start()
time.Sleep(1 * time.Second)

timetableJSON, err := cmd.GetSNCBStationTimeTable(stationName, "", "departure")
timetableJSON, err := api.GetSNCBStationTimeTable(stationName, "", "departure")
if err != nil {
panic(err)
}

departures, err := cmd.ParseiRailDepartures(timetableJSON)
departures, err := api.ParseiRailDepartures(timetableJSON)
if err != nil {
fmt.Printf("failed to parse iRail departures JSON: %v", err)
}
Expand Down

0 comments on commit df8b809

Please sign in to comment.