From 55e174e3b1130b9df346c5733b01707c31d0dee3 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 24 Mar 2022 14:43:45 -0600 Subject: [PATCH] root moves fixed future schedules to shceduled on automove --- pttodo/root.go | 15 ++++++----- pttodo/schedule.go | 36 ++++++++++++++++++++++++++ pttodo/schedule_test.go | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 7 deletions(-) diff --git a/pttodo/root.go b/pttodo/root.go index 0db9de7..6886111 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -22,18 +22,19 @@ func (root Root) Equals(other Root) bool { func (root *Root) AutoMove() { root.MoveScheduledToTodo() - //root.MoveTodoToScheduled() + root.MoveTodoToScheduled() } func (root *Root) MoveTodoToScheduled() { for i := len(root.Todo) - 1; i >= 0; i-- { - if root.Todo[i].Schedule != "" && !root.Todo[i].Triggered() { - root.Scheduled = append(root.Scheduled, root.Todo[i]) - for j := i; j < len(root.Todo)-1; j++ { - root.Todo[j] = root.Todo[j+1] - } - root.Todo = root.Todo[:len(root.Todo)-1] + if !root.Todo[i].Schedule.isFixedFuture() { + continue } + root.Scheduled = append(root.Scheduled, root.Todo[i]) + for j := i; j < len(root.Todo)-1; j++ { + root.Todo[j] = root.Todo[j+1] + } + root.Todo = root.Todo[:len(root.Todo)-1] } } diff --git a/pttodo/schedule.go b/pttodo/schedule.go index 0c3b739..207626d 100644 --- a/pttodo/schedule.go +++ b/pttodo/schedule.go @@ -25,6 +25,42 @@ func (schedule Schedule) Next(t time.Time) (time.Time, error) { return scheduler.next(t) } +func (schedule Schedule) isFixedFuture() bool { + if !schedule.isFixed() { + return false + } + return schedule.isFuture() +} + +func (schedule Schedule) isFixed() bool { + if schedule.empty() { + return false + } + scheduler := schedulerFactory(string(schedule)) + switch scheduler.(type) { + case scheduleEZDate, scheduleDue: + default: + return false + } + return true +} + +func (schedule Schedule) isFuture() bool { + if schedule.empty() { + return false + } + scheduler := schedulerFactory(string(schedule)) + t, err := scheduler.next(time.Now()) + if err != nil { + return false + } + return time.Now().Before(t) +} + +func (schedule Schedule) empty() bool { + return string(schedule) == "" +} + type scheduler interface { next(time.Time) (time.Time, error) } diff --git a/pttodo/schedule_test.go b/pttodo/schedule_test.go index 5482205..a60a082 100644 --- a/pttodo/schedule_test.go +++ b/pttodo/schedule_test.go @@ -130,3 +130,60 @@ func TestSchedulerFactory(t *testing.T) { } }) } + +func TestScheduleIsFixedFuture(t *testing.T) { + cases := map[string]struct { + input string + want bool + }{ + "empty": { + input: "", + want: false, + }, + "cron": { + input: "* * * * *", + want: false, + }, + "duration": { + input: "1m", + want: false, + }, + "due past": { + input: "123", + want: false, + }, + "due future": { + input: "9648154541", + want: true, + }, + "ez date short past": { + input: "2000-01-02", + want: false, + }, + "ez date short future": { + input: "2100-01-02", + want: true, + }, + "ez date long past": { + input: "2000-01-02T01", + want: false, + }, + "ez date long future": { + input: "2100-01-02T02", + want: true, + }, + } + + for name, d := range cases { + c := d + t.Run(name, func(t *testing.T) { + schedule := Schedule(c.input) + got := schedule.isFixedFuture() + if got != c.want { + gotFuture := schedule.isFuture() + gotFixed := schedule.isFixed() + t.Errorf("want %v, got %v = %v && %v", c.want, got, gotFuture, gotFixed) + } + }) + } +}