planner/plan/command/list.go

121 lines
2.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"
"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 {
2024-12-29 09:32:49 +01:00
params storage.TaskListParams
2024-09-30 07:34:40 +02:00
}
2024-12-27 11:20:32 +01:00
func NewListArgs() ListArgs {
return ListArgs{}
2024-09-30 07:34:40 +02:00
}
2024-12-27 11:20:32 +01:00
func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
2024-12-29 09:32:49 +01:00
if len(main) > 2 {
return nil, ErrWrongCommand
}
now := time.Now()
today := item.NewDate(now.Year(), int(now.Month()), now.Day())
tomorrow := item.NewDate(now.Year(), int(now.Month()), now.Day()+1)
var date item.Date
var includeBefore, recurrer bool
switch len(main) {
case 0:
date = today
includeBefore = true
case 1:
switch {
case slices.Contains([]string{"today", "tod"}, main[0]):
date = today
includeBefore = true
case slices.Contains([]string{"tomorrow", "tom"}, main[0]):
date = tomorrow
case main[0] == "list":
default:
return nil, ErrWrongCommand
}
case 2:
if main[0] == "list" && main[1] == "recur" {
recurrer = true
} else {
return nil, ErrWrongCommand
}
default:
2024-12-27 11:20:32 +01:00
return nil, ErrWrongCommand
2024-10-29 07:22:04 +01:00
}
2024-12-29 09:32:49 +01:00
return &List{
args: ListArgs{
params: storage.TaskListParams{
Date: date,
IncludeBefore: includeBefore,
Recurrer: recurrer,
},
},
}, nil
2024-12-27 11:20:32 +01:00
}
type List struct {
2024-12-29 09:32:49 +01:00
args ListArgs
2024-10-29 07:22:04 +01:00
}
2024-12-29 09:32:49 +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
}
2024-12-29 09:32:49 +01:00
all, err := deps.TaskRepo.FindMany(list.args.params)
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 {
2024-12-31 08:37:00 +01:00
data := [][]string{{"id", "project", "date", "dur", "title"}}
2024-12-29 09:32:49 +01:00
for _, tl := range lr.Tasks {
2024-12-31 08:37:00 +01:00
data = append(data, []string{
fmt.Sprintf("%d", tl.LocalID),
tl.Task.Project,
tl.Task.Date.String(),
tl.Task.Duration.String(),
tl.Task.Title,
})
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
}