Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a4901b9ae7
commit
d27eca0a07
13 changed files with 71 additions and 57 deletions
|
@ -1,9 +0,0 @@
|
|||
package lib
|
||||
|
||||
import "regexp"
|
||||
|
||||
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||
|
||||
func StripANSI(s string) string {
|
||||
return ansiRegex.ReplaceAllString(s, "")
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package lib
|
||||
|
||||
import "os"
|
||||
|
||||
func GetZetDir() string {
|
||||
return os.Getenv("ZET_DIRECTORY")
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func EditFile(filePath string) {
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
fmt.Printf("Error: The file for path '%s' was not found\n", filePath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
editor := os.Getenv("EDITOR")
|
||||
|
||||
err := Exec(editor, filePath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func ViewFile(filePath string) string {
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
fmt.Printf("Error: The file for path '%s' was not found\n", filePath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error opening the file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return string(content)
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SelectWithFzf(items []string) (string, error) {
|
||||
cmd := exec.Command("fzf", "--ansi")
|
||||
|
||||
var input bytes.Buffer
|
||||
|
||||
for _, item := range items {
|
||||
input.WriteString(item + "\n")
|
||||
}
|
||||
|
||||
cmd.Stdin = &input
|
||||
|
||||
var output bytes.Buffer
|
||||
cmd.Stdout = &output
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Run()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
selected := strings.TrimSpace(output.String())
|
||||
|
||||
return selected, nil
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func CommitZettel(id int, title string) {
|
||||
idString := strconv.Itoa(id)
|
||||
|
||||
runGitCommand("add", idString)
|
||||
runGitCommand("commit", "-m", title)
|
||||
runGitCommand("push")
|
||||
}
|
||||
|
||||
func execGitCommand(parts ...string) (string, error) {
|
||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
||||
command := exec.Command("git", args...)
|
||||
|
||||
output, err := command.CombinedOutput()
|
||||
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func runGitCommand(parts ...string) {
|
||||
args := append([]string{"-C", GetZetDir()}, parts...)
|
||||
command := exec.Command("git", args...)
|
||||
|
||||
command.Stderr = os.Stderr
|
||||
command.Stdin = os.Stdin
|
||||
command.Stdout = os.Stdout
|
||||
|
||||
command.Run()
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func AsJSON(zets []string) (string, error) {
|
||||
var items []Item
|
||||
|
||||
for _, entry := range zets {
|
||||
cleanEntry := StripANSI(entry)
|
||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
||||
|
||||
id, _ := strconv.Atoi(parts[0])
|
||||
|
||||
item := Item{
|
||||
ID: id,
|
||||
Title: parts[1],
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(items, "", " ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling to JSON:", err)
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(jsonData), nil
|
||||
}
|
|
@ -1,199 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CreateZet(title string) {
|
||||
zid := newZid()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
EditFile(filePath)
|
||||
|
||||
onSave(zid)
|
||||
}
|
||||
|
||||
func EditZet(id int) {
|
||||
zetPath := path.Join(GetZetDir(), strconv.Itoa(id), "index.adoc")
|
||||
|
||||
EditFile(zetPath)
|
||||
|
||||
onSave(id)
|
||||
}
|
||||
|
||||
func GetAllZets() []int {
|
||||
zets, err := execGitCommand("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 GetLatestZet() int {
|
||||
z := GetAllZets()
|
||||
|
||||
return z[len(z)-1]
|
||||
}
|
||||
|
||||
func SearchZets(query string) []int {
|
||||
zets, err := execGitCommand("grep", "-i", "--name-only", "--word-regex", query)
|
||||
|
||||
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)
|
||||
|
||||
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))
|
||||
|
||||
lines = append(lines, line)
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func ViewZet(id int) string {
|
||||
zetPath := path.Join(GetZetDir(), 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(GetZetDir(), 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 ""
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func onSave(id int) {
|
||||
title := getTitle(id)
|
||||
|
||||
CommitZettel(id, title)
|
||||
|
||||
// TODO: Or delete the file if empty.
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue