-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodolist.go
103 lines (84 loc) · 2.21 KB
/
todolist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// todolist finds lines starting with `- [ ]` in `.md` files.
// It expects a `todolist` environment variable to tell it where to look for
// files.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
func searchForMatchesByLine(tagRegex regexp.Regexp, fileName string) []string {
file, err := os.Open(fileName)
if err != nil {
fmt.Printf("Unable to read file: %s - %s", fileName, err)
}
scanner := bufio.NewScanner(file)
var results = []string{}
var result = []string{}
for scanner.Scan() {
result = tagRegex.FindAllString(scanner.Text(), -1)
results = append(results, result...)
}
return results
}
func main() {
todoFile := flag.String("file", "", "Only search this file for todo lines.")
doneFlag := flag.Bool("done", false, "List any done lines.")
countFlag := flag.Bool("count", false, "Count todo items, rather than list.")
// maxFlag := flag.Int("max", 5, "Maximum number of files or tags to list.")
flag.Parse()
todoRegex, _ := regexp.Compile(`(^[\-\+]\W\[\W+\].+$)`) // How we find todos
doneRegex, _ := regexp.Compile(`(^[\-\+]\W\[x\].+$)`) // How we find done items.
if *doneFlag {
todoRegex = doneRegex
}
var rootPath string = os.Getenv(`todolist`)
var notMarkdown int = 0
var matchCount int = 0
// fmt.Printf("List: %t, Find: %s.", *listFlag, *searchFlag)
// fmt.Printf("List: %t, Find: %s.", "--default to list--", *searchFlag)
// fmt.Println("")
if *todoFile != "" {
rootPath = *todoFile
}
walkErr := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) != ".md" {
notMarkdown += 1
return nil
}
var found = []string{}
found = searchForMatchesByLine(*todoRegex, path)
var todo string
if len(found) > 0 && ! *countFlag {
fmt.Println("")
fmt.Println("## ", path)
}
for _, todo = range found {
if *countFlag {
matchCount += 1
} else {
fmt.Println(todo)
}
}
/*
if found {
// fmt.Printf("Found a match in: %s", path)
matchCount += 1
results = append(results, path)
} else {
// fmt.Printf("Found no match in: %s", path)
}
*/
return nil
})
if *countFlag {
fmt.Printf("%d",matchCount)
}
if walkErr != nil {
log.Fatal(walkErr)
}
}