Refactor picker

This commit is contained in:
Martin
2026-02-01 11:41:41 +01:00
parent 5de3b646fc
commit b47763034b
5 changed files with 271 additions and 122 deletions

144
components/picker/picker.go Normal file
View File

@ -0,0 +1,144 @@
package picker
import (
"tasksquire/common"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Item struct {
text string
}
func NewItem(text string) Item { return Item{text: text} }
func (i Item) Title() string { return i.text }
func (i Item) Description() string { return "" }
func (i Item) FilterValue() string { return i.text }
type Picker struct {
common *common.Common
list list.Model
onSelect func(list.Item) tea.Cmd
title string
filterByDefault bool
}
type PickerOption func(*Picker)
func WithFilterByDefault(enabled bool) PickerOption {
return func(p *Picker) {
p.filterByDefault = enabled
}
}
func New(
c *common.Common,
title string,
items []list.Item,
onSelect func(list.Item) tea.Cmd,
opts ...PickerOption,
) *Picker {
delegate := list.NewDefaultDelegate()
delegate.ShowDescription = false
delegate.SetSpacing(0)
l := list.New(items, delegate, 0, 0)
l.SetShowTitle(false)
l.SetShowHelp(false)
l.SetShowStatusBar(false)
l.SetFilteringEnabled(true)
// Custom key for filtering (insert mode)
l.KeyMap.Filter = key.NewBinding(
key.WithKeys("i"),
key.WithHelp("i", "filter"),
)
p := &Picker{
common: c,
list: l,
onSelect: onSelect,
title: title,
}
if c.TW.GetConfig().Get("uda.tasksquire.picker.filter_by_default") == "yes" {
p.filterByDefault = true
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Picker) SetSize(width, height int) {
// We do NOT set common.SetSize here, as we are a sub-component.
// Set list size. The parent is responsible for providing a reasonable size.
// If this component is intended to fill a page, width/height will be large.
// If it's a small embedded box, they will be small.
// We apply a small margin for the title if needed, but for now we just pass through
// minus a header gap if we render a title.
headerHeight := 2 // Title + gap
p.list.SetSize(width, height-headerHeight)
}
func (p *Picker) Init() tea.Cmd {
if p.filterByDefault {
return func() tea.Msg {
return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'i'}}
}
}
return nil
}
func (p *Picker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
// If filtering, let the list handle keys (including Enter to stop filtering)
if p.list.FilterState() == list.Filtering {
if key.Matches(msg, p.common.Keymap.Ok) {
items := p.list.VisibleItems()
if len(items) == 1 {
return p, p.onSelect(items[0])
}
}
break // Pass to list.Update
}
switch {
case key.Matches(msg, p.common.Keymap.Ok):
selectedItem := p.list.SelectedItem()
if selectedItem == nil {
return p, nil
}
return p, p.onSelect(selectedItem)
}
}
p.list, cmd = p.list.Update(msg)
return p, cmd
}
func (p *Picker) View() string {
title := p.common.Styles.Form.Focused.Title.Render(p.title)
return lipgloss.JoinVertical(lipgloss.Left, title, p.list.View())
}
// SelectItemByFilterValue selects the item with the given filter value
func (p *Picker) SelectItemByFilterValue(filterValue string) {
items := p.list.Items()
for i, item := range items {
if item.FilterValue() == filterValue {
p.list.Select(i)
break
}
}
}

View File

