2025-09-30 23:49:23 +01:00
|
|
|
package zet
|
2025-09-23 23:56:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2025-09-30 23:49:23 +01:00
|
|
|
|
|
|
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
|
|
|
"code.oliverdavies.uk/opdavies/cmd-zet/internal/git"
|
2025-09-23 23:56:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func getTitle(id int) string {
|
|
|
|
return getTitleFromFile(path.Join(strconv.Itoa(id), "index.adoc"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTitleFromFile(filePath string) string {
|
2025-09-30 23:49:23 +01:00
|
|
|
filePath = path.Join(config.GetZetDir(), filePath)
|
2025-09-23 23:56:47 +01:00
|
|
|
|
|
|
|
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 ""
|
|
|
|
}
|
2025-09-25 23:30:22 +01:00
|
|
|
|
|
|
|
func onSave(id int) {
|
|
|
|
title := getTitle(id)
|
|
|
|
|
2025-09-30 23:49:23 +01:00
|
|
|
git.CommitZettel(id, title)
|
2025-09-29 22:36:04 +01:00
|
|
|
|
|
|
|
// TODO: Or delete the file if empty.
|
2025-09-25 23:30:22 +01:00
|
|
|
}
|