[WIP] Editing

This commit is contained in:
Martin Pander
2024-05-23 16:01:25 +02:00
parent 7712711736
commit fe00170a5c
6 changed files with 133 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package pages
import (
"fmt"
"log/slog"
"strings"
"tasksquire/common"
"tasksquire/taskwarrior"
"time"
@ -24,11 +25,17 @@ const (
)
type TaskEditorPage struct {
common *common.Common
task taskwarrior.Task
form *huh.Form
mode Mode
statusline tea.Model
common *common.Common
task taskwarrior.Task
form *huh.Form
mode Mode
statusline tea.Model
nFields int
currentField int
// TODO: rework support for adding tags and projects
additionalTags string
additionalProject string
}
type TaskEditorKeys struct {
@ -85,11 +92,27 @@ func NewTaskEditorPage(common *common.Common, task taskwarrior.Task) *TaskEditor
Title("Project").
Value(&p.task.Project),
huh.NewInput().
Title("Project").
Value(&p.additionalProject).
Validate(func(project string) error {
if strings.Contains(project, " ") {
return fmt.Errorf("project name cannot contain spaces")
}
return nil
}).
Inline(true),
huh.NewMultiSelect[string]().
Options(huh.NewOptions(tagOptions...)...).
Title("Tags").
Value(&p.task.Tags),
huh.NewInput().
Title("Tags").
Value(&p.additionalTags).
Inline(true),
huh.NewInput().
Title("Due").
Value(&p.task.Due).
@ -112,10 +135,13 @@ func NewTaskEditorPage(common *common.Common, task taskwarrior.Task) *TaskEditor
WithShowHelp(false).
WithShowErrors(true).
// use styles from common
WithHeight(30).
WithHeight(40).
WithWidth(50).
WithTheme(p.common.Styles.Form)
p.nFields = 6
p.currentField = 0
p.statusline = NewStatusLine(common, p.mode)
return p
@ -141,7 +167,6 @@ func (p *TaskEditorPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
p.mode = ModeInsert
}
}
switch p.mode {
case ModeNormal:
switch msg := msg.(type) {
@ -208,7 +233,19 @@ func (p *TaskEditorPage) View() string {
}
func (p *TaskEditorPage) updateTasksCmd() tea.Msg {
p.common.TW.AddTask(&p.task)
if p.task.Project == "(none)" {
p.task.Project = ""
}
if p.task.Priority == "(none)" {
p.task.Priority = ""
}
if p.additionalTags != "" {
p.task.Tags = append(p.task.Tags, strings.Split(p.additionalTags, " ")...)
}
if p.additionalProject != "" {
p.task.Project = p.additionalProject
}
p.common.TW.ImportTask(&p.task)
return UpdatedTasksMsg{}
}