Refactor picker
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user