Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaya-Sem committed Aug 19, 2024
1 parent 07e5f2e commit 19e4f67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
20 changes: 10 additions & 10 deletions cmd/tables/baseTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"github.com/charmbracelet/lipgloss"
)

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

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

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

func (m *TableModel[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *Model[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var teaCmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
Expand All @@ -46,16 +46,16 @@ func (m *TableModel[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, teaCmd
}

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

func (m *TableModel[T]) updateSelectedDetails() {
func (m *Model[T]) updateSelectedDetails() {
selectedRow := m.table.SelectedRow()
if selectedRow != nil {
selectedIndex := m.table.Cursor()
Expand All @@ -65,7 +65,7 @@ func (m *TableModel[T]) updateSelectedDetails() {
}
}

func (m *TableModel[T]) getDetailedInfo(item T) string {
func (m *Model[T]) getDetailedInfo(item T) string {
switch v := any(item).(type) {
case api.Connection:
return getDetailedConnectionInfo(v)
Expand All @@ -76,7 +76,7 @@ func (m *TableModel[T]) getDetailedInfo(item T) string {
}
}

func RenderTable[T TableData](
func RenderTable[T Data](
columnItems []table.Column,
rowItems []table.Row,
data []T,
Expand All @@ -98,7 +98,7 @@ func RenderTable[T TableData](
Foreground(lipgloss.Color(SelectedForeground)).
Background(lipgloss.Color(SelectedBackground))
t.SetStyles(s)
m := &TableModel[T]{
m := &Model[T]{
table: t,
data: data,
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/tables/connectionTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

func getDetailedConnectionInfo(c api.Connection) string {
return fmt.Sprintf(`
Departure in %s
Destination: %s
Track: %s
Departure Time: %s
Vehicle: %s
Departure in %s
Destination: %s
Track: %s
Departure Time: %s
Vehicle: %s
`,
CalculateHumanRelativeTime(c),
c.Departure.Station,
Expand Down
4 changes: 2 additions & 2 deletions cmd/tables/tableUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/charmbracelet/lipgloss"
)

const ()

const (
Gray = "240"
White = "15"
Expand All @@ -19,6 +17,8 @@ const (
tableHeight = 10
)

var DetailsBoxStyle = lipgloss.NewStyle().Padding(1)

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

Expand Down
14 changes: 5 additions & 9 deletions cmd/tables/timetableTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"fmt"
"github.com/Kaya-Sem/commandtrein/cmd"
"github.com/Kaya-Sem/commandtrein/cmd/api"
"github.com/charmbracelet/lipgloss"
)

func getDetailedDepartureInfo(d api.TimetableDeparture) string {
relativeTime := CalculateHumanRelativeTime(d)
return fmt.Sprintf(`
Departure in: %s
Track: %s
Departure Time: %s
Vehicle: %s
Occupancy: %s
Departure in: %s
Track: %s
Departure Time: %s
Vehicle: %s
Occupancy: %s
`,
relativeTime,
d.Platform,
Expand All @@ -23,6 +22,3 @@ Occupancy: %s
styleOccupancy(d.Occupancy.Name),
)
}

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

0 comments on commit 19e4f67

Please sign in to comment.