2014-06-13 03:14:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-08-29 17:24:27 +00:00
|
|
|
var NoteDir string
|
|
|
|
var err error
|
|
|
|
|
2014-06-13 03:14:29 +00:00
|
|
|
if len(os.Getenv("NOTEDIR")) > 0 {
|
|
|
|
NoteDir, err = filepath.Abs(os.Getenv("NOTEDIR"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NoteDir = filepath.Join(os.Getenv("HOME"), "Notes")
|
|
|
|
}
|
2017-08-29 17:24:27 +00:00
|
|
|
|
|
|
|
flNoteDir := flag.String("d", NoteDir, "directory of notes")
|
|
|
|
flNoteFilePat := flag.String("p", "Tasks*.md", "file pattern")
|
|
|
|
flNoteTodoPat := flag.String("s", "TODO", "search for pattern in files")
|
2014-06-13 03:14:29 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-08-29 17:24:27 +00:00
|
|
|
matches, err := filepath.Glob(filepath.Join(*flNoteDir, *flNoteFilePat))
|
2014-06-13 03:14:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
fh, err := os.Open(match)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
rdr := bufio.NewReader(fh)
|
|
|
|
count := int64(0)
|
|
|
|
for {
|
|
|
|
line, err := rdr.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-08-24 19:43:33 +00:00
|
|
|
count++
|
2017-08-29 17:24:27 +00:00
|
|
|
if strings.Contains(line, *flNoteTodoPat) {
|
2014-06-13 03:14:29 +00:00
|
|
|
fmt.Printf("%s:%d\t%s\n", filepath.Base(match), count, strings.TrimRight(line, " \n"))
|
|
|
|
trimmed := strings.TrimSpace(line)
|
2017-08-29 17:24:27 +00:00
|
|
|
if strings.HasSuffix(trimmed, ":") || strings.HasSuffix(trimmed, *flNoteTodoPat) {
|
2014-06-13 03:14:29 +00:00
|
|
|
i := strings.IndexRune(line, rune(trimmed[0]))
|
|
|
|
for {
|
|
|
|
buf, err := rdr.Peek(i + 1)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if strings.Count(string(buf), " ") > 0 {
|
|
|
|
nextLine, err := rdr.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s:%d\t%s\n", filepath.Base(match), count, strings.TrimRight(nextLine, " \n"))
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fh.Close()
|
|
|
|
}
|
|
|
|
}
|