From 7338c774abdf75697706353cfe519f27e0f8fc28 Mon Sep 17 00:00:00 2001 From: ruinivist Date: Tue, 10 Feb 2026 21:54:47 +0000 Subject: [PATCH] feat: add ui package --- go.mod | 29 +++++++++ go.sum | 54 +++++++++++++++++ internal/ui/commands.go | 20 +++++++ internal/ui/model.go | 128 ++++++++++++++++++++++++++++++++++++++++ internal/ui/styles.go | 18 ++++++ internal/ui/table.go | 32 ++++++++++ 6 files changed, 281 insertions(+) create mode 100644 go.sum create mode 100644 internal/ui/commands.go create mode 100644 internal/ui/model.go create mode 100644 internal/ui/styles.go create mode 100644 internal/ui/table.go diff --git a/go.mod b/go.mod index 3c968d6..c09f351 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,32 @@ module tz-snipe go 1.25.5 + +require ( + github.com/charmbracelet/bubbles v1.0.0 + github.com/charmbracelet/bubbletea v1.3.10 + github.com/charmbracelet/lipgloss v1.1.0 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/colorprofile v0.4.1 // indirect + github.com/charmbracelet/x/ansi v0.11.6 // indirect + github.com/charmbracelet/x/cellbuf v0.0.15 // indirect + github.com/charmbracelet/x/term v0.2.2 // indirect + github.com/clipperhouse/displaywidth v0.9.0 // indirect + github.com/clipperhouse/stringish v0.1.1 // indirect + github.com/clipperhouse/uax29/v2 v2.5.0 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.3.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.19 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.16.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.38.0 // indirect + golang.org/x/text v0.3.8 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..abdde7d --- /dev/null +++ b/go.sum @@ -0,0 +1,54 @@ +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY= +github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E= +github.com/charmbracelet/bubbles v1.0.0 h1:12J8/ak/uCZEMQ6KU7pcfwceyjLlWsDLAxB5fXonfvc= +github.com/charmbracelet/bubbles v1.0.0/go.mod h1:9d/Zd5GdnauMI5ivUIVisuEm3ave1XwXtD1ckyV6r3E= +github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw= +github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4= +github.com/charmbracelet/colorprofile v0.4.1 h1:a1lO03qTrSIRaK8c3JRxJDZOvhvIeSco3ej+ngLk1kk= +github.com/charmbracelet/colorprofile v0.4.1/go.mod h1:U1d9Dljmdf9DLegaJ0nGZNJvoXAhayhmidOdcBwAvKk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/x/ansi v0.11.6 h1:GhV21SiDz/45W9AnV2R61xZMRri5NlLnl6CVF7ihZW8= +github.com/charmbracelet/x/ansi v0.11.6/go.mod h1:2JNYLgQUsyqaiLovhU2Rv/pb8r6ydXKS3NIttu3VGZQ= +github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI= +github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= +github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk= +github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI= +github.com/clipperhouse/displaywidth v0.9.0 h1:Qb4KOhYwRiN3viMv1v/3cTBlz3AcAZX3+y9OLhMtAtA= +github.com/clipperhouse/displaywidth v0.9.0/go.mod h1:aCAAqTlh4GIVkhQnJpbL0T/WfcrJXHcj8C0yjYcjOZA= +github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs= +github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA= +github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w94cO8U= +github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= +github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= +github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/internal/ui/commands.go b/internal/ui/commands.go new file mode 100644 index 0000000..4fd9e2f --- /dev/null +++ b/internal/ui/commands.go @@ -0,0 +1,20 @@ +package ui + +import ( + "tz-snipe/internal/github" + + tea "github.com/charmbracelet/bubbletea" +) + +type tzsMsg []string +type errMsg error + +func fetchGithubCmd(username string) tea.Cmd { + return func() tea.Msg { + tzs, err := github.GetTzsForUser(username) + if err != nil { + return errMsg(err) + } + return tzsMsg(tzs) + } +} diff --git a/internal/ui/model.go b/internal/ui/model.go new file mode 100644 index 0000000..55898ba --- /dev/null +++ b/internal/ui/model.go @@ -0,0 +1,128 @@ +package ui + +import ( + "fmt" + "strings" + "tz-snipe/internal/core" + "tz-snipe/internal/geodata" + + "github.com/charmbracelet/bubbles/table" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +type Model struct { + db geodata.TimezoneMap + username string + manualTz string + preds []core.Prediction + loadingGithubTz bool + err error // this is how error handling is to be done in bubbletea + // just pass the error to ui layer + displayTz string + table table.Model +} + +// go quirk: struct trick to get lsp to highlight errors +var _ tea.Model = Model{} + +// REMINDER REMINDER REMINDER +// Model, NewModel, Init, Commands, Update, View is all you need + +func NewModel(db geodata.TimezoneMap, ghUser, manualTz string) Model { + m := Model{ + db: db, + username: ghUser, + manualTz: manualTz, + loadingGithubTz: false, + } + switch { + case manualTz != "": + preds, err := core.GetStatsForTZs([]string{manualTz}) + if err != nil { + m.err = err + } else { + m.preds = preds + m.table = newTable(m.preds) + m.displayTz = m.manualTz + } + case ghUser != "": + m.loadingGithubTz = true + } + + return m +} + +func (m Model) Init() tea.Cmd { + if m.loadingGithubTz { + return fetchGithubCmd(m.username) + } + return nil +} + +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + // tbf this interrupt handling should be default + // go's type assertion trick aghain + // you want it handled first and globally so that + // fall throughs work nicely + if asKey, casted := msg.(tea.KeyMsg); casted { + if asKey.Type == tea.KeyCtrlC || asKey.String() == "q" { + return m, tea.Quit + } + } + + // .(type) is special go syntax + // defiendonly for interfaces and must be used in a switch + switch msg := msg.(type) { + case errMsg: + m.err = msg + return m, nil + + case tzsMsg: + preds, err := core.GetStatsForTZs(msg) + if err != nil { + m.err = err + } else { + m.preds = preds + m.table = newTable(m.preds) + m.displayTz = strings.Join(msg, ", ") + m.loadingGithubTz = false + } + return m, nil + } + + // anuthing else, to the table + + // go quirk: non name on left side of ... + // I can't use := as then it tries to declare it + var cmd tea.Cmd + m.table, cmd = m.table.Update(msg) + return m, cmd +} + +func (m Model) View() string { + if m.err != nil { + return AppStyle.Render(ErrStyle.Render("Error: " + m.err.Error())) + } + + if m.loadingGithubTz { + return AppStyle.Render(SubtleStyle.Render("Fetching github commits...")) + } + + if len(m.preds) == 0 { + return AppStyle.Render(SubtleStyle.Render("No valid tzs found. Are you sure it's correct?")) + } + + var header string + // manual mode + if m.manualTz != "" { + header += SubtleStyle.Render(fmt.Sprintf("Tz : %s\n", m.displayTz)) + } else { + header += SubtleStyle.MaxWidth(60).Render(fmt.Sprintf("User : %s\nTz %s\n", m.username, m.displayTz)) + } + + table := m.table.View() + + view := lipgloss.JoinVertical(lipgloss.Left, header, table) + return AppStyle.Render(view) +} diff --git a/internal/ui/styles.go b/internal/ui/styles.go new file mode 100644 index 0000000..f26855c --- /dev/null +++ b/internal/ui/styles.go @@ -0,0 +1,18 @@ +package ui + +import "github.com/charmbracelet/lipgloss" + +// Go quirk: can't use := at package level, need o do this +var ( + AppStyle = lipgloss.NewStyle().Margin(1, 2) + + // 2. The Key Data (Green for tz, Pink for user) + KeyStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575")).Bold(true) + ValStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF007C")).Bold(true) + + // 3. The Subtle Text (Gray for labels/help) + SubtleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")) + + // 4. Error (Red) + ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF5F87")) +) diff --git a/internal/ui/table.go b/internal/ui/table.go new file mode 100644 index 0000000..14377f6 --- /dev/null +++ b/internal/ui/table.go @@ -0,0 +1,32 @@ +package ui + +import ( + "fmt" + "tz-snipe/internal/core" + + "github.com/charmbracelet/bubbles/table" +) + +func newTable(preds []core.Prediction) table.Model { + cols := []table.Column{ + {Title: "Country", Width: 20}, + {Title: "Probability", Width: 30}, + } + + var rows []table.Row + + // format data & build rows + for i := 0; i < len(preds); i++ { + p := preds[i] + rows = append(rows, table.Row{p.Country, fmt.Sprintf("%.4f", p.Probability*100)}) + } + + table := table.New( + table.WithColumns(cols), + table.WithRows(rows), + table.WithFocused(true), + table.WithHeight(10), // scrollable after 10 + ) + + return table +}