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

View File

@ -4,18 +4,19 @@ import (
"log/slog"
"slices"
"tasksquire/common"
"tasksquire/components/picker"
"tasksquire/taskwarrior"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type ContextPickerPage struct {
common *common.Common
contexts taskwarrior.Contexts
form *huh.Form
picker *picker.Picker
}
func NewContextPickerPage(common *common.Common) *ContextPickerPage {
@ -34,19 +35,22 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
slices.Sort(options)
options = append([]string{"(none)"}, options...)
p.form = huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Key("context").
Options(huh.NewOptions(options...)...).
Title("Contexts").
Description("Choose a context").
Value(&selected).
WithTheme(common.Styles.Form),
),
).
WithShowHelp(false).
WithShowErrors(true)
items := []list.Item{}
for _, opt := range options {
items = append(items, picker.NewItem(opt))
}
onSelect := func(item list.Item) tea.Cmd {
return func() tea.Msg { return contextSelectedMsg{item: item} }
}
p.picker = picker.New(common, "Contexts", items, onSelect)
// Set active context
if selected == "" {
selected = "(none)"
}
p.picker.SelectItemByFilterValue(selected)
p.SetSize(common.Width(), common.Height())
@ -56,29 +60,46 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
func (p *ContextPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height)
if width >= 20 {
p.form = p.form.WithWidth(20)
} else {
p.form = p.form.WithWidth(width)
// Set list size with some padding/limits to look like a picker
listWidth := width - 4
if listWidth > 40 {
listWidth = 40
}
if height >= 30 {
p.form = p.form.WithHeight(30)
} else {
p.form = p.form.WithHeight(height)
listHeight := height - 6
if listHeight > 20 {
listHeight = 20
}
p.picker.SetSize(listWidth, listHeight)
}
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) {
var cmds []tea.Cmd
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
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:
switch {
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)
if f, ok := f.(*huh.Form); ok {
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...)
_, cmd = p.picker.Update(msg)
return p, cmd
}
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(
p.common.Width(),
p.common.Height(),
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

View File

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

View File

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