-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.qml
More file actions
274 lines (233 loc) · 7.77 KB
/
Copy pathService.qml
File metadata and controls
274 lines (233 loc) · 7.77 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import QtQuick
import Quickshell
import Quickshell.Io
import "Vault.js" as Vault
// Shared state for the bar popup and the lazy full panel. Both show the same
// note list, so the listing runs once here rather than once per surface.
Item {
id: root
visible: false
width: 0
height: 0
property var shell: null
property var manifest: null
property var pluginRegistry: null
readonly property string home: Quickshell.env("HOME") || ""
readonly property string pluginId: manifest && manifest.id
? String(manifest.id) : "harbefas.vault"
readonly property string pluginDir: manifest && manifest.__sourceDir
? String(manifest.__sourceDir) : home + "/.config/omarchy/plugins/harbefas.vault"
property string vaultPath: ""
property int recentCount: 40
readonly property string resolvedVault: vaultPath.trim()
readonly property bool vaultConfigured: vaultPath.trim() !== ""
onVaultPathChanged: {
index = []
recent = []
notes = []
recentMtimes = ({})
refresh()
}
// Every note in the vault, used to resolve wikilinks by title. Cheap to
// hold: the listing already walked the whole tree.
property var index: []
property var recent: []
property var notes: []
property var recentMtimes: ({})
property string query: ""
property string searchTerm: ""
property bool searchAgain: false
property bool searching: false
// A listing that failed is not an empty vault. Without this the panel drew
// "No notes" whether the vault was empty, the path had been renamed, or
// find was not installed at all.
property string error: ""
// Offer an optional launcher shortcut without ever changing the user's
// Hyprland configuration. The suggestion disappears once a matching bind
// is found in either supported configuration format.
readonly property string suggestedKey: "SUPER + ALT + V"
readonly property string suggestedBind:
'o.bind("' + root.suggestedKey + '", "Vault quick view", "omarchy shell -q harbefas.vault.widget toggle")'
property bool keybindConfigured: false
property int bindingsScanned: 0
readonly property bool bindingsReady: root.bindingsScanned >= 2
function noteBindings(text) {
// The full panel binding is independent; only the widget binding disables
// this widget-launcher suggestion.
if (String(text || "").indexOf("harbefas.vault.widget") >= 0)
root.keybindConfigured = true
root.bindingsScanned += 1
}
function applySettings(s) {
if (!s) return
if (typeof s.vaultPath === "string" && s.vaultPath !== "") vaultPath = s.vaultPath
var count = Number(s.recentCount)
if (isFinite(count) && count > 0) recentCount = Math.round(count)
}
FileView {
path: home + "/.config/hypr/bindings.lua"
watchChanges: true
onLoaded: root.noteBindings(text())
onLoadFailed: root.bindingsScanned += 1
}
FileView {
path: home + "/.config/hypr/bindings.conf"
watchChanges: true
onLoaded: root.noteBindings(text())
onLoadFailed: root.bindingsScanned += 1
}
// ---------------------------------------------------------------- listing
function refresh() {
if (!root.vaultConfigured) return
if (!listProcess.running) listProcess.running = true
}
function setQuery(text) {
query = String(text || "")
searchDebounce.restart()
}
function runQuery() {
var term = query.trim()
if (term === "") {
searchAgain = false
notes = recent
searching = false
return
}
notes = Vault.filterNotes(index, term, recentCount)
if (searchProcess.running) {
searchAgain = true
return
}
searchTerm = term
searching = true
searchProcess.running = true
}
function finishSearch(output) {
var term = searchTerm
searching = false
notes = Vault.mergeSearch(output, index, term, resolvedVault, recentMtimes)
if (query.trim() !== term) {
searchAgain = false
searchDebounce.restart()
}
}
function openWidget() {
var bar = shell ? shell.bar : null
return !!bar && typeof bar.summonBarWidget === "function"
&& bar.summonBarWidget(pluginId) === true
}
function closeWidget() {
var bar = shell ? shell.bar : null
return !!bar && typeof bar.hideBarWidget === "function"
&& bar.hideBarWidget(pluginId) === true
}
function isWidgetOpen() {
var bar = shell ? shell.bar : null
return !!bar && typeof bar.isBarWidgetOpen === "function"
&& bar.isBarWidgetOpen(pluginId) === true
}
Timer {
id: searchDebounce
interval: 180
onTriggered: root.runQuery()
}
Process {
id: listProcess
running: false
property string errorText: ""
command: [root.pluginDir + "/bin/bounded-output", "find", root.resolvedVault,
"-type", "f", "-name", "*.md",
"-not", "-path", "*/.*", "-printf", "%T@\t%p\n"]
stderr: StdioCollector {
waitForEnd: true
onStreamFinished: listProcess.errorText = String(text || "").trim()
}
// find reports an empty result with 0, so anything else is a fault: a
// vault path that is gone, or find missing from PATH.
onExited: function(exitCode) {
root.error = exitCode === 0 ? ""
: (listProcess.errorText || "listing the vault failed with " + exitCode)
listProcess.errorText = ""
}
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
if (text.length > Vault.MAX_PROCESS_OUTPUT) {
root.index = []
root.recent = []
root.notes = []
root.recentMtimes = ({})
return
}
var all = Vault.parseListing(text, root.resolvedVault, 0)
var parsed = all.slice(0, root.recentCount)
root.index = all
root.recent = parsed
root.recentMtimes = Vault.mtimeMap(all)
if (root.query.trim() === "") root.notes = parsed
}
}
}
Process {
id: searchProcess
running: false
property string errorText: ""
command: [root.pluginDir + "/bin/bounded-output", "rg", "--files-with-matches",
"--smart-case", "--fixed-strings",
"--glob", "*.md", "--", root.searchTerm, root.resolvedVault]
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
if (text.length > Vault.MAX_PROCESS_OUTPUT) {
root.finishSearch("")
return
}
root.finishSearch(text)
}
}
stderr: StdioCollector {
waitForEnd: true
onStreamFinished: searchProcess.errorText = String(text || "").trim()
}
// rg exits 1 when nothing matched, which is an empty result, not a fault.
// 2 and above are, and so is 127 when rg is not installed.
onExited: function(exitCode) {
if (exitCode >= 2)
root.error = searchProcess.errorText || "search failed with " + exitCode
searchProcess.errorText = ""
if (root.searching) root.finishSearch("")
if (root.searchAgain) {
root.searchAgain = false
root.searchDebounce.restart()
}
}
}
// --------------------------------------------------------------- daily
function resolveNote(name) {
return Vault.resolveNote(name, index)
}
function dailyPath() {
return resolvedVault + "/" + Vault.dailyNotePath(new Date())
}
IpcHandler {
target: root.pluginId + ".widget"
function open(): string {
return root.openWidget() ? "opened" : "unavailable"
}
function close(): string {
return root.closeWidget() ? "closed" : "unavailable"
}
function show(): string {
return root.openWidget() ? "opened" : "unavailable"
}
function hide(): string {
return root.closeWidget() ? "closed" : "unavailable"
}
function toggle(): string {
return root.isWidgetOpen()
? (root.closeWidget() ? "closed" : "unavailable")
: (root.openWidget() ? "opened" : "unavailable")
}
}
Component.onCompleted: refresh()
}