Add time page
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 ReportPickerPage struct {
|
||||
common *common.Common
|
||||
reports taskwarrior.Reports
|
||||
form *huh.Form
|
||||
picker *picker.Picker
|
||||
}
|
||||
|
||||
func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report) *ReportPickerPage {
|
||||
@ -24,27 +25,29 @@ func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report
|
||||
reports: common.TW.GetReports(),
|
||||
}
|
||||
|
||||
selected := activeReport.Name
|
||||
itemProvider := func() []list.Item {
|
||||
options := make([]string, 0)
|
||||
for _, r := range p.reports {
|
||||
options = append(options, r.Name)
|
||||
}
|
||||
slices.Sort(options)
|
||||
|
||||
options := make([]string, 0)
|
||||
for _, r := range p.reports {
|
||||
options = append(options, r.Name)
|
||||
items := []list.Item{}
|
||||
for _, opt := range options {
|
||||
items = append(items, picker.NewItem(opt))
|
||||
}
|
||||
return items
|
||||
}
|
||||
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)
|
||||
onSelect := func(item list.Item) tea.Cmd {
|
||||
return func() tea.Msg { return reportSelectedMsg{item: item} }
|
||||
}
|
||||
|
||||
p.picker = picker.New(common, "Reports", itemProvider, onSelect)
|
||||
|
||||
if activeReport != nil {
|
||||
p.picker.SelectItemByFilterValue(activeReport.Name)
|
||||
}
|
||||
|
||||
p.SetSize(common.Width(), common.Height())
|
||||
|
||||
@ -54,72 +57,76 @@ func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report
|
||||
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)
|
||||
// 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 *ReportPickerPage) Init() tea.Cmd {
|
||||
return p.form.Init()
|
||||
return p.picker.Init()
|
||||
}
|
||||
|
||||
type reportSelectedMsg struct {
|
||||
item list.Item
|
||||
}
|
||||
|
||||
func (p *ReportPickerPage) 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 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
|
||||
}
|
||||
}
|
||||
case reportSelectedMsg:
|
||||
reportName := msg.item.FilterValue()
|
||||
report := p.common.TW.GetReport(reportName)
|
||||
|
||||
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 model, func() tea.Msg { return UpdateReportMsg(report) }
|
||||
case tea.KeyMsg:
|
||||
if !p.picker.IsFiltering() {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return p, tea.Batch(cmds...)
|
||||
_, cmd = p.picker.Update(msg)
|
||||
return p, cmd
|
||||
}
|
||||
|
||||
func (p *ReportPickerPage) 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 *ReportPickerPage) updateReportCmd() tea.Msg {
|
||||
return UpdateReportMsg(p.common.TW.GetReport(p.form.GetString("report")))
|
||||
}
|
||||
|
||||
type UpdateReportMsg *taskwarrior.Report
|
||||
type UpdateReportMsg *taskwarrior.Report
|
||||
Reference in New Issue
Block a user