Skip to content

Commit

Permalink
fix: code duplication of tables (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaya-Sem authored Aug 18, 2024
1 parent 5eb1cd8 commit 66ac959
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 205 deletions.
109 changes: 109 additions & 0 deletions cmd/tables/baseTable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package table

import (
"fmt"
"os"

"github.com/Kaya-Sem/commandtrein/cmd/api"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type TableData interface {
api.Connection | api.TimetableDeparture
}

type TableModel[T TableData] struct {
table table.Model
selectedDetails string
showMessage bool
message string
data []T
}

func (m *TableModel[T]) Init() tea.Cmd { return nil }

func (m *TableModel[T]) 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()
m.showMessage = true
m.message = m.getDetailedInfo(m.data[selectedIndex])
}
return m, tea.Quit
}
}
m.table, teaCmd = m.table.Update(msg)
m.updateSelectedDetails()
return m, teaCmd
}

func (m *TableModel[T]) View() string {
if m.showMessage {
return m.message
}
tableView := m.table.View()
detailsView := detailsBoxStyle.Render(m.selectedDetails)
return lipgloss.JoinHorizontal(lipgloss.Top, tableView, detailsView)
}

func (m *TableModel[T]) updateSelectedDetails() {
selectedRow := m.table.SelectedRow()
if selectedRow != nil {
selectedIndex := m.table.Cursor()
m.selectedDetails = m.getDetailedInfo(m.data[selectedIndex])
} else {
m.selectedDetails = "Nothing found!"
}
}

func (m *TableModel[T]) getDetailedInfo(item T) string {
switch v := any(item).(type) {
case api.Connection:
return getDetailedConnectionInfo(v)
case api.TimetableDeparture:
return getDetailedDepartureInfo(v)
default:
return "Unsupported data type"
}
}

func RenderTable[T TableData](
columnItems []table.Column,
rowItems []table.Row,
data []T,
) {
fmt.Println()
t := table.New(
table.WithColumns(columnItems),
table.WithRows(rowItems),
table.WithFocused(true),
table.WithHeight(tableHeight),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color(BorderColor)).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color(SelectedForeground)).
Background(lipgloss.Color(SelectedBackground))
t.SetStyles(s)
m := &TableModel[T]{
table: t,
data: data,
}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
101 changes: 0 additions & 101 deletions cmd/tables/connectionTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,8 @@ import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"os"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type connectionTableModel struct {
table table.Model
selectedDetails string
showMessage bool
message string
departures []api.Connection
}

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

func getDetailedConnectionInfo(c api.Connection) string {
return fmt.Sprintf(`
Departure in %s
Expand All @@ -36,89 +21,3 @@ Vehicle: %s
c.Departure.Vehicle,
)
}

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

m.selectedDetails = getDetailedConnectionInfo(selectedConnection)
} else {
m.selectedDetails = "Nothing found!"
}
}

func (m connectionTableModel) 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 = getDetailedConnectionInfo(selectedDeparture)
}
return m, tea.Quit
}
}

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

return m, teaCmd
}

func (m connectionTableModel) View() string {
if m.showMessage {
return m.message
}
tableView := m.table.View()
detailsView := detailsBoxStyle.Render(m.selectedDetails)

return lipgloss.JoinHorizontal(lipgloss.Top, tableView, detailsView)
}

func RenderConnectionTable(
columnItems []table.Column,
rowItems []table.Row,
connections []api.Connection,
) {
fmt.Println()

columns := columnItems
rows := rowItems

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(tableHeight),
)

s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color(BorderColor)).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color(SelectedForeground)).
Background(lipgloss.Color(SelectedBackground))
t.SetStyles(s)

m := connectionTableModel{
table: t,
departures: connections,
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
102 changes: 0 additions & 102 deletions cmd/tables/timetableTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,9 @@ import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"os"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

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

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

func getDetailedDepartureInfo(d api.TimetableDeparture) string {
relativeTime := CalculateHumanRelativeTime(d)
return fmt.Sprintf(`
Expand All @@ -38,93 +24,5 @@ Occupancy: %s
)
}

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

// Update the selected details including the relative time
m.selectedDetails = getDetailedDepartureInfo(selectedDeparture)
} else {
m.selectedDetails = "Nothing found!"
}
}

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)

m.updateSelectedDetails()

return m, teaCmd
}

// TODO: export to consts
var detailsBoxStyle = lipgloss.NewStyle().Padding(1)

func (m timetableTableModel) View() string {
if m.showMessage {
return m.message
}
tableView := m.table.View()
detailsView := detailsBoxStyle.Render(m.selectedDetails)

return lipgloss.JoinHorizontal(lipgloss.Top, tableView, detailsView)
}

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

columns := columnItems
rows := rowItems

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(tableHeight),
)

s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color(BorderColor)).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color(SelectedForeground)).
Background(lipgloss.Color(SelectedBackground))
t.SetStyles(s)

m := timetableTableModel{
table: t,
departures: departures,
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func handleConnection(stationFrom string, stationTo string) {
}

s.Stop()
table.RenderConnectionTable(columns, rows, connections)
table.RenderTable(columns, rows, connections)

}

Expand Down Expand Up @@ -124,5 +124,5 @@ func handleTimetable(stationName string) {

s.Stop()

table.RenderTimetableTable(columns, rows, departures)
table.RenderTable(columns, rows, departures)
}

0 comments on commit 66ac959

Please sign in to comment.