From 074cb059eb51dc030ac5c4cd7631a5e397b075e8 Mon Sep 17 00:00:00 2001 From: june Date: Sun, 9 Mar 2025 10:06:40 +1300 Subject: [PATCH] stuff --- bubbletea.go | 120 +++++++++++++++++++++++++-------------------------- helper.go | 10 +++++ 2 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 helper.go diff --git a/bubbletea.go b/bubbletea.go index ed983e8..faf6312 100644 --- a/bubbletea.go +++ b/bubbletea.go @@ -1,40 +1,41 @@ package main -import( +import ( "fmt" + tea "github.com/charmbracelet/bubbletea" ) func (m model) Init() tea.Cmd { - // Just return `nil`, which means "no I/O right now, please." - return nil + // Just return `nil`, which means "no I/O right now, please." + return nil } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { + switch msg := msg.(type) { - // Is it a key press? - case tea.KeyMsg: + // Is it a key press? + case tea.KeyMsg: - // Cool, what was the actual key pressed? - switch msg.String() { + // Cool, what was the actual key pressed? + switch msg.String() { - // These keys should exit the program. - case "ctrl+c", "q": - return m, tea.Quit + // These keys should exit the program. + case "ctrl+c", "q": + return m, tea.Quit - // The "up" and "k" keys move the cursor up - case "up", "k": - if m.cursor > 0 { - m.cursor-- - } + // The "up" and "k" keys move the cursor up + case "up", "k": + if m.cursor > 0 { + m.cursor-- + } + + // The "down" and "j" keys move the cursor down + case "down", "j": + if m.cursor < len(m.todos)-1 { + m.cursor++ + } - // The "down" and "j" keys move the cursor down - case "down", "j": - if m.cursor < len(m.todos)-1 { - m.cursor++ - } - case "left", "h": if m.tab > 0 { m.tab-- @@ -45,32 +46,32 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.tab++ } - // The "enter" key and the spacebar (a literal space) toggle - // the selected state for the item that the cursor is pointing at. - case "enter", " ": - _, ok := m.selected[m.cursor] - if ok { - delete(m.selected, m.cursor) - } else { - m.selected[m.cursor] = struct{}{} - } - } - } + // The "enter" key and the spacebar (a literal space) toggle + // the selected state for the item that the cursor is pointing at. + case "enter", " ": + _, ok := m.selected[m.cursor] + if ok { + delete(m.selected, m.cursor) + } else { + m.selected[m.cursor] = struct{}{} + } + } + } - // Return the updated model to the Bubble Tea runtime for processing. - // Note that we're not returning a command. - return m, nil + // Return the updated model to the Bubble Tea runtime for processing. + // Note that we're not returning a command. + return m, nil } func (m model) View() string { - // The header - s := "" + // The header + s := "" currentList := []todo{} switch m.tab { case 0: s += "Inbox" - inboxFilter := func(t todo) bool { return t.isInbox} + inboxFilter := func(t todo) bool { return t.isInbox } currentList = filter(m.todos, inboxFilter) case 1: s += "Today" @@ -84,30 +85,29 @@ func (m model) View() string { s += "\n\n" + // Iterate over our choices + for i, choice := range currentList { - // Iterate over our choices - for i, choice := range currentList { + // Is the cursor pointing at this choice? + cursor := " " // no cursor + if m.cursor == i { + cursor = ">" // cursor! + } - // Is the cursor pointing at this choice? - cursor := " " // no cursor - if m.cursor == i { - cursor = ">" // cursor! - } + // Is this choice selected? + checked := " " // not selected + if _, ok := m.selected[i]; ok { + checked = "x" // selected! + } - // Is this choice selected? - checked := " " // not selected - if _, ok := m.selected[i]; ok { - checked = "x" // selected! - } + // Render the row + s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) + } - // Render the row - s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) - } - - // The footer + // The footer s += "n: new\n" - s += "q: quit.\n" + s += "q: quit.\n" - // Send the UI for rendering - return s -} \ No newline at end of file + // Send the UI for rendering + return s +} diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..f22aa6e --- /dev/null +++ b/helper.go @@ -0,0 +1,10 @@ +package main + +func filter[T any](ss []T, test func(T) bool) (ret []T) { + for _, s := range ss { + if test(s) { + ret = append(ret, s) + } + } + return +} \ No newline at end of file