2025-09-23 23:56:47 +01:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2025-09-26 09:33:16 +01:00
|
|
|
"os/exec"
|
2025-09-23 23:56:47 +01:00
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2025-09-26 09:33:16 +01:00
|
|
|
func CreateZet(title string) {
|
|
|
|
zid := newZid()
|
|
|
|
|
|
|
|
fmt.Println(title, zid)
|
|
|
|
|
|
|
|
path := path.Join(GetZetDir(), strconv.Itoa(zid))
|
|
|
|
|
|
|
|
os.Mkdir(path, 0750)
|
|
|
|
|
|
|
|
filePath := fmt.Sprintf("%s/index.adoc", path)
|
|
|
|
|
|
|
|
file, err := os.Create(filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = fmt.Fprintf(file, "= %s", title)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2025-09-26 12:17:37 +01:00
|
|
|
|
|
|
|
EditFile(filePath)
|
|
|
|
|
|
|
|
onSave(zid)
|
2025-09-26 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
2025-09-24 19:35:56 +01:00
|
|
|
func EditZet(id int) {
|
|
|
|
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
|
|
|
|
|
|
|
EditFile(zetPath)
|
2025-09-25 23:30:22 +01:00
|
|
|
|
|
|
|
onSave(id)
|
2025-09-24 19:35:56 +01:00
|
|
|
}
|
|
|
|
|
2025-09-24 18:39:03 +01:00
|
|
|
func GetAllZets() []int {
|
|
|
|
zets, err := execGitCommand("ls-files")
|
2025-09-23 23:56:47 +01:00
|
|
|
|
|
|
|
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)
|
2025-09-24 13:00:00 +01:00
|
|
|
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
2025-09-24 18:39:03 +01:00
|
|
|
func SearchZets(query string) []int {
|
|
|
|
zets, err := execGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
2025-09-24 13:00:00 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("No matches found for %s.\n", query)
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2025-09-23 23:56:47 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-09-24 18:39:03 +01:00
|
|
|
func ViewZet(id int) string {
|
|
|
|
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
2025-09-24 08:30:00 +01:00
|
|
|
|
|
|
|
return ViewFile(zetPath)
|
|
|
|
}
|
|
|
|
|
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-24 18:39:03 +01:00
|
|
|
filePath = path.Join(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
|
|
|
|
2025-09-26 09:33:16 +01:00
|
|
|
func newZid() int {
|
|
|
|
cmd := exec.Command("ls", GetZetDir())
|
|
|
|
output, _ := cmd.CombinedOutput()
|
|
|
|
|
|
|
|
zets := strings.Split(string(output), "\n")
|
|
|
|
|
|
|
|
var zetCount int
|
|
|
|
|
|
|
|
for _, zet := range zets {
|
|
|
|
num, err := strconv.Atoi(zet)
|
|
|
|
if err == nil && num > zetCount {
|
|
|
|
zetCount = num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zetCount + 1
|
|
|
|
}
|
|
|
|
|
2025-09-25 23:30:22 +01:00
|
|
|
func onSave(id int) {
|
|
|
|
title := getTitle(id)
|
|
|
|
|
|
|
|
CommitZettel(id, title)
|
|
|
|
}
|