From 17f34afb2ed9dfb391678f65527785ce3852db9c Mon Sep 17 00:00:00 2001 From: Ted Pier Date: Mon, 18 Aug 2025 20:34:35 -0700 Subject: [PATCH] make stuff sorted and dates n stuff --- main.go | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 9d52134..e6ad3ea 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,11 @@ import ( "os/exec" "os/signal" "slices" + "strconv" "strings" + "sync" "syscall" + "time" "golang.org/x/term" @@ -149,22 +152,42 @@ CREATE TABLE notes ( check(err, "Error unmarshalling user data") serverAddress, username, password = userData[0], userData[1], userData[2] - - fzf := exec.Command("fzf") - stdin, err := fzf.StdinPipe() + var notes []Note + fzf := exec.Command("fzf", "--ansi") + pipe, err := fzf.StdinPipe() check(err, "Error getting stdin pipe for fzf") check(fzf.Start(), "Error starting fzf") + var wg sync.WaitGroup cacheJsonBody := load(db) - listAll(cacheJsonBody, stdin) + for i := range cacheJsonBody { + go list(cacheJsonBody[i][1], ¬es, &wg) + wg.Add(1) + } fetchJsonBody := fetch() for i := range fetchJsonBody { if !slices.Contains(cacheJsonBody, fetchJsonBody[i]) { - go list(fetchJsonBody[i][1], stdin, Note{}) + go list(fetchJsonBody[i][1], ¬es, &wg) + wg.Add(1) } } + wg.Wait() + + slices.SortFunc(notes, func(a, b Note) int { + return strings.Compare(b.Date, a.Date) + }) + for i := range notes { + timestamp, err := strconv.ParseInt(notes[i].Date, 10, 64) + if err != nil { + fmt.Println("Error parsing Unix timestamp", err) + continue + } + date := time.Unix(timestamp/1000, 0).Format("Mon, Jan 01, 06") + fmt.Fprintf(pipe, "%s | \033[1m%s\033[0m | %s\n", date, notes[i].Title, strings.ReplaceAll(notes[i].Body, "\n", " / ")) + } + pipe.Close() cache(db, fetchJsonBody) fzf.Wait() default: @@ -292,19 +315,15 @@ func load(db *sql.DB) [][2]string { return notes } -func listAll(jsonBody [][2]string, fzfStdin io.WriteCloser) { - decryptedBody := make([]Note, len(jsonBody)) - for i := range jsonBody { - go list(jsonBody[i][1], fzfStdin, decryptedBody[i]) - } -} - -func list(jsonBody string, fzfStdin io.WriteCloser, decryptedBody Note) { +func list(jsonBody string, notes *[]Note, wg *sync.WaitGroup) { + defer wg.Done() + var decryptedBody Note err := json.Unmarshal([]byte(decrypt(jsonBody, password)), &decryptedBody) if err != nil { return } if len(decryptedBody.Title) > 0 && len(decryptedBody.Body) > 0 { - fmt.Fprintf(fzfStdin, "%s | %s\n", decryptedBody.Title, strings.ReplaceAll(decryptedBody.Body, "\n", " ")) + *notes = append(*notes, decryptedBody) // ew + return } }