first commit

main
Nick Dumas 1 year ago
commit 9dfe4ae070

@ -0,0 +1,3 @@
module code.ndumas.com/ndumas/vault-stats
go 1.19

@ -0,0 +1,66 @@
package main
import (
"bufio"
"flag"
"io/fs"
"log"
"os"
"path/filepath"
)
func main() {
var (
vaultDir string
)
flag.StringVar(&vaultDir, "vault", "./vault", "path to Obsidian vault")
flag.Parse()
absVaultPath, err := filepath.Abs(vaultDir)
if err != nil {
log.Fatalf("error generating absolute path: %s\n", err)
}
log.Println("absVaultPath:", absVaultPath)
vaultRoot := os.DirFS(absVaultPath)
noteLengthsChar := make(map[string]int)
noteLengthsWord := make(map[string]int)
fs.WalkDir(vaultRoot, ".", func(path string, d fs.DirEntry, err error) error {
absFilePath := filepath.Join(vaultDir, path)
f, err := os.Open(absFilePath)
if err != nil {
log.Printf("error opening note to calculate stats: %s\n", err)
return err
}
fStat, err := f.Stat()
if err != nil {
log.Printf("error calling stat() on file: %s\n", err)
return err
}
basePath := filepath.Base(path)
noteLengthsChar[basePath] = int(fStat.Size())
r := bufio.NewScanner(f)
for r.Scan() {
noteLengthsWord[basePath] += 1
}
return nil
})
log.Printf("Average character count: %.2f\n", averageMap(noteLengthsChar))
log.Printf("Average word count: %.2f\n", averageMap(noteLengthsWord))
}
func averageMap(m map[string]int) float64 {
sum := 0.0
for _, v := range m {
sum += float64(v)
}
return sum / float64(len(m))
}
Loading…
Cancel
Save