diff --git a/dist/plan b/dist/plan index 40b05d3..a22e835 100755 Binary files a/dist/plan and b/dist/plan differ diff --git a/plan/command/list.go b/plan/command/list.go index bc741a9..3c6f8e9 100644 --- a/plan/command/list.go +++ b/plan/command/list.go @@ -11,23 +11,33 @@ import ( ) type ListArgs struct { - params storage.TaskListParams + fieldTPL map[string][]string + params storage.TaskListParams } func NewListArgs() ListArgs { - return ListArgs{} + return ListArgs{ + fieldTPL: map[string][]string{ + "project": {"p", "project"}, + }, + } } -func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) { +func (la ListArgs) Parse(main []string, fields map[string]string) (Command, error) { if len(main) > 2 { return nil, ErrWrongCommand } + fields, err := ResolveFields(fields, la.fieldTPL) + if err != nil { + return nil, err + } 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 + var project string switch len(main) { case 0: @@ -40,7 +50,10 @@ func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error includeBefore = true case slices.Contains([]string{"tomorrow", "tom"}, main[0]): date = tomorrow - case main[0] == "list": + case main[0] == "list" || main[0] == "l": + if val, ok := fields["project"]; ok { + project = val + } default: return nil, ErrWrongCommand } @@ -60,6 +73,7 @@ func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error Date: date, IncludeBefore: includeBefore, Recurrer: recurrer, + Project: project, }, }, }, nil diff --git a/plan/storage/sqlite/task.go b/plan/storage/sqlite/task.go index c7892f4..b7250d3 100644 --- a/plan/storage/sqlite/task.go +++ b/plan/storage/sqlite/task.go @@ -80,6 +80,10 @@ func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error where = append(where, `date <= ?`) args = append(args, params.Date.String()) } + if params.Project != "" { + where = append(where, `project = ?`) + args = append(args, params.Project) + } if len(where) > 0 { query += ` WHERE ` + where[0] diff --git a/plan/storage/storage.go b/plan/storage/storage.go index 8634ffd..caec98b 100644 --- a/plan/storage/storage.go +++ b/plan/storage/storage.go @@ -32,6 +32,7 @@ type TaskListParams struct { Recurrer bool Date item.Date IncludeBefore bool + Project string } type Task interface { @@ -53,6 +54,9 @@ func Match(tsk item.Task, params TaskListParams) bool { return false } } + if params.Project != "" && params.Project != tsk.Project { + return false + } return true } diff --git a/plan/storage/storage_test.go b/plan/storage/storage_test.go index 937e7b9..54bf19e 100644 --- a/plan/storage/storage_test.go +++ b/plan/storage/storage_test.go @@ -15,14 +15,16 @@ func TestMatch(t *testing.T) { Date: item.NewDate(2024, 12, 29), Recurrer: item.NewRecurrer("2024-12-29, daily"), TaskBody: item.TaskBody{ - Title: "name", + Title: "name", + Project: "p1", }, } tskNotMatch := item.Task{ ID: "id", Date: item.NewDate(2024, 12, 28), TaskBody: item.TaskBody{ - Title: "name", + Title: "name", + Project: "p2", }, } @@ -42,6 +44,12 @@ func TestMatch(t *testing.T) { Recurrer: true, }, }, + { + name: "project", + params: storage.TaskListParams{ + Project: "p1", + }, + }, } { t.Run(tc.name, func(t *testing.T) { if !storage.Match(tskMatch, tc.params) {