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 ViewZet(zetDir string, id int) string { zetPath := path.Join(zetDir, strconv.Itoa(id), "index.adoc") return ViewFile(zetPath) } 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 "" }