126 lines
2.5 KiB
Go
126 lines
2.5 KiB
Go
package pages
|
|
|
|
import (
|
|
"log/slog"
|
|
"slices"
|
|
"tasksquire/common"
|
|
"tasksquire/taskwarrior"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/huh"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
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).
|
|
WithTheme(common.Styles.Form),
|
|
),
|
|
).
|
|
WithShowHelp(false).
|
|
WithShowErrors(false)
|
|
|
|
p.SetSize(common.Width(), common.Height())
|
|
|
|
return p
|
|
}
|
|
|
|
func (p *ReportPickerPage) 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)
|
|
}
|
|
|
|
if height >= 30 {
|
|
p.form = p.form.WithHeight(30)
|
|
} else {
|
|
p.form = p.form.WithHeight(height)
|
|
}
|
|
}
|
|
|
|
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.WindowSizeMsg:
|
|
p.SetSize(msg.Width, msg.Height)
|
|
case tea.KeyMsg:
|
|
switch {
|
|
case key.Matches(msg, p.common.Keymap.Back):
|
|
model, err := p.common.PopPage()
|
|
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.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 *ReportPickerPage) View() string {
|
|
return lipgloss.Place(
|
|
p.common.Width(),
|
|
p.common.Height(),
|
|
lipgloss.Center,
|
|
lipgloss.Center,
|
|
p.common.Styles.Base.Render(p.form.View()),
|
|
)
|
|
}
|
|
|
|
func (p *ReportPickerPage) updateReportCmd() tea.Msg {
|
|
return UpdateReportMsg(p.common.TW.GetReport(p.form.GetString("report")))
|
|
}
|
|
|
|
type UpdateReportMsg *taskwarrior.Report
|