Initial commit
This commit is contained in:
105
pages/contextPicker.go
Normal file
105
pages/contextPicker.go
Normal file
@ -0,0 +1,105 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"slices"
|
||||
"tasksquire/common"
|
||||
"tasksquire/taskwarrior"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
type ContextPickerPage struct {
|
||||
common *common.Common
|
||||
contexts taskwarrior.Contexts
|
||||
form *huh.Form
|
||||
}
|
||||
|
||||
func NewContextPickerPage(common *common.Common) *ContextPickerPage {
|
||||
p := &ContextPickerPage{
|
||||
common: common,
|
||||
contexts: common.TW.GetContexts(),
|
||||
}
|
||||
|
||||
// if allowAdd {
|
||||
// fields = append(fields, huh.NewInput().
|
||||
// Key("input").
|
||||
// Title("Input").
|
||||
// Prompt(fmt.Sprintf("Enter a new %s", header)).
|
||||
// Inline(false),
|
||||
// )
|
||||
// }
|
||||
|
||||
selected := common.TW.GetActiveContext().Name
|
||||
options := make([]string, 0)
|
||||
for _, c := range p.contexts {
|
||||
options = append(options, c.Name)
|
||||
}
|
||||
slices.Sort(options)
|
||||
|
||||
p.form = huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().
|
||||
Key("context").
|
||||
Options(huh.NewOptions(options...)...).
|
||||
Title("Contexts").
|
||||
Description("Choose a context").
|
||||
Value(&selected),
|
||||
),
|
||||
).
|
||||
WithShowHelp(false).
|
||||
WithShowErrors(true)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *ContextPickerPage) Init() tea.Cmd {
|
||||
return p.form.Init()
|
||||
}
|
||||
|
||||
func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmds []tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch {
|
||||
case key.Matches(msg, p.common.Keymap.Back):
|
||||
model, err := p.common.PageStack.Pop()
|
||||
if err != nil {
|
||||
slog.Error("page stack empty")
|
||||
return nil, tea.Quit
|
||||
}
|
||||
return model, BackCmd
|
||||
}
|
||||
}
|
||||
|
||||
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.PageStack.Pop()
|
||||
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 {
|
||||
return p.common.Styles.Main.Render(p.form.View())
|
||||
}
|
||||
|
||||
func (p *ContextPickerPage) updateContextCmd() tea.Msg {
|
||||
return UpdateContextMsg(p.common.TW.GetContext(p.form.GetString("context")))
|
||||
}
|
||||
|
||||
type UpdateContextMsg *taskwarrior.Context
|
||||
Reference in New Issue
Block a user