This commit is contained in:
june 2025-03-09 10:06:40 +13:00
parent db8e66666c
commit 074cb059eb
Signed by untrusted user who does not match committer: breadone
GPG Key ID: FDC19FE143200483
2 changed files with 70 additions and 60 deletions

View File

@ -1,40 +1,41 @@
package main package main
import( import (
"fmt" "fmt"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please." // Just return `nil`, which means "no I/O right now, please."
return nil return nil
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
// Is it a key press? // Is it a key press?
case tea.KeyMsg: case tea.KeyMsg:
// Cool, what was the actual key pressed? // Cool, what was the actual key pressed?
switch msg.String() { switch msg.String() {
// These keys should exit the program. // These keys should exit the program.
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit return m, tea.Quit
// The "up" and "k" keys move the cursor up // The "up" and "k" keys move the cursor up
case "up", "k": case "up", "k":
if m.cursor > 0 { if m.cursor > 0 {
m.cursor-- 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": case "left", "h":
if m.tab > 0 { if m.tab > 0 {
m.tab-- m.tab--
@ -45,32 +46,32 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.tab++ m.tab++
} }
// The "enter" key and the spacebar (a literal space) toggle // The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at. // the selected state for the item that the cursor is pointing at.
case "enter", " ": case "enter", " ":
_, ok := m.selected[m.cursor] _, ok := m.selected[m.cursor]
if ok { if ok {
delete(m.selected, m.cursor) delete(m.selected, m.cursor)
} else { } else {
m.selected[m.cursor] = struct{}{} m.selected[m.cursor] = struct{}{}
} }
} }
} }
// Return the updated model to the Bubble Tea runtime for processing. // Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command. // Note that we're not returning a command.
return m, nil return m, nil
} }
func (m model) View() string { func (m model) View() string {
// The header // The header
s := "" s := ""
currentList := []todo{} currentList := []todo{}
switch m.tab { switch m.tab {
case 0: case 0:
s += "Inbox" s += "Inbox"
inboxFilter := func(t todo) bool { return t.isInbox} inboxFilter := func(t todo) bool { return t.isInbox }
currentList = filter(m.todos, inboxFilter) currentList = filter(m.todos, inboxFilter)
case 1: case 1:
s += "Today" s += "Today"
@ -84,30 +85,29 @@ func (m model) View() string {
s += "\n\n" s += "\n\n"
// Iterate over our choices
for i, choice := range currentList {
// Iterate over our choices // Is the cursor pointing at this choice?
for i, choice := range currentList { cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Is the cursor pointing at this choice? // Is this choice selected?
cursor := " " // no cursor checked := " " // not selected
if m.cursor == i { if _, ok := m.selected[i]; ok {
cursor = ">" // cursor! checked = "x" // selected!
} }
// Is this choice selected? // Render the row
checked := " " // not selected s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
if _, ok := m.selected[i]; ok { }
checked = "x" // selected!
}
// Render the row // The footer
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
// The footer
s += "n: new\n" s += "n: new\n"
s += "q: quit.\n" s += "q: quit.\n"
// Send the UI for rendering // Send the UI for rendering
return s return s
} }

10
helper.go Normal file
View File

@ -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
}