Add titles command to show all IDs and titles

Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-09-23 23:56:47 +01:00
parent 12a0353a4c
commit 9019f4579d
10 changed files with 150 additions and 246 deletions

12
internal/lib/git.go Normal file
View file

@ -0,0 +1,12 @@
package lib
import "os/exec"
func execGitCommand(dir string, parts ...string) (string, error) {
args := append([]string{"-C", dir}, parts...)
command := exec.Command("git", args...)
output, err := command.CombinedOutput()
return string(output), err
}

93
internal/lib/lib.go Normal file
View file

@ -0,0 +1,93 @@
package lib
import (
"bufio"
"fmt"
"log"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
)
func GetAllZets(dir string) []int {
zets, err := execGitCommand(dir, "ls-files")
if err != nil {
log.Println(err)
}
re := regexp.MustCompile(`[0-9]+`)
matches := re.FindAllString(zets, -1)
sort.Strings(matches)
ids := make(map[int]struct{})
for _, id := range matches {
num, err := strconv.Atoi(id)
if err == nil {
ids[num] = struct{}{}
}
}
var sorted []int
for num := range ids {
sorted = append(sorted, num)
}
sort.Ints(sorted)
return sorted
}
func ParseZetList(ids []int) []string {
var lines []string
green := "\033[32m"
reset := "\033[0m"
for _, num := range ids {
line := fmt.Sprintf("%s%s%s %s", green, strconv.Itoa(num), reset, getTitle(num))
fmt.Println(line)
lines = append(lines, line)
}
return lines
}
func getTitle(id int) string {
return getTitleFromFile(path.Join(strconv.Itoa(id), "index.adoc"))
}
func getTitleFromFile(filePath string) string {
filePath = path.Join("/home/opdavies/Documents/zet", filePath)
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
text := scanner.Text()
return strings.TrimPrefix(text, "= ")
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
return ""
}