Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
976f5bf073 | ||
|
|
ac3dc241f1 |
@@ -27,13 +27,15 @@ const (
|
||||
)
|
||||
|
||||
type config struct {
|
||||
targets []string
|
||||
mergeme string
|
||||
root string
|
||||
tags []string
|
||||
search string
|
||||
edit bool
|
||||
dry bool
|
||||
targets []string
|
||||
mergeme string
|
||||
root string
|
||||
tags []string
|
||||
search string
|
||||
edit bool
|
||||
dry bool
|
||||
add string
|
||||
addSchedule string
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -49,6 +51,14 @@ func _main() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if config.add != "" {
|
||||
if err := add(config.dry, config.targets, pttodo.Todo{
|
||||
Todo: config.add,
|
||||
Schedule: pttodo.Schedule(config.addSchedule),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if config.edit {
|
||||
if err := edit(config.dry, config.targets); err != nil {
|
||||
return err
|
||||
@@ -73,6 +83,8 @@ func getConfig() config {
|
||||
flag.StringVar(&config.search, "search", "", "fts case insensitive")
|
||||
flag.BoolVar(&config.edit, "e", false, "edit file")
|
||||
flag.BoolVar(&config.dry, "dry", false, "dry run")
|
||||
flag.StringVar(&config.add, "add", "", "todo to add")
|
||||
flag.StringVar(&config.addSchedule, "add-schedule", "", "todo to add schedule")
|
||||
flag.Parse()
|
||||
|
||||
config.tags = strings.Split(tagss, ",")
|
||||
@@ -109,6 +121,34 @@ func verifyFile(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func add(dry bool, filepaths []string, todo pttodo.Todo) error {
|
||||
target := filepaths[0]
|
||||
var original pttodo.Root
|
||||
|
||||
b, err := ioutil.ReadFile(target)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(b, &original); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
original.Todo = append([]pttodo.Todo{todo}, original.Todo...)
|
||||
original.AutoMove()
|
||||
|
||||
c, err := yaml.Marshal(original)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if dry {
|
||||
log.Printf("wouldve written...\n%s", c)
|
||||
return nil
|
||||
} else if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func edit(dry bool, filepaths []string) error {
|
||||
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo-*")
|
||||
if err != nil {
|
||||
|
||||
BIN
cmd/pttodo-cli/pttodo-cli
Executable file
BIN
cmd/pttodo-cli/pttodo-cli
Executable file
Binary file not shown.
49
cmd/pttodo-cli/testdata/test.yaml
vendored
49
cmd/pttodo-cli/testdata/test.yaml
vendored
@@ -1,45 +1,10 @@
|
||||
todo:
|
||||
- todo: b
|
||||
ts: Thu Mar 24 09:03:24 MDT 2022
|
||||
scheduled:
|
||||
- todo: due
|
||||
schedule: "2022-05-01"
|
||||
ts: Thu Mar 24 09:16:21 MDT 2022
|
||||
- hi20890
|
||||
- hi
|
||||
- b
|
||||
- todo: loop
|
||||
schedule: 10s
|
||||
ts: Thu Mar 24 09:17:11 MDT 2022
|
||||
done:
|
||||
- todo: abc
|
||||
schedule: "2022-05-01"
|
||||
ts: Wed Mar 23 08:03:45 MDT 2022
|
||||
- todo: def
|
||||
schedule: "2022-05-01"
|
||||
ts: Wed Mar 23 08:04:05 MDT 2022
|
||||
- todo: other
|
||||
ts: Wed Mar 23 09:44:57 MDT 2022
|
||||
- todo: b
|
||||
ts: Wed Mar 23 09:45:13 MDT 2022
|
||||
- todo: a
|
||||
ts: Wed Mar 23 09:45:15 MDT 2022
|
||||
- todo: a
|
||||
ts: Wed Mar 23 09:45:24 MDT 2022
|
||||
- todo: a
|
||||
ts: Wed Mar 23 09:45:31 MDT 2022
|
||||
- todo: looper
|
||||
schedule: 15s
|
||||
ts: Thu Mar 24 09:01:41 MDT 2022
|
||||
- todo: looper
|
||||
schedule: 15s
|
||||
ts: Thu Mar 24 09:04:51 MDT 2022
|
||||
- todo: loop
|
||||
schedule: 10s
|
||||
ts: Thu Mar 24 09:06:59 MDT 2022
|
||||
- todo: due
|
||||
schedule: "2022-02-01"
|
||||
ts: Thu Mar 24 09:15:42 MDT 2022
|
||||
- todo: due
|
||||
schedule: "2022-02-01"
|
||||
ts: Thu Mar 24 09:16:53 MDT 2022
|
||||
- todo: due
|
||||
schedule: "2022-03-01"
|
||||
ts: Thu Mar 24 09:17:07 MDT 2022
|
||||
- todo: past
|
||||
schedule: "2000-01-02"
|
||||
scheduled: []
|
||||
done: []
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user