diff --git a/plan/command/list.go b/plan/command/list.go index 1251ead..a616223 100644 --- a/plan/command/list.go +++ b/plan/command/list.go @@ -2,11 +2,15 @@ package command import ( "fmt" + "slices" + "time" + "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/storage" ) type ListArgs struct { + params storage.TaskListParams } func NewListArgs() ListArgs { @@ -14,14 +18,49 @@ func NewListArgs() ListArgs { } func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) { - if len(main) > 0 && main[0] != "list" { + if len(main) > 1 { return nil, ErrWrongCommand } - return &List{}, nil + 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": + } + case 2: + if main[0] != "list" || main[1] != "recur" { + return nil, ErrWrongCommand + } + recurrer = true + } + + return &List{ + args: ListArgs{ + params: storage.TaskListParams{ + Date: date, + IncludeBefore: includeBefore, + Recurrer: recurrer, + }, + }, + }, nil } type List struct { + args ListArgs } func (list *List) Do(deps Dependencies) ([][]string, error) { @@ -29,17 +68,18 @@ func (list *List) Do(deps Dependencies) ([][]string, error) { if err != nil { return nil, fmt.Errorf("could not get local ids: %v", err) } - all, err := deps.TaskRepo.FindMany(storage.TaskListParams{}) + all, err := deps.TaskRepo.FindMany(list.args.params) if err != nil { return nil, err } + data := [][]string{{"id", "date", "dur", "title"}} for _, e := range all { lid, ok := localIDs[e.ID] if !ok { return nil, fmt.Errorf("could not find local id for %s", e.ID) } - fmt.Printf("%s\t%d\t%s\t%s\t%s\n", e.ID, lid, e.Title, e.Date.String(), e.Duration.String()) + data = append(data, []string{fmt.Sprintf("%d", lid), e.Date.String(), e.Duration.String(), e.Title}) } - return nil, nil + return data, nil } diff --git a/plan/storage/memory/task_test.go b/plan/storage/memory/task_test.go index 03d8f7b..7fac228 100644 --- a/plan/storage/memory/task_test.go +++ b/plan/storage/memory/task_test.go @@ -23,7 +23,8 @@ func TestTask(t *testing.T) { t.Log("store") tsk1 := item.Task{ - ID: "id-1", + ID: "id-1", + Date: item.NewDate(2024, 12, 29), } if err := mem.Store(tsk1); err != nil { t.Errorf("exp nil, got %v", err) @@ -53,4 +54,16 @@ func TestTask(t *testing.T) { if diff := item.TaskDiffs([]item.Task{tsk1, tsk2}, actTasks); diff != "" { t.Errorf("(exp -, got +)\n%s", diff) } + + t.Log("fond some") + actTasks, actErr = mem.FindMany(storage.TaskListParams{ + Date: item.NewDate(2024, 12, 29), + }) + if actErr != nil { + t.Errorf("exp nil, got %v", actErr) + } + if diff := item.TaskDiffs([]item.Task{tsk1}, actTasks); diff != "" { + t.Errorf("(exp -, got +)\n%s", diff) + } + } diff --git a/plan/storage/sqlite/task.go b/plan/storage/sqlite/task.go index a323ade..ecf9a79 100644 --- a/plan/storage/sqlite/task.go +++ b/plan/storage/sqlite/task.go @@ -71,10 +71,14 @@ func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error if params.Recurrer { where = append(where, `recurrer IS NOT NULL AND recurrer != ''`) } - if !params.Date.IsZero() { + if !params.Date.IsZero() && !params.IncludeBefore { where = append(where, `date = ?`) args = append(args, params.Date.String()) } + if !params.Date.IsZero() && params.IncludeBefore { + where = append(where, `date <= ?`) + args = append(args, params.Date.String()) + } if len(where) > 0 { query += ` WHERE ` + where[0] diff --git a/plan/storage/storage.go b/plan/storage/storage.go index a517be6..8634ffd 100644 --- a/plan/storage/storage.go +++ b/plan/storage/storage.go @@ -29,8 +29,9 @@ type Sync interface { } type TaskListParams struct { - Recurrer bool - Date item.Date + Recurrer bool + Date item.Date + IncludeBefore bool } type Task interface { @@ -40,12 +41,17 @@ type Task interface { Delete(id string) error } -func Match(tsk item.Task, req TaskListParams) bool { - if req.Recurrer && tsk.Recurrer == nil { +func Match(tsk item.Task, params TaskListParams) bool { + if params.Recurrer && tsk.Recurrer == nil { return false } - if !req.Date.IsZero() && !req.Date.Equal(tsk.Date) { - return false + if !params.Date.IsZero() { + if !params.IncludeBefore && !params.Date.Equal(tsk.Date) { + return false + } + if params.IncludeBefore && tsk.Date.After(params.Date) { + return false + } } return true