@ -4,18 +4,19 @@ import (
"log/slog" "log/slog"
"slices" "slices"
"tasksquire/common" "tasksquire/common"
"tasksquire/components/picker"
"tasksquire/taskwarrior" "tasksquire/taskwarrior"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type ContextPickerPage struct { type ContextPickerPage struct {
common *common.Common common *common.Common
contexts taskwarrior.Contexts contexts taskwarrior.Contexts
form *huh.Form picker *picker.Picker
} }
func NewContextPickerPage(common *common.Common) *ContextPickerPage { func NewContextPickerPage(common *common.Common) *ContextPickerPage {
@ -34,19 +35,22 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
slices.Sort(options) slices.Sort(options)
options = append([]string{"(none)"}, options...) options = append([]string{"(none)"}, options...)
p.form = huh.NewForm( items := []list.Item{}
huh.NewGroup( for _, opt := range options {
huh.NewSelect[string](). items = append(items, picker.NewItem(opt))
Key("context"). }
Options(huh.NewOptions(options...)...).
Title("Contexts"). onSelect := func(item list.Item) tea.Cmd {
Description("Choose a context"). return func() tea.Msg { return contextSelectedMsg{item: item} }
Value(&selected). }
WithTheme(common.Styles.Form),
), p.picker = picker.New(common, "Contexts", items, onSelect)
).
WithShowHelp(false). // Set active context
WithShowErrors(true) if selected == "" {
selected = "(none)"
}
p.picker.SelectItemByFilterValue(selected)
p.SetSize(common.Width(), common.Height()) p.SetSize(common.Width(), common.Height())
@ -56,29 +60,46 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
func (p *ContextPickerPage) SetSize(width, height int) { func (p *ContextPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height) p.common.SetSize(width, height)
if width >= 20 { // Set list size with some padding/limits to look like a picker
p.form = p.form.WithWidth(20) listWidth := width - 4
} else { if listWidth > 40 {
p.form = p.form.WithWidth(width) listWidth = 40
} }
listHeight := height - 6
if height >= 30 { if listHeight > 20 {
p.form = p.form.WithHeight(30) listHeight = 20
} else {
p.form = p.form.WithHeight(height)
} }
p.picker.SetSize(listWidth, listHeight)
} }
func (p *ContextPickerPage) Init() tea.Cmd { func (p *ContextPickerPage) Init() tea.Cmd {
return p.form.Init() return p.picker.Init()
}
type contextSelectedMsg struct {
item list.Item
} }
func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height) p.SetSize(msg.Width, msg.Height)
case contextSelectedMsg:
name := msg.item.(picker.Item).Title()
if name == "(none)" {
name = ""
}
ctx := p.common.TW.GetContext(name)
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, func() tea.Msg { return UpdateContextMsg(ctx) }
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, p.common.Keymap.Back): case key.Matches(msg, p.common.Keymap.Back):
@ -91,41 +112,26 @@ func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
} }
f, cmd := p.form.Update(msg) _, cmd = p.picker.Update(msg)
if f, ok := f.(*huh.Form); ok { return p, cmd
p.form = f
cmds = append(cmds, cmd)
}
if p.form.State == huh.StateCompleted {
cmds = append(cmds, p.updateContextCmd)
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, tea.Batch(cmds...)
}
return p, tea.Batch(cmds...)
} }
func (p *ContextPickerPage) View() string { func (p *ContextPickerPage) View() string {
width := p.common.Width() - 4
if width > 40 {
width = 40
}
content := p.picker.View()
styledContent := lipgloss.NewStyle().Width(width).Render(content)
return lipgloss.Place( return lipgloss.Place(
p.common.Width(), p.common.Width(),
p.common.Height(), p.common.Height(),
lipgloss.Center, lipgloss.Center,
lipgloss.Center, lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()), p.common.Styles.Base.Render(styledContent),
) )
} }
func (p *ContextPickerPage) updateContextCmd() tea.Msg {
context := p.form.GetString("context")
if context == "(none)" {
context = ""
}
return UpdateContextMsg(p.common.TW.GetContext(context))
}
type UpdateContextMsg *taskwarrior.Context type UpdateContextMsg *taskwarrior.Context

View File

@ -3,16 +3,17 @@ package pages
import ( import (
"log/slog" "log/slog"
"tasksquire/common" "tasksquire/common"
"tasksquire/components/picker"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type ProjectPickerPage struct { type ProjectPickerPage struct {
common *common.Common common *common.Common
form *huh.Form picker *picker.Picker
} }
func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectPickerPage { func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectPickerPage {
@ -20,30 +21,23 @@ func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectP
common: common, common: common,
} }
var selected string projects := common.TW.GetProjects()
if activeProject == "" { items := []list.Item{picker.NewItem("(none)")}
selected = "(none)" for _, proj := range projects {
} else { items = append(items, picker.NewItem(proj))
selected = activeProject
} }
projects := common.TW.GetProjects() onSelect := func(item list.Item) tea.Cmd {
options := []string{"(none)"} return func() tea.Msg { return projectSelectedMsg{item: item} }
options = append(options, projects...) }
p.form = huh.NewForm( p.picker = picker.New(common, "Projects", items, onSelect)
huh.NewGroup(
huh.NewSelect[string](). // Set active project
Key("project"). if activeProject == "" {
Options(huh.NewOptions(options...)...). activeProject = "(none)"
Title("Projects"). }
Description("Choose a project"). p.picker.SelectItemByFilterValue(activeProject)
Value(&selected).
WithTheme(common.Styles.Form),
),
).
WithShowHelp(false).
WithShowErrors(false)
p.SetSize(common.Width(), common.Height()) p.SetSize(common.Width(), common.Height())
@ -53,29 +47,44 @@ func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectP
func (p *ProjectPickerPage) SetSize(width, height int) { func (p *ProjectPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height) p.common.SetSize(width, height)
if width >= 20 { // Set list size with some padding/limits to look like a picker
p.form = p.form.WithWidth(20) listWidth := width - 4
} else { if listWidth > 40 {
p.form = p.form.WithWidth(width) listWidth = 40
} }
listHeight := height - 6
if height >= 30 { if listHeight > 20 {
p.form = p.form.WithHeight(30) listHeight = 20
} else {
p.form = p.form.WithHeight(height)
} }
p.picker.SetSize(listWidth, listHeight)
} }
func (p *ProjectPickerPage) Init() tea.Cmd { func (p *ProjectPickerPage) Init() tea.Cmd {
return p.form.Init() return p.picker.Init()
}
type projectSelectedMsg struct {
item list.Item
} }
func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height) p.SetSize(msg.Width, msg.Height)
case projectSelectedMsg:
proj := msg.item.(picker.Item).Title()
if proj == "(none)" {
proj = ""
}
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, func() tea.Msg { return UpdateProjectMsg(proj) }
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, p.common.Keymap.Back): case key.Matches(msg, p.common.Keymap.Back):
@ -88,41 +97,30 @@ func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
} }
f, cmd := p.form.Update(msg) _, cmd = p.picker.Update(msg)
if f, ok := f.(*huh.Form); ok { return p, cmd
p.form = f
cmds = append(cmds, cmd)
}
if p.form.State == huh.StateCompleted {
cmds = append(cmds, p.updateProjectCmd)
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, tea.Batch(cmds...)
}
return p, tea.Batch(cmds...)
} }
func (p *ProjectPickerPage) View() string { func (p *ProjectPickerPage) View() string {
width := p.common.Width() - 4
if width > 40 {
width = 40
}
content := p.picker.View()
styledContent := lipgloss.NewStyle().Width(width).Render(content)
return lipgloss.Place( return lipgloss.Place(
p.common.Width(), p.common.Width(),
p.common.Height(), p.common.Height(),
lipgloss.Center, lipgloss.Center,
lipgloss.Center, lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()), p.common.Styles.Base.Render(styledContent),
) )
} }
func (p *ProjectPickerPage) updateProjectCmd() tea.Msg { func (p *ProjectPickerPage) updateProjectCmd() tea.Msg {
project := p.form.GetString("project") return nil
if project == "(none)" {
project = ""
}
return UpdateProjectMsg(project)
} }
type UpdateProjectMsg string type UpdateProjectMsg string

View File

@ -94,24 +94,24 @@ func (p *ReportPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, tea.Quit return p, tea.Quit
case key.Matches(msg, p.common.Keymap.SetReport): case key.Matches(msg, p.common.Keymap.SetReport):
p.subpage = NewReportPickerPage(p.common, p.activeReport) p.subpage = NewReportPickerPage(p.common, p.activeReport)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.SetContext): case key.Matches(msg, p.common.Keymap.SetContext):
p.subpage = NewContextPickerPage(p.common) p.subpage = NewContextPickerPage(p.common)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Add): case key.Matches(msg, p.common.Keymap.Add):
p.subpage = NewTaskEditorPage(p.common, taskwarrior.NewTask()) p.subpage = NewTaskEditorPage(p.common, taskwarrior.NewTask())
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Edit): case key.Matches(msg, p.common.Keymap.Edit):
p.subpage = NewTaskEditorPage(p.common, *p.selectedTask) p.subpage = NewTaskEditorPage(p.common, *p.selectedTask)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Ok): case key.Matches(msg, p.common.Keymap.Ok):
p.common.TW.SetTaskDone(p.selectedTask) p.common.TW.SetTaskDone(p.selectedTask)
return p, p.getTasks() return p, p.getTasks()
@ -120,9 +120,9 @@ func (p *ReportPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, p.getTasks() return p, p.getTasks()
case key.Matches(msg, p.common.Keymap.SetProject): case key.Matches(msg, p.common.Keymap.SetProject):
p.subpage = NewProjectPickerPage(p.common, p.activeProject) p.subpage = NewProjectPickerPage(p.common, p.activeProject)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Tag): case key.Matches(msg, p.common.Keymap.Tag):
if p.selectedTask != nil { if p.selectedTask != nil {
tag := p.common.TW.GetConfig().Get("uda.tasksquire.tag.default") tag := p.common.TW.GetConfig().Get("uda.tasksquire.tag.default")

View File

@ -12,9 +12,10 @@ type TWConfig struct {
var ( var (
defaultConfig = map[string]string{ defaultConfig = map[string]string{
"uda.tasksquire.report.default": "next", "uda.tasksquire.report.default": "next",
"uda.tasksquire.tag.default": "next", "uda.tasksquire.tag.default": "next",
"uda.tasksquire.tags.default": "low_energy,customer,delegate,code,communication,research", "uda.tasksquire.tags.default": "mngmnt,ops,low_energy,cust,delegate,code,comm,research",
"uda.tasksquire.picker.filter_by_default": "yes",
} }
) )