Fix pickers; Add new select option

This commit is contained in:
Martin
2024-06-24 16:36:11 +02:00
parent fac7ff81dd
commit 73d51b956a
8 changed files with 715 additions and 31 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type ContextPickerPage struct {
@ -40,18 +41,32 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
Options(huh.NewOptions(options...)...).
Title("Contexts").
Description("Choose a context").
Value(&selected),
Value(&selected).
WithTheme(common.Styles.Form),
),
).
WithShowHelp(false).
WithShowErrors(true).
WithTheme(p.common.Styles.Form)
WithShowErrors(true)
p.SetSize(common.Width(), common.Height())
return p
}
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)
}
if height >= 30 {
p.form = p.form.WithHeight(30)
} else {
p.form = p.form.WithHeight(height)
}
}
func (p *ContextPickerPage) Init() tea.Cmd {
@ -96,7 +111,13 @@ func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (p *ContextPickerPage) View() string {
return p.common.Styles.Base.Render(p.form.View())
return lipgloss.Place(
p.common.Width(),
p.common.Height(),
lipgloss.Center,
lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()),
)
}
func (p *ContextPickerPage) updateContextCmd() tea.Msg {

View File

@ -7,6 +7,7 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type ProjectPickerPage struct {
@ -37,17 +38,32 @@ func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectP
Options(huh.NewOptions(options...)...).
Title("Projects").
Description("Choose a project").
Value(&selected),
Value(&selected).
WithTheme(common.Styles.Form),
),
).
WithShowHelp(false).
WithShowErrors(false)
p.SetSize(common.Width(), common.Height())
return p
}
func (p *ProjectPickerPage) 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 *ProjectPickerPage) Init() tea.Cmd {
@ -92,7 +108,13 @@ func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (p *ProjectPickerPage) View() string {
return p.common.Styles.Base.Render(p.form.View())
return lipgloss.Place(
p.common.Width(),
p.common.Height(),
lipgloss.Center,
lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()),
)
}
func (p *ProjectPickerPage) updateProjectCmd() tea.Msg {

View File

@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type ReportPickerPage struct {
@ -38,17 +39,32 @@ func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report
Options(huh.NewOptions(options...)...).
Title("Reports").
Description("Choose a report").
Value(&selected),
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 {
@ -93,7 +109,13 @@ func (p *ReportPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (p *ReportPickerPage) View() string {
return p.common.Styles.Base.Render(p.form.View())
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 {

View File

@ -382,13 +382,13 @@ type taskEdit struct {
fields []huh.Field
cursor int
newProjectName *string
newAnnotation *string
udaValues map[string]*string
// newProjectName *string
newAnnotation *string
udaValues map[string]*string
}
func NewTaskEdit(com *common.Common, task *taskwarrior.Task) *taskEdit {
newProject := ""
// newProject := ""
projectOptions := append([]string{"(none)"}, com.TW.GetProjects()...)
if task.Project == "" {
task.Project = "(none)"
@ -410,19 +410,19 @@ func NewTaskEdit(com *common.Common, task *taskwarrior.Task) *taskEdit {
Prompt(": ").
WithTheme(com.Styles.Form),
huh.NewSelect[string]().
Options(huh.NewOptions(projectOptions...)...).
input.NewSelect(com).
Options(true, input.NewOptions(projectOptions...)...).
Title("Project").
Value(&task.Project).
WithKeyMap(defaultKeymap).
WithTheme(com.Styles.Form),
huh.NewInput().
Title("New Project").
Value(&newProject).
Inline(true).
Prompt(": ").
WithTheme(com.Styles.Form),
// huh.NewInput().
// Title("New Project").
// Value(&newProject).
// Inline(true).
// Prompt(": ").
// WithTheme(com.Styles.Form),
}
udaValues := make(map[string]*string)
@ -513,8 +513,8 @@ func NewTaskEdit(com *common.Common, task *taskwarrior.Task) *taskEdit {
udaValues: udaValues,
newProjectName: &newProject,
newAnnotation: &newAnnotation,
// newProjectName: &newProject,
newAnnotation: &newAnnotation,
}
t.fields[0].Focus()
@ -997,9 +997,9 @@ func (p *TaskEditorPage) updateTasksCmd() tea.Msg {
}
}
if *(p.areas[0].(*taskEdit).newProjectName) != "" {
p.task.Project = *p.areas[0].(*taskEdit).newProjectName
}
// if *(p.areas[0].(*taskEdit).newProjectName) != "" {
// p.task.Project = *p.areas[0].(*taskEdit).newProjectName
// }
if *(p.areas[1].(*tagEdit).newTagsValue) != "" {
newTags := strings.Split(*p.areas[1].(*tagEdit).newTagsValue, " ")