-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_notes.go
46 lines (38 loc) · 1.04 KB
/
list_notes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"os"
"time"
"github.com/dustin/go-humanize"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/urfave/cli/v2"
_ "modernc.org/sqlite"
)
func ListNotes(c *cli.Context) error {
err := FindAndOpenDB()
if err != nil {
return ExitWithError(err, "DB file not found. Did you call 'init' yet?")
}
var notes []Note
if c.Args().Len() == 0 {
notes, err = FindAllNotes()
} else {
notes, err = FindNotesBySubstring(c.Args().First())
}
if err != nil {
return ExitWithError(err, "Could not find notes")
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Title", "Created", "Modified"})
for _, note := range notes {
ct, _ := time.Parse(time.RFC3339, note.created)
mt, _ := time.Parse(time.RFC3339, note.modified)
t.AppendRow(table.Row{note.id, note.title, humanize.Time(ct), humanize.Time(mt)})
}
t.SetStyle(table.StyleRounded)
t.Style().Color.Header = text.Colors{text.FgYellow}
t.Style().Options.SeparateRows = true
t.Render()
return nil
}