forked from bjrnt/alfred-bear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (65 loc) · 1.65 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"os"
aw "github.com/deanishe/awgo"
"github.com/dustin/go-humanize"
"golang.org/x/text/unicode/norm"
)
const (
ignoreTrashedNotesEnvVar = "IGNORE_TRASHED"
ignoreArchivedNotesEnvVar = "IGNORE_ARCHIVED"
)
var (
IconWorkflow = &aw.Icon{
Value: "icon.png",
Type: aw.IconTypeImage,
}
wf *aw.Workflow
userHomeDir string
ignoreTrashed = true
ignoreArchived = true
)
func tryGetEnvBool(key string) bool {
val := os.Getenv(key)
return len(val) != 0 && val != "0"
}
func init() {
wf = aw.New()
ignoreTrashed = tryGetEnvBool(ignoreTrashedNotesEnvVar)
ignoreArchived = tryGetEnvBool(ignoreArchivedNotesEnvVar)
// Try to read the user's home directory. This is required for finding Bear's SQLite DB
var err error
userHomeDir, err = os.UserHomeDir()
if err != nil {
wf.FatalError(err)
}
}
func run() {
// For some annoying reason, UTF-8 arguments from Alfred are not normalized in the correct way,
// so any non-ASCII characters break the query. Here we normalize the UTF-8 input to make sure
// that it can be handled properly by the DB
query := string(norm.NFC.Bytes([]byte(os.Args[1])))
// search the user's bear notes
notes, err := search(query)
if err != nil {
wf.FatalError(err)
}
// send the results to alfred
sendNotes(notes)
wf.SendFeedback()
}
func sendNotes(notes []note) {
for _, note := range notes {
wf.NewItem(note.Title).
Subtitle(fmt.Sprintf("Last edited %s", humanize.Time(note.ModificationDate))).
UID(note.Identifier).
Arg(note.Identifier).
Valid(true).
Icon(IconWorkflow)
}
wf.WarnEmpty("No matching notes found", "Try another query")
}
func main() {
wf.Run(run)
}