first rough version of searching notes on the commandline

This commit is contained in:
Erik Winter 2021-01-10 10:32:51 +01:00
parent c6f8c21f38
commit 348f4ab085
18 changed files with 192 additions and 10 deletions

71
cmd/note-cli/main.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"git.sr.ht/~ewintr/shitty-ssg/cmd/note-cli/note"
)
func main() {
notesPath := os.Getenv("NOTES_PATH")
if notesPath == "" {
log.Fatal("no notes directory to parse")
}
if len(os.Args) != 2 {
log.Fatal("exactly one search term is required as parameter")
}
searchTerm := os.Args[1]
var notes note.Notes
if err := filepath.Walk(notesPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) == ".adoc" {
if err := notes.AddFileNote(path); err != nil {
return nil
}
}
return nil
}); err != nil {
log.Fatal(err)
}
notes = notes.FilterByTerm(searchTerm)
if len(notes) == 0 {
fmt.Println("Found nothing.")
return
}
for i, n := range notes {
fmt.Printf("%d) %s\n", i, n.Title)
}
reader := bufio.NewReader(os.Stdin)
r, _, err := reader.ReadRune()
if err != nil {
log.Fatal(err)
}
c := string(r)
switch c {
case "q":
return
default:
i, err := strconv.Atoi(c)
if err != nil {
log.Fatal(err)
}
if i < 0 || i >= len(notes) {
fmt.Println("number out of range")
return
}
fmt.Printf("\n\n%s\n\n%s\n\n", notes[i].Title, notes[i].Content)
}
}

76
cmd/note-cli/note/note.go Normal file
View File

@ -0,0 +1,76 @@
package note
import (
"strings"
"git.sr.ht/~ewintr/shitty-ssg/pkg/adoc"
)
type Kind string
type Tag string
const (
KIND_NOTE = Kind("note")
KIND_PRIVATE_NOTE = Kind("private_note")
KIND_WORK_NOTE = Kind("work_note")
KIND_INVALID = Kind("")
)
func mapKind(akind adoc.Kind) Kind {
nkind, ok := map[adoc.Kind]Kind{
adoc.KIND_NOTE: KIND_NOTE,
adoc.KIND_PRIVATE_NOTE: KIND_PRIVATE_NOTE,
adoc.KIND_WORK_NOTE: KIND_WORK_NOTE,
}[akind]
if !ok {
return KIND_INVALID
}
return nkind
}
type Note struct {
doc *adoc.ADoc
Title string
Kind Kind
Tags []Tag
Content string
}
func NewNote(doc *adoc.ADoc) *Note {
var paragraphs []string
for _, be := range doc.Content {
paragraphs = append(paragraphs, be.Text())
}
content := strings.Join(paragraphs, "\n\n")
var tags []Tag
for _, t := range doc.Tags {
tags = append(tags, Tag(t))
}
return &Note{
doc: doc,
Kind: mapKind(doc.Kind),
Title: doc.Title,
Tags: tags,
Content: content,
}
}
func (n *Note) Contains(term string) bool {
for _, t := range n.Tags {
if strings.ToLower(string(t)) == strings.ToLower(term) {
return true
}
}
for _, w := range strings.Split(n.Content, " ") {
if strings.ToLower(w) == strings.ToLower(term) {
return true
}
}
return false
}

View File

@ -0,0 +1,33 @@
package note
import (
"io/ioutil"
"git.sr.ht/~ewintr/shitty-ssg/pkg/adoc"
)
type Notes []*Note
func (n *Notes) AddFileNote(fPath string) error {
content, err := ioutil.ReadFile(fPath)
if err != nil {
return err
}
note := NewNote(adoc.New(string(content)))
if note.Kind != KIND_INVALID {
*n = append(*n, note)
}
return nil
}
func (n *Notes) FilterByTerm(term string) Notes {
found := Notes{}
for _, note := range *n {
if note.Contains(term) {
found = append(found, note)
}
}
return found
}

View File

@ -7,11 +7,12 @@ import (
const (
KIND_NOTE = Kind("note")
KIND_PRIVATE_NOTE = Kind("private_note")
KIND_WORK_NOTE = Kind("work_note")
KIND_VKV = Kind("vkv")
KIND_STORY = Kind("story")
KIND_SNIPPET = Kind("snippet")
KIND_ESSAY = Kind("essay")
KIND_WORK = Kind("work")
KIND_ARTICLE = Kind("article")
KIND_TUTORIAL = Kind("tutorial")
KIND_UNKNOWN = Kind("unknown")
@ -28,7 +29,8 @@ func NewKind(text string) Kind {
}
for _, k := range []string{
"note", "vkv", "story", "snippet",
"note", "private_note", "work_note", "vkv",
"story", "snippet",
"essay", "tutorial", "work", "article",
} {
if k == text {