Initial commit
This commit is contained in:
97
pages/reportPicker.go
Normal file
97
pages/reportPicker.go
Normal file
@ -0,0 +1,97 @@
|
||||
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 ReportPickerPage struct {
|
||||
common *common.Common
|
||||
reports taskwarrior.Reports
|
||||
form *huh.Form
|
||||
}
|
||||
|
||||
func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report) *ReportPickerPage {
|
||||
p := &ReportPickerPage{
|
||||
common: common,
|
||||
reports: common.TW.GetReports(),
|
||||
}
|
||||
|
||||
selected := activeReport.Name
|
||||
|
||||
options := make([]string, 0)
|
||||
for _, r := range p.reports {
|
||||
options = append(options, r.Name)
|
||||
}
|
||||
slices.Sort(options)
|
||||
|
||||
p.form = huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().
|
||||
Key("report").
|
||||
Options(huh.NewOptions(options...)...).
|
||||
Title("Reports").
|
||||
Description("Choose a report").
|
||||
Value(&selected),
|
||||
),
|
||||
).
|
||||
WithShowHelp(false).
|
||||
WithShowErrors(false)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *ReportPickerPage) Init() tea.Cmd {
|
||||
return p.form.Init()
|
||||
}
|
||||
|
||||
func (p *ReportPickerPage) 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, []tea.Cmd{BackCmd, p.updateReportCmd}...)
|
||||
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 *ReportPickerPage) View() string {
|
||||
return p.common.Styles.Main.Render(p.form.View())
|
||||
}
|
||||
|
||||
func (p *ReportPickerPage) updateReportCmd() tea.Msg {
|
||||
return UpdateReportMsg(p.common.TW.GetReport(p.form.GetString("report")))
|
||||
}
|
||||
|
||||
type UpdateReportMsg *taskwarrior.Report
|
||||
Reference in New Issue
Block a user