9 Commits

Author SHA1 Message Date
Bel LaPointe
50913b8913 change default file path from stdin 2022-01-10 09:44:30 -05:00
Bel LaPointe
42d532ee61 update todo 2022-01-09 10:27:30 -05:00
Bel LaPointe
24316fe690 accept THH on ezdate 2022-01-09 10:24:40 -05:00
Bel LaPointe
8058304219 dont log noops 2022-01-09 10:22:23 -05:00
Bel LaPointe
7f24b3f337 do not save if no effective changes for edit, dump 2022-01-09 10:18:08 -05:00
Bel LaPointe
fc12e0550d add root.equals and todo.equals 2022-01-09 10:17:44 -05:00
Bel LaPointe
05f5244cd1 root.yaml prio, otherwise sort -f results 2022-01-08 23:44:30 -05:00
Bel LaPointe
c559c8eba6 if cli -f is a dir, then edit each file+clean each file+keep each as individual file 2022-01-08 23:42:05 -05:00
Bel LaPointe
dd7ac8d786 parseinlocation for non utc crud 2022-01-06 21:28:44 -05:00
13 changed files with 264 additions and 63 deletions

View File

@@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path"
"sort"
"strings"
"syscall"
@@ -33,10 +34,10 @@ func main() {
func _main() error {
defaultFilepath, ok := os.LookupEnv("PTTODO_FILE")
if !ok {
defaultFilepath = "-"
defaultFilepath = "./todo.yaml"
}
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
filepathToMergeIn := flag.String("g", "", "path to yaml file to merge into -f")
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file or dir (starting with root then alphabetical for dir)")
filepathToMergeIn := flag.String("g", "", "path to yaml file to merge into -f (modifies f)")
root := flag.String("root", DUMP_TODO, "path to pretty print ("+fmt.Sprint([]string{DUMP_ALL, DUMP_TODO, DUMP_SCHEDULED, DUMP_DONE})+")")
tags := flag.String("tags", "", "csv of all tags to find, -tag to invert")
search := flag.String("search", "", "fts case insensitive")
@@ -48,8 +49,17 @@ func _main() error {
return err
}
}
filepaths := []string{*filepath}
if stat, err := os.Stat(*filepath); err != nil && !os.IsNotExist(err) {
return err
} else if err == nil && stat.IsDir() {
filepaths, err = listDir(*filepath)
if err != nil {
return err
}
}
if *e {
if err := edit(*dry, *filepath); err != nil {
if err := edit(*dry, filepaths); err != nil {
return err
}
}
@@ -57,7 +67,7 @@ func _main() error {
if *tags != "" {
tagslist = strings.Split(*tags, ",")
}
return dump(*dry, os.Stdout, *filepath, tagslist, *search, *root)
return dump(*dry, os.Stdout, filepaths, tagslist, *search, *root)
}
func verifyRoot(root pttodo.Root) error {
@@ -79,16 +89,19 @@ func verifyRoot(root pttodo.Root) error {
}
func verifyFile(path string) error {
if err := dump(true, io.Discard, path, nil, "", DUMP_ALL); err != nil {
if err := dump(true, io.Discard, []string{path}, nil, "", DUMP_ALL); err != nil {
return fmt.Errorf("failed verifying file %s: %w", path, err)
}
return nil
}
func edit(dry bool, filepath string) error {
var tempFile string
cp := func() error {
f, err := ioutil.TempFile(os.TempDir(), path.Base(filepath))
func edit(dry bool, filepaths []string) error {
tempDir, err := ioutil.TempDir(os.TempDir(), "edit-pttodo")
if err != nil {
return err
}
cpOne := func(filepath string) error {
f, err := os.Create(path.Join(tempDir, path.Base(filepath)))
if err != nil {
return err
}
@@ -103,14 +116,18 @@ func edit(dry bool, filepath string) error {
g.Close()
}
f.Close()
tempFile = f.Name() + ".yaml"
return os.Rename(f.Name(), tempFile)
return nil
}
cp := func() error {
for _, filepath := range filepaths {
if err := cpOne(filepath); err != nil {
return err
}
}
return nil
}
vi := func() error {
bin := "vim"
if editor := os.Getenv("EDITOR"); editor != "" {
bin = editor
}
editorbin, err := exec.LookPath(bin)
if err != nil {
editorbin, err = exec.LookPath("vi")
@@ -118,12 +135,17 @@ func edit(dry bool, filepath string) error {
if err != nil {
return err
}
args := []string{editorbin, tempFile}
args := []string{editorbin, "-p"}
tempfiles, err := listDir(tempDir)
if err != nil {
return err
}
args = append(args, tempfiles...)
cpid, err := syscall.ForkExec(
editorbin,
args,
&syscall.ProcAttr{
Dir: "",
Dir: tempDir,
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
Sys: nil,
@@ -145,7 +167,7 @@ func edit(dry bool, filepath string) error {
}
return nil
}
verify := func() error {
verifyOne := func(tempFile string) error {
for {
err := verifyFile(tempFile)
if err == nil {
@@ -162,13 +184,44 @@ func edit(dry bool, filepath string) error {
}
return verifyFile(tempFile)
}
save := func() error {
verify := func() error {
for _, filepath := range filepaths {
tempFile := path.Join(tempDir, path.Base(filepath))
if err := verifyOne(tempFile); err != nil {
return err
}
}
return nil
}
saveOne := func(filepath string) error {
tempFile := path.Join(tempDir, path.Base(filepath))
var rootTemp, rootOld pttodo.Root
if a, err := ioutil.ReadFile(tempFile); err != nil {
return err
} else if err := yaml.Unmarshal(a, &rootTemp); err != nil {
return err
} else if b, err := ioutil.ReadFile(filepath); err != nil {
return err
} else if err := yaml.Unmarshal(b, &rootOld); err != nil {
return err
} else if rootTemp.Equals(rootOld) {
//log.Printf("no changes to %s", filepath)
return nil
}
if dry {
log.Printf("would've saved %s as %s", tempFile, filepath)
return nil
}
return os.Rename(tempFile, filepath)
}
save := func() error {
for _, filepath := range filepaths {
if err := saveOne(filepath); err != nil {
return err
}
}
return nil
}
for _, foo := range []func() error{cp, vi, verify, save} {
if err := foo(); err != nil {
@@ -176,7 +229,7 @@ func edit(dry bool, filepath string) error {
}
}
if !dry {
os.Remove(tempFile)
os.RemoveAll(tempDir)
}
return nil
@@ -241,21 +294,48 @@ func marshalRootToTempFile(root pttodo.Root) (string, error) {
return filepath, err
}
func dump(dry bool, writer io.Writer, filepath string, tags []string, search, rootDisplay string) error {
reader, err := filePathReader(filepath)
if err != nil {
return err
}
b, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
func dump(dry bool, writer io.Writer, filepaths []string, tags []string, search, rootDisplay string) error {
var root pttodo.Root
if err := yaml.Unmarshal(b, &root); err != nil {
return err
for _, filepath := range filepaths {
reader, err := filePathReader(filepath)
if err != nil {
return err
}
b, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
var root2, root2post pttodo.Root
if err := yaml.Unmarshal(b, &root2); err != nil {
return err
}
if err := yaml.Unmarshal(b, &root2post); err != nil {
return err
}
root2.MoveScheduledToTodo()
if !dry {
if !root2.Equals(root2post) {
log.Printf("refreshing %s", filepath)
b3, err := yaml.Marshal(root2)
if err != nil {
return err
}
if err := os.WriteFile(filepath, b3, os.ModePerm); err != nil {
return err
}
} else {
//log.Printf("not refreshing %s", filepath)
}
}
root.MergeIn(root2)
}
root.MoveScheduledToTodo()
var v interface{} = root
@@ -296,21 +376,13 @@ func dump(dry bool, writer io.Writer, filepath string, tags []string, search, ro
}
v = todos
}
b2, err := yaml.Marshal(v)
if err != nil {
return err
}
fmt.Fprintf(writer, "%s\n", b2)
if dry {
return nil
}
b3, err := yaml.Marshal(root)
if err != nil {
return err
}
return os.WriteFile(filepath, b3, os.ModePerm)
return nil
}
func filePathReader(path string) (io.Reader, error) {
@@ -326,3 +398,24 @@ func filePathReader(path string) (io.Reader, error) {
}
return reader, nil
}
func listDir(dname string) ([]string, error) {
entries, err := os.ReadDir(dname)
if err != nil {
return nil, err
}
paths := make([]string, 0, len(entries))
for i := range entries {
if entries[i].IsDir() {
continue
}
paths = append(paths, path.Join(dname, entries[i].Name()))
}
sort.Slice(paths, func(i, j int) bool {
if path.Base(paths[i]) == "root.yaml" {
return true
}
return paths[i] < paths[j]
})
return paths, nil
}

View File

@@ -0,0 +1,29 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"testing"
)
func TestListDir(t *testing.T) {
d := t.TempDir()
want := []string{}
for i := 0; i < 3; i++ {
p := path.Join(d, strconv.Itoa(i))
ioutil.WriteFile(p, []byte{}, os.ModePerm)
want = append(want, p)
}
os.Mkdir(path.Join(d, "d"), os.ModePerm)
got, err := listDir(d)
if err != nil {
t.Fatal(err)
}
if fmt.Sprint(want) != fmt.Sprint(got) {
t.Fatal(want, got)
}
}

4
cmd/pttodo-cli/testdata/1.yaml vendored Normal file
View File

@@ -0,0 +1,4 @@
todo:
- "1"
scheduled: []
done: []

4
cmd/pttodo-cli/testdata/2.yaml vendored Normal file
View File

@@ -0,0 +1,4 @@
todo:
- "2"
scheduled: []
done: []

4
cmd/pttodo-cli/testdata/3.yaml vendored Normal file
View File

@@ -0,0 +1,4 @@
todo:
- "3"
scheduled: []
done: []

4
cmd/pttodo-cli/testdata/root.yaml vendored Normal file
View File

@@ -0,0 +1,4 @@
todo:
- root
scheduled: []
done: []

View File

@@ -6,6 +6,20 @@ type Root struct {
Done []Todo
}
func (root Root) Equals(other Root) bool {
for i, slice := range [][2][]Todo{
[2][]Todo{root.Todo, other.Todo},
[2][]Todo{root.Scheduled, other.Scheduled},
[2][]Todo{root.Done, other.Done},
} {
_ = i
if !equalTodoSlices(slice[0], slice[1]) {
return false
}
}
return true
}
func (root *Root) MoveScheduledToTodo() {
for i := len(root.Scheduled) - 1; i >= 0; i-- {
if root.Scheduled[i].Triggered() {

View File

@@ -24,6 +24,8 @@ func TestJSONRoot(t *testing.T) {
t.Fatal(err)
} else if fmt.Sprint(root) != fmt.Sprint(root2) {
t.Fatal(root, root2)
} else if !root.Equals(root2) {
t.Fatal(root2)
}
}
@@ -95,6 +97,8 @@ func TestRootMoveScheduledToTodo(t *testing.T) {
got.MoveScheduledToTodo()
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
} else if !got.Equals(want) {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, got)
}
})
}
@@ -144,6 +148,8 @@ todo:
root0.MergeIn(root1)
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
} else if !root0.Equals(rootWant) {
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
}
}

View File

@@ -94,8 +94,11 @@ func (due scheduleDue) next(time.Time) (time.Time, error) {
// 2022-01-01
type scheduleEZDate string
var scheduleEZDatePattern = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}$`)
var scheduleEZDatePattern = regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}(T[0-9]{2})?$`)
func (ezdate scheduleEZDate) next(time.Time) (time.Time, error) {
return time.Parse("2006-01-02", string(ezdate))
if len(ezdate) == len("20xx-xx-xxTxx") {
return time.ParseInLocation("2006-01-02T15", string(ezdate), time.Local)
}
return time.ParseInLocation("2006-01-02", string(ezdate), time.Local)
}

View File

@@ -51,11 +51,17 @@ func TestJSONSchedule(t *testing.T) {
func TestSchedulerFactory(t *testing.T) {
start := time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)
someDay := time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)
someHour := time.Date(2001, 2, 3, 15, 0, 0, 0, time.UTC)
cases := map[string]struct {
input string
want interface{}
next time.Time
}{
"ezdate with hour": {
input: `2000-01-03T15`,
want: scheduleEZDate(`2000-01-03T15`),
next: someHour,
},
"long dur": {
input: `2h1m`,
want: scheduleDuration("2h1m"),

View File

@@ -43,3 +43,37 @@ func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
alt := (*Alt)(todo)
return unmarshal(alt)
}
func (todo Todo) Equals(other Todo) bool {
if !equalTodoSlices(todo.Subtasks, other.Subtasks) {
return false
}
if todo.TS != other.TS {
return false
}
if todo.Tags != other.Tags {
return false
}
if todo.Schedule != other.Schedule {
return false
}
if todo.Details != other.Details {
return false
}
if todo.Todo != other.Todo {
return false
}
return true
}
func equalTodoSlices(a, b []Todo) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equals(b[i]) {
return false
}
}
return true
}

View File

@@ -44,7 +44,7 @@ func (ts *TS) UnmarshalYAML(unmarshaller func(interface{}) error) error {
}
var s string
if err := unmarshaller(&s); err == nil {
t, err := time.Parse(time.UnixDate, s)
t, err := time.ParseInLocation(time.UnixDate, s, time.Local)
*ts = TS(t.Unix())
return err
}

View File

@@ -1,20 +1,11 @@
todo:
- when merging, check for complete/incomplete and cross reference todo vs scheduled
vs done
- how to defer a scheduled task?
- merge multi todo files for 'inbox' style with some dedupe (probably best to just
do this via calling pttodo-cli from script or somethin) (probably at an off hour
so no collisions from live editing)
- click on links when dumped via cli
- what do about todo-now vs todo vs watch vs later... could do many files and manage
outside binary, but search would suck. Good would be vendor lockin, and that's UI
problem
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I
modify only file on remote
details: |
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
- but thats a step back from current todo solution
tags: stuffToTry,secondTag
scheduled: []
done:
- xactions emails extend ledger,todo-server with pttodo - schedule merge inbox style
- xactions emails extend ledger,todo-server with pttodo
- todo: ez edit on many platforms, even offline and mobile
details: |
mobile view + complete method
@@ -23,10 +14,19 @@ todo:
web server access to ops
is a web ui for mobile best solution?
let git be smart-ish and keep latest? would provide versioning OOTB without touching raw
- xactions emails extend ledger,todo-server with pttodo
- xactions emails extend ledger,todo-server with pttodo - schedule merge inbox style
scheduled: []
done:
- todo: when to run scheduled modifier? like, syncthing could have conflicts if I
modify only file on remote
details: |
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting
- but thats a step back from current todo solution
tags: stuffToTry,secondTag
- what do about todo-now vs todo vs watch vs later... could do many files and manage
outside binary, but search would suck. Good would be vendor lockin, and that's UI
problem
- merge multi todo files for 'inbox' style with some dedupe (probably best to just
do this via calling pttodo-cli from script or somethin) (probably at an off hour
so no collisions from live editing)
- how to defer a scheduled task?
- todo: add tag anti-search, like -tag=-notMe
ts: Tue Jan 4 06:56:03 EST 2022
- todo: if in todo, then omit ts field