-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
info dump on enter (connections) (#13)
* 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
- Loading branch information
Showing
12 changed files
with
302 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
) | ||
|
||
type Spinner struct { | ||
spinner *spinner.Spinner | ||
prefix string | ||
suffix string | ||
sleep time.Duration | ||
} | ||
|
||
func NewSpinner(prefix, suffix string, sleep time.Duration) *Spinner { | ||
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) | ||
s.Prefix = prefix | ||
s.Suffix = suffix | ||
return &Spinner{ | ||
spinner: s, | ||
prefix: prefix, | ||
suffix: suffix, | ||
sleep: sleep, | ||
} | ||
} | ||
|
||
func (s *Spinner) Start() { | ||
s.spinner.Start() | ||
time.Sleep(s.sleep) | ||
} | ||
|
||
func (s *Spinner) Stop() { | ||
s.spinner.Stop() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package table | ||
|
||
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 | ||
relativeTime 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(` | ||
Detailed info: | ||
Destination: %s | ||
Track: %s | ||
Departure Time: %s | ||
Vehicle: %s | ||
`, | ||
c.Departure.Station, | ||
c.Departure.Platform, | ||
cmd.UnixToHHMM(c.Departure.Time), | ||
c.Departure.Vehicle, | ||
) | ||
} | ||
|
||
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) | ||
|
||
// Calculate the relative time for the currently selected row | ||
selectedRow := m.table.SelectedRow() | ||
if selectedRow != nil { | ||
departureTime := selectedRow[0] | ||
relativeTime := CalculateHumanRelativeTime(departureTime) | ||
m.relativeTime = relativeTime | ||
} else { | ||
m.relativeTime = "" | ||
} | ||
|
||
return m, teaCmd | ||
} | ||
|
||
func (m connectionTableModel) View() string { | ||
if m.showMessage { | ||
// Show the message instead of the tables if the flag is set | ||
return m.message | ||
} | ||
|
||
// 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: " + m.relativeTime + "\n" | ||
} | ||
return m.table.View() + "\n" | ||
} | ||
|
||
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, // Store the departures | ||
} | ||
|
||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package table | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
const ( | ||
BorderColor = "240" // gray | ||
SelectedForeground = "229" // not setting it to yellow will make the text yellow (yellow on purple = white?) | ||
SelectedBackground = "57" // purple | ||
tableHeight = 6 | ||
) | ||
|
||
// CalculateHumanRelativeTime used for calucating human-readable "from now" time. E.g 'in 20 minutes' | ||
func CalculateHumanRelativeTime(departureTime string) string { | ||
now := time.Now() | ||
|
||
depTime, err := time.Parse("15:04", departureTime) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
// Combine the parsed time with today's date | ||
depDateTime := time.Date(now.Year(), now.Month(), now.Day(), depTime.Hour(), depTime.Minute(), 0, 0, now.Location()) | ||
|
||
// If the departure time is earlier than now, assume it's for the next day | ||
if depDateTime.Before(now) { | ||
depDateTime = depDateTime.Add(24 * time.Hour) | ||
} | ||
|
||
// Calculate the duration between now and the departure time | ||
duration := depDateTime.Sub(now) | ||
|
||
// Handle special cases | ||
if duration < 1*time.Minute { | ||
return "now" | ||
} else if duration < 60*time.Minute { | ||
return fmt.Sprintf("%d min", int(duration.Minutes())) | ||
} else if duration < 120*time.Minute { | ||
minutes := int(duration.Minutes()) % 60 | ||
if minutes == 0 { | ||
return "1 hour" | ||
} | ||
return fmt.Sprintf("1 hour %d min", minutes) | ||
} | ||
|
||
hours := int(duration.Hours()) | ||
minutes := int(duration.Minutes()) % 60 | ||
if minutes == 0 { | ||
return fmt.Sprintf("%d hours", hours) | ||
} | ||
|
||
return fmt.Sprintf("%d hours %d min", hours, minutes) | ||
} |
Oops, something went wrong.