Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Info on Enter #5

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions cmd/departure-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,58 @@ import (
"github.com/charmbracelet/lipgloss"
)

const (
BorderColor = "240" // gray
SelectedForeground = "229" // not setting it to yellow will make the text yellow
SelectedBackground = "57" // purple
tableHeight = 6
)

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

type tableModel struct {
table table.Model
relativeTime string
showMessage bool
message string
departures []timetableDeparture
}

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

func getDetailedDepartureInfo(d timetableDeparture) string {
return fmt.Sprintf(`
Detailed info:
Destination: %s
Track: %s
Departure Time: %s
Vehicle: %s
Occupancy: %s
`,
d.Station,
d.Platform,
UnixToHHMM(d.Time),
d.Vehicle,
d.Occupancy,
)
}

func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
case "q", "ctrl+c", "esc":
return m, tea.Quit
case "enter":
selectedRow := m.table.SelectedRow()
if selectedRow != nil {
selectedIndex := m.table.Cursor()
selectedDeparture := m.departures[selectedIndex]
m.showMessage = true
m.message = getDetailedDepartureInfo(selectedDeparture)
}
return m, tea.Quit
}
}
Expand All @@ -45,14 +81,23 @@ func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m tableModel) View() string {
if m.showMessage {
// Show the message instead of the table if the flag is set
return baseStyle1.Render(m.message)
}

// 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"
}

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

columns := columnItems
Expand All @@ -62,22 +107,25 @@ func RenderTable(columnItems []table.Column, rowItems []table.Row) {
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(6),
table.WithHeight(tableHeight),
)

s := table.DefaultStyles()
// s.Cell.Align(lipgloss.Position(4))
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderForeground(lipgloss.Color(BorderColor)).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57"))
Foreground(lipgloss.Color(SelectedForeground)).
Background(lipgloss.Color(SelectedBackground))
t.SetStyles(s)

m := tableModel{t, ""}
m := tableModel{
table: t,
departures: departures, // Store the departures
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func PrintDepartureTable(connections []Connection) {
Bold(false)
t.SetStyles(s)

m := tableModel{t, ""}
m := model{t, ""}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func handleConnection(stationFrom string, stationTo string) {
panic(err)
}

// TODO: simple flag for basic lines
/* cmd.PrintConnection(connections) */
s.Stop()
cmd.PrintDepartureTable(connections)
}
Expand Down Expand Up @@ -93,5 +91,5 @@ func handleTimetable(stationName string) {

s.Stop()

cmd.RenderTable(columns, rows)
cmd.RenderTable(columns, rows, departures)
}