commit 9dfe4ae0705167311afb9bff503e6800e32324e1 Author: Nick Dumas Date: Sat Jul 22 12:41:32 2023 -0400 first commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fd1ce0c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.ndumas.com/ndumas/vault-stats + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..fb0846d --- /dev/null +++ b/main.go @@ -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)) + +}