From ac37f53069192853b6324a86d19de00112e0af2c Mon Sep 17 00:00:00 2001 From: june Date: Sun, 9 Mar 2025 10:40:37 +1300 Subject: [PATCH] Adding new task menu, better tab bar ui using lipgloss --- bubbletea.go | 28 ++++++++++++++++++---------- go.mod | 6 +++++- go.sum | 4 ++++ model.go | 5 +++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/bubbletea.go b/bubbletea.go index e9e7040..a56c84e 100644 --- a/bubbletea.go +++ b/bubbletea.go @@ -2,7 +2,7 @@ package main import ( "fmt" - + "github.com/charmbracelet/lipgloss" tea "github.com/charmbracelet/bubbletea" ) @@ -12,6 +12,8 @@ func (m model) Init() tea.Cmd { } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmd tea.Cmd + switch msg := msg.(type) { // Is it a key press? @@ -26,23 +28,23 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // The "up" and "k" keys move the cursor up case "up", "k": - if m.cursor > 0 { + if m.cursor > 0 && !m.addTask { m.cursor-- } // The "down" and "j" keys move the cursor down case "down", "j": - if m.cursor < len(m.todos)-1 { + if m.cursor < len(m.todos)-1 && !m.addTask { m.cursor++ } case "left", "h": - if m.tab > 0 { + if m.tab > 0 && !m.addTask { m.tab-- } case "right", "l": - if m.tab < 4 { + if m.tab < 4 && !m.addTask { m.tab++ } @@ -65,9 +67,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } + m.textinput, cmd = m.textinput.Update(msg) + // Return the updated model to the Bubble Tea runtime for processing. // Note that we're not returning a command. - return m, nil + return m, cmd } func (m model) View() string { @@ -111,22 +115,26 @@ func (m model) View() string { s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) } + // render tab bar for i, v := range []string{"Inbox", "Today", "Tomorrow", "Scheduled", "Anytime"} { if i == m.tab { - s += fmt.Sprintf("**%s** ", v) + s += lipgloss.NewStyle().Bold(true).Render(fmt.Sprintf("%s ", v)) } else { s += fmt.Sprintf("%s ", v) } } // The footer - s += "\nn: new\n" - s += "q: quit.\n" + s += "\nn: new" if m.addTask { - + s += ": >" + m.textinput.Prompt = "New Task..." + s += m.textinput.View() } + s += "\nq: quit.\n" + // Send the UI for rendering return s } diff --git a/go.mod b/go.mod index 2a55b5b..1fe68a9 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,13 @@ module breadone/gotd go 1.24.1 -require github.com/charmbracelet/bubbletea v1.3.4 +require ( + github.com/charmbracelet/bubbles v0.20.0 + github.com/charmbracelet/bubbletea v1.3.4 +) require ( + github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/lipgloss v1.0.0 // indirect github.com/charmbracelet/x/ansi v0.8.0 // indirect diff --git a/go.sum b/go.sum index 7b9f814..386e628 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= 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/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE= +github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU= github.com/charmbracelet/bubbletea v1.3.4 h1:kCg7B+jSCFPLYRA52SDZjr51kG/fMUEoPoZrkaDHyoI= github.com/charmbracelet/bubbletea v1.3.4/go.mod h1:dtcUCyCGEX3g9tosuYiut3MXgY/Jsv9nKVdibKKRRXo= github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg= diff --git a/model.go b/model.go index 94f4130..3f592f7 100644 --- a/model.go +++ b/model.go @@ -1,5 +1,9 @@ package main +import ( + "github.com/charmbracelet/bubbles/textinput" +) + type todo struct { name string done bool @@ -16,6 +20,7 @@ type model struct { selected map[int]struct{} // which to-do items are selected tab int // which tab is selected addTask bool // defines if the new task window is shown + textinput textinput.Model } func initialModel() model {