make stuff sorted and dates n stuff
This commit is contained in:
47
main.go
47
main.go
@@ -21,8 +21,11 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
@@ -149,22 +152,42 @@ CREATE TABLE notes (
|
|||||||
check(err, "Error unmarshalling user data")
|
check(err, "Error unmarshalling user data")
|
||||||
|
|
||||||
serverAddress, username, password = userData[0], userData[1], userData[2]
|
serverAddress, username, password = userData[0], userData[1], userData[2]
|
||||||
|
var notes []Note
|
||||||
fzf := exec.Command("fzf")
|
fzf := exec.Command("fzf", "--ansi")
|
||||||
stdin, err := fzf.StdinPipe()
|
pipe, err := fzf.StdinPipe()
|
||||||
check(err, "Error getting stdin pipe for fzf")
|
check(err, "Error getting stdin pipe for fzf")
|
||||||
check(fzf.Start(), "Error starting fzf")
|
check(fzf.Start(), "Error starting fzf")
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
cacheJsonBody := load(db)
|
cacheJsonBody := load(db)
|
||||||
listAll(cacheJsonBody, stdin)
|
for i := range cacheJsonBody {
|
||||||
|
go list(cacheJsonBody[i][1], ¬es, &wg)
|
||||||
|
wg.Add(1)
|
||||||
|
}
|
||||||
fetchJsonBody := fetch()
|
fetchJsonBody := fetch()
|
||||||
|
|
||||||
for i := range fetchJsonBody {
|
for i := range fetchJsonBody {
|
||||||
if !slices.Contains(cacheJsonBody, fetchJsonBody[i]) {
|
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)
|
cache(db, fetchJsonBody)
|
||||||
fzf.Wait()
|
fzf.Wait()
|
||||||
default:
|
default:
|
||||||
@@ -292,19 +315,15 @@ func load(db *sql.DB) [][2]string {
|
|||||||
return notes
|
return notes
|
||||||
}
|
}
|
||||||
|
|
||||||
func listAll(jsonBody [][2]string, fzfStdin io.WriteCloser) {
|
func list(jsonBody string, notes *[]Note, wg *sync.WaitGroup) {
|
||||||
decryptedBody := make([]Note, len(jsonBody))
|
defer wg.Done()
|
||||||
for i := range jsonBody {
|
var decryptedBody Note
|
||||||
go list(jsonBody[i][1], fzfStdin, decryptedBody[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func list(jsonBody string, fzfStdin io.WriteCloser, decryptedBody Note) {
|
|
||||||
err := json.Unmarshal([]byte(decrypt(jsonBody, password)), &decryptedBody)
|
err := json.Unmarshal([]byte(decrypt(jsonBody, password)), &decryptedBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(decryptedBody.Title) > 0 && len(decryptedBody.Body) > 0 {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user