Skip to content

Commit

Permalink
detail block viewing (#16)
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

* rename

* added delay to timetable

* cleanup dependencies

* refactor name

* mega refactor

* abstracted spinner

* print detailed info on enter connections

* added basic right view

* added styling for occupancy

* added unknown styling

* added more todo's

* switchified styling of occupancy
  • Loading branch information
Kaya-Sem authored Aug 17, 2024
1 parent 1aa6bda commit 20f8fbf
Showing 1 changed file with 50 additions and 40 deletions.
90 changes: 50 additions & 40 deletions cmd/tables/timetableTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,91 @@ import (
"github.com/charmbracelet/lipgloss"
)

var (
lowOccupancyStyle = lipgloss.NewStyle().Italic(true).Foreground(lipgloss.Color("2")) // green
mediumOccupancyStyle = lipgloss.NewStyle().Italic(true).Foreground(lipgloss.Color("214")) // orange
highOccupancyStyle = lipgloss.NewStyle().Italic(true).Foreground(lipgloss.Color("9")) // red
unknownOccupancyStyle = lipgloss.NewStyle().Italic(true).Faint(true).Italic(true)
)

func styleOccupancy(s string) string {
switch s {
case "low":
return lowOccupancyStyle.Render(s)
case "medium":
return mediumOccupancyStyle.Render(s)
case "high":
return highOccupancyStyle.Render(s)
default:
return unknownOccupancyStyle.Render(s)
}
}

type timetableTableModel struct {
table table.Model
relativeTime string
showMessage bool
message string
departures []api.TimetableDeparture
table table.Model
selectedDetails string
departures []api.TimetableDeparture
}

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

func getDetailedDepartureInfo(d api.TimetableDeparture) string {
func getDetailedDepartureInfo(d api.TimetableDeparture, relativeTime string) string {
return fmt.Sprintf(`
Detailed info:
Destination: %s
Departure in: %s
Track: %s
Departure Time: %s
Vehicle: %s
Occupancy: %s
`,
d.Station,
relativeTime,
d.Platform,
cmd.UnixToHHMM(d.Time),
d.Vehicle,
d.Occupancy,
styleOccupancy(d.Occupancy.Name),
)
}

func (m *timetableTableModel) updateSelectedDetails() {
selectedRow := m.table.SelectedRow()
if selectedRow != nil {
selectedIndex := m.table.Cursor()
selectedDeparture := m.departures[selectedIndex]

departureTime := selectedRow[0] // TODO: dont hardcode, this is magic num
relativeTime := CalculateHumanRelativeTime(departureTime)

// Update the selected details including the relative time
m.selectedDetails = getDetailedDepartureInfo(selectedDeparture, relativeTime)
} else {
m.selectedDetails = "No row selected" // Should never really happen
}
}

func (m timetableTableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var teaCmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
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
}
}

m.table, teaCmd = m.table.Update(msg)

// Calculate the relative time for the currently selected row
selectedRow := m.table.SelectedRow()
if selectedRow != nil {
departureTime := selectedRow[0]
CalculateHumanRelativeTime(departureTime)
relativeTime := CalculateHumanRelativeTime(departureTime)
m.relativeTime = relativeTime
} else {
m.relativeTime = ""
}
m.updateSelectedDetails()

return m, teaCmd
}

var italicStyle = lipgloss.NewStyle().Italic(true)
// TODO: add rounded corners for Bram?
var detailsBoxStyle = lipgloss.NewStyle().Padding(1) //.Border(lipgloss.NormalBorder())

func (m timetableTableModel) View() string {
if m.showMessage {
// Show the message instead of the tables if the flag is set
return m.message
}
tableView := m.table.View()
detailsView := detailsBoxStyle.Render(m.selectedDetails)

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

func RenderTimetableTable(
Expand Down Expand Up @@ -118,7 +128,7 @@ func RenderTimetableTable(

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

if _, err := tea.NewProgram(m).Run(); err != nil {
Expand Down

0 comments on commit 20f8fbf

Please sign in to comment.