This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
folders.go
177 lines (139 loc) · 3.97 KB
/
folders.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-10-22
//
package main
import (
"fmt"
"log"
"strings"
aw "github.com/deanishe/awgo"
safari "github.com/deanishe/go-safari"
)
// Filters all bookmark folders and output Alfred results.
func doFilterAllFolders() error {
showUpdateStatus()
log.Printf("query=%s", query)
sf := safari.Folders()
// Send results
// log.Printf("Sending %d results to Alfred ...", len(ff))
for _, f := range sf {
folderItem(f)
}
if query != "" {
res := wf.Filter(query)
log.Printf("%d folder(s) match %q", len(res), query)
for i, r := range res {
log.Printf("#%02d %5.2f %q", i+1, r.Score, r.SortKey)
}
}
wf.WarnEmpty("No folders found", "Try a different query?")
wf.SendFeedback()
return nil
}
// Filters the contents of a specific folder and output Alfred results.
func doFilterFolder() error {
log.Printf("query=%s, uid=%s", query, uid)
// ----------------------------------------------------------------
// Gather results
f := safari.FolderForUID(uid)
if f == nil {
return fmt.Errorf("No folder found with UID: %s", uid)
}
log.Printf("%d folders, %d bookmarks in \"%s\"", len(f.Folders), len(f.Bookmarks), f.Title())
// ----------------------------------------------------------------
// Show "Back" options if query is empty
if query == "" {
wf.Configure(aw.SuppressUIDs(true))
if len(f.Ancestors) > 0 {
p := f.Ancestors[len(f.Ancestors)-1]
it := wf.NewItem(fmt.Sprintf("Up to \"%s\"", p.Title())).
Icon(IconUp).
Valid(true).
Var("ALSF_UID", p.UID())
// Alternate action: Go to All Folders
it.NewModifier("cmd").
Subtitle("Go back to All Folders").
Var("action", "top")
// Default only
it.Var("action", "browse")
// it.SetVar("browse_folder", "1")
} else if uid != "" { // One of the top-level items, e.g. Favorites
wf.NewItem("Back to All Folders").
Valid(true).
Icon(IconHome).
Var("action", "top")
}
}
// ----------------------------------------------------------------
// Sort Folders and Bookmarks
items := []safari.Item{}
for _, f2 := range f.Folders {
items = append(items, f2)
}
for _, bm := range f.Bookmarks {
items = append(items, bm)
}
log.Printf("%d item(s) in folder %q", len(items), f.Title())
for _, it := range items {
if bm, ok := it.(*safari.Bookmark); ok {
URLerItem(&bmURLer{bm})
} else if f2, ok := it.(*safari.Folder); ok {
folderItem(f2)
} else {
log.Printf("Could't cast item: %v", it)
}
}
if query != "" {
res := wf.Filter(query)
log.Printf("%d result(s) for %q", len(res), query)
for i, r := range res {
log.Printf("#%02d %5.2f %q", i+1, r.Score, r.SortKey)
}
}
wf.WarnEmpty("No bookmarks or folders found", "Try a different query?")
wf.SendFeedback()
return nil
}
// --------------------------------------------------------------------
// Helpers
// folderSubtitle generates a subtitle for a Folder.
func folderSubtitle(f *safari.Folder) string {
s := []string{}
for _, f2 := range f.Ancestors {
s = append(s, f2.Title())
}
return strings.Join(s, " / ")
}
// folderTitle generates a title for a Folder.
func folderTitle(f *safari.Folder) string {
return fmt.Sprintf("%s (%d bookmarks)", f.Title(), len(f.Bookmarks))
}
// folderItem returns a feedback Item for Safari Folder.
func folderItem(f *safari.Folder) *aw.Item {
it := wf.NewItem(folderTitle(f)).
Subtitle(folderSubtitle(f)).
Match(f.Title()).
UID(f.UID()).
Icon(IconFolder)
// Make folder actionable if it isn't empty
if len(f.Bookmarks)+len(f.Folders) > 0 {
it.Valid(true).
Var("ALSF_UID", f.UID()).
Var("ALSF_ACTION", urlActionDefault)
// Allow opening folder if it contains bookmarks
m := it.NewModifier("cmd")
if len(f.Bookmarks) > 0 {
m.Subtitle(fmt.Sprintf("Open %d bookmark(s)", len(f.Bookmarks)))
m.Var("action", "open")
} else {
m.Valid(false)
}
// Default only
it.Var("action", "browse")
}
return it
}