planner/plan/command/list.go

203 lines
4.3 KiB
Go
Raw Normal View History

2024-09-30 07:34:40 +02:00
package command
import (
"fmt"
2024-12-29 09:32:49 +01:00
"slices"
2025-01-05 10:52:41 +01:00
"sort"
2024-12-29 09:32:49 +01:00
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/format"
"go-mod.ewintr.nl/planner/plan/storage"
2024-09-30 07:34:40 +02:00
)
2024-12-27 11:20:32 +01:00
type ListArgs struct {
2025-01-04 11:03:57 +01:00
fieldTPL map[string][]string
HasRecurrer bool
From item.Date
To item.Date
Project string
2024-09-30 07:34:40 +02:00
}
2024-12-27 11:20:32 +01:00
func NewListArgs() ListArgs {
2024-12-31 09:22:03 +01:00
return ListArgs{
fieldTPL: map[string][]string{
2025-01-04 11:03:57 +01:00
"project": {"p", "project"},
"from": {"f", "from"},
"to": {"t", "to"},
"recurring": {"rec", "recurring"},
2024-12-31 09:22:03 +01:00
},
}
2024-09-30 07:34:40 +02:00
}
2024-12-31 09:22:03 +01:00
func (la ListArgs) Parse(main []string, fields map[string]string) (Command, error) {
if len(main) > 1 {
2024-12-29 09:32:49 +01:00
return nil, ErrWrongCommand
}
2024-12-31 09:22:03 +01:00
fields, err := ResolveFields(fields, la.fieldTPL)
if err != nil {
return nil, err
}
2025-01-04 11:03:57 +01:00
2024-12-29 09:32:49 +01:00
now := time.Now()
today := item.NewDate(now.Year(), int(now.Month()), now.Day())
switch len(main) {
case 0:
2025-01-05 10:34:58 +01:00
// fields["to"] = today.String()
2024-12-29 09:32:49 +01:00
case 1:
switch {
2025-01-04 11:03:57 +01:00
case slices.Contains([]string{"tod", "today"}, main[0]):
fields["to"] = today.String()
case slices.Contains([]string{"tom", "tomorrow"}, main[0]):
fields["from"] = today.Add(1).String()
fields["to"] = today.Add(1).String()
case main[0] == "week":
fields["from"] = today.String()
fields["to"] = today.Add(7).String()
case main[0] == "recur":
2025-01-05 09:58:15 +01:00
fields["recurrer"] = "true"
// case main[0] == "list":
// fields["from"] = today.String()
// fields["to"] = today.String()
2024-12-29 09:32:49 +01:00
default:
return nil, ErrWrongCommand
}
2024-10-29 07:22:04 +01:00
}
2025-01-04 11:03:57 +01:00
var fromDate, toDate item.Date
var hasRecurrer bool
var project string
if val, ok := fields["from"]; ok {
fromDate = item.NewDateFromString(val)
}
if val, ok := fields["to"]; ok {
toDate = item.NewDateFromString(val)
}
if val, ok := fields["recurrer"]; ok && val == "true" {
hasRecurrer = true
}
if val, ok := fields["project"]; ok {
project = val
}
return List{
Args: ListArgs{
HasRecurrer: hasRecurrer,
From: fromDate,
To: toDate,
Project: project,
2024-12-29 09:32:49 +01:00
},
}, nil
2024-12-27 11:20:32 +01:00
}
type List struct {
2025-01-04 11:03:57 +01:00
Args ListArgs
2024-10-29 07:22:04 +01:00
}
2025-01-04 11:03:57 +01:00
func (list List) Do(deps Dependencies) (CommandResult, error) {
2024-12-27 11:20:32 +01:00
localIDs, err := deps.LocalIDRepo.FindAll()
2024-10-03 07:32:48 +02:00
if err != nil {
2024-12-29 09:32:49 +01:00
return nil, fmt.Errorf("could not get local ids: %v", err)
2024-10-03 07:32:48 +02:00
}
2025-01-04 11:03:57 +01:00
all, err := deps.TaskRepo.FindMany(storage.TaskListParams{
HasRecurrer: list.Args.HasRecurrer,
From: list.Args.From,
To: list.Args.To,
Project: list.Args.Project,
})
2024-10-03 07:32:48 +02:00
if err != nil {
2024-12-29 09:32:49 +01:00
return nil, err
2024-10-03 07:32:48 +02:00
}
2024-12-29 09:32:49 +01:00
res := make([]TaskWithLID, 0, len(all))
for _, tsk := range all {
lid, ok := localIDs[tsk.ID]
2024-10-03 07:32:48 +02:00
if !ok {
2024-12-29 09:32:49 +01:00
return nil, fmt.Errorf("could not find local id for %s", tsk.ID)
2024-09-30 07:34:40 +02:00
}
2024-12-29 09:32:49 +01:00
res = append(res, TaskWithLID{
LocalID: lid,
Task: tsk,
})
}
return ListResult{
Tasks: res,
}, nil
}
type TaskWithLID struct {
LocalID int
Task item.Task
}
type ListResult struct {
Tasks []TaskWithLID
}
func (lr ListResult) Render() string {
2025-01-05 10:52:41 +01:00
sort.Slice(lr.Tasks, func(i, j int) bool {
if lr.Tasks[i].Task.Project < lr.Tasks[j].Task.Project {
return true
}
if lr.Tasks[i].Task.Project > lr.Tasks[j].Task.Project {
return false
}
if lr.Tasks[i].Task.Recurrer == nil && lr.Tasks[j].Task.Recurrer != nil {
return true
}
if lr.Tasks[i].Task.Recurrer != nil && lr.Tasks[j].Task.Recurrer == nil {
return false
}
if lr.Tasks[i].Task.Date.After(lr.Tasks[j].Task.Date) {
return false
}
return lr.Tasks[i].LocalID < lr.Tasks[j].LocalID
})
2025-01-05 08:29:10 +01:00
var showRec, showDur bool
2024-12-29 09:32:49 +01:00
for _, tl := range lr.Tasks {
2025-01-05 08:29:10 +01:00
if tl.Task.Recurrer != nil {
showRec = true
}
if tl.Task.Duration > time.Duration(0) {
showDur = true
}
}
title := []string{"id"}
if showRec {
title = append(title, "rec")
}
title = append(title, "project", "date")
if showDur {
title = append(title, "dur")
}
title = append(title, "title")
data := [][]string{title}
for _, tl := range lr.Tasks {
row := []string{fmt.Sprintf("%d", tl.LocalID)}
if showRec {
recStr := ""
if tl.Task.Recurrer != nil {
recStr = "*"
}
row = append(row, recStr)
}
row = append(row, tl.Task.Project, tl.Task.Date.String())
if showDur {
durStr := ""
if tl.Task.Duration > time.Duration(0) {
durStr = tl.Task.Duration.String()
}
row = append(row, durStr)
}
row = append(row, tl.Task.Title)
data = append(data, row)
2024-09-30 07:34:40 +02:00
}
2024-12-29 09:32:49 +01:00
return fmt.Sprintf("\n%s\n", format.Table(data))
2024-09-30 07:34:40 +02:00
}