support tag, simple case insensitve search when recursing

Bel LaPointe 2021-12-31 23:20:27 -05:00
parent 031db8788b
commit af0f094a65
2 changed files with 37 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"strings"
"syscall" "syscall"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -28,6 +29,8 @@ func _main() error {
defaultFilepath = "-" defaultFilepath = "-"
} }
filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file") filepath := flag.String("f", defaultFilepath, "($PTTODO_FILE) path to yaml file")
tags := flag.String("tags", "", "csv of all tags to find")
search := flag.String("search", "", "fts case insensitive")
e := flag.Bool("e", false, "edit file") e := flag.Bool("e", false, "edit file")
dry := flag.Bool("dry", false, "dry run") dry := flag.Bool("dry", false, "dry run")
flag.Parse() flag.Parse()
@ -36,7 +39,11 @@ func _main() error {
return err return err
} }
} }
return dump(*dry, os.Stdout, *filepath) var tagslist []string
if *tags != "" {
tagslist = strings.Split(*tags, ",")
}
return dump(*dry, os.Stdout, *filepath, flag.Arg(0), tagslist, *search)
} }
func edit(dry bool, filepath string) error { func edit(dry bool, filepath string) error {
@ -100,7 +107,7 @@ func edit(dry bool, filepath string) error {
return nil return nil
} }
verify := func() error { verify := func() error {
if err := dump(true, io.Discard, tempFile); err != nil { if err := dump(true, io.Discard, tempFile, "", nil, ""); err != nil {
return fmt.Errorf("failed to verify %s: %v", tempFile, err) return fmt.Errorf("failed to verify %s: %v", tempFile, err)
} }
return nil return nil
@ -125,7 +132,7 @@ func edit(dry bool, filepath string) error {
return nil return nil
} }
func dump(dry bool, writer io.Writer, filepath string) error { func dump(dry bool, writer io.Writer, filepath, recurse string, tags []string, search string) error {
var reader io.Reader var reader io.Reader
if filepath == "-" { if filepath == "-" {
reader = os.Stdin reader = os.Stdin
@ -149,7 +156,7 @@ func dump(dry bool, writer io.Writer, filepath string) error {
root.MoveScheduledToTodo() root.MoveScheduledToTodo()
var v interface{} = root var v interface{} = root
switch flag.Arg(0) { switch recurse {
case "": case "":
case "todo": case "todo":
v = root.Todo v = root.Todo
@ -158,6 +165,31 @@ func dump(dry bool, writer io.Writer, filepath string) error {
case "done": case "done":
v = root.Done v = root.Done
} }
if todos, ok := v.([]pttodo.Todo); ok {
if len(tags) > 0 {
result := make([]pttodo.Todo, 0, len(todos))
for _, todo := range todos {
want := len(todo.Tags) > 0
for _, tag := range tags {
want = want && strings.Contains(todo.Tags, tag)
}
if want {
result = append(result, todo)
}
}
todos = result
}
if len(search) > 0 {
result := make([]pttodo.Todo, 0, len(todos))
for _, todo := range todos {
if strings.Contains(strings.ToLower(fmt.Sprint(todo)), strings.ToLower(search)) {
result = append(result, todo)
}
}
todos = result
}
v = todos
}
b2, err := yaml.Marshal(v) b2, err := yaml.Marshal(v)
if err != nil { if err != nil {
return err return err

View File

@ -7,6 +7,7 @@ todo:
details: | details: |
- if it's a web ui hook or somethin, then it'd only have file conflict if I modify without waiting - 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 - but thats a step back from current todo solution
tags: stuffToTry,secondTag
- todo: ez edit on many platforms, even offline and mobile - todo: ez edit on many platforms, even offline and mobile
ts: Fri Dec 31 22:33:12 EST 2021 ts: Fri Dec 31 22:33:12 EST 2021
details: | details: |