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
|
pragma Singleton
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Io
import qs.Common
Singleton {
id: root
property bool dsearchAvailable: false
property int searchIdCounter: 0
property int indexVersion: 0
property bool supportsTypeFilter: false
property bool versionChecked: false
signal searchResultsReceived(var results)
signal statsReceived(var stats)
signal errorOccurred(string error)
Process {
id: checkProcess
command: ["sh", "-c", "command -v dsearch"]
running: true
stdout: SplitParser {
onRead: line => {
if (line && line.trim().length > 0) {
root.dsearchAvailable = true;
}
}
}
onExited: exitCode => {
if (exitCode !== 0) {
root.dsearchAvailable = false;
} else {
root._checkVersion();
}
}
}
function _checkVersion() {
Proc.runCommand("dsearch-version", ["dsearch", "version", "--json"], (stdout, exitCode) => {
root.versionChecked = true;
if (exitCode !== 0)
return;
const response = JSON.parse(stdout);
root.indexVersion = response.index_schema || 0;
root.supportsTypeFilter = root.indexVersion >= 2;
});
}
function ping(callback) {
if (!dsearchAvailable) {
if (callback) {
callback({
"error": "dsearch not available"
});
}
return;
}
Proc.runCommand("dsearch-ping", ["dsearch", "ping", "--json"], (stdout, exitCode) => {
if (callback) {
if (exitCode === 0) {
try {
const response = JSON.parse(stdout);
callback({
"result": response
});
} catch (e) {
callback({
"error": "failed to parse ping response"
});
}
} else {
callback({
"error": "ping failed"
});
}
}
});
}
function search(query, params, callback) {
if (!query || query.length === 0) {
if (callback) {
callback({
"error": "query is required"
});
}
return;
}
if (!dsearchAvailable) {
if (callback) {
callback({
"error": "dsearch not available"
});
}
return;
}
const args = ["dsearch", "search", query, "--json"];
if (params) {
if (params.limit !== undefined) {
args.push("-n", String(params.limit));
}
if (params.type) {
args.push("-t", params.type);
}
if (params.ext) {
args.push("-e", params.ext);
}
if (params.folder) {
args.push("--folder", params.folder);
}
if (params.field) {
args.push("-f", params.field);
}
if (params.fuzzy) {
args.push("--fuzzy");
}
if (params.sort) {
args.push("--sort", params.sort);
}
if (params.desc !== undefined) {
args.push("--desc=" + (params.desc ? "true" : "false"));
}
if (params.minSize !== undefined) {
args.push("--min-size", String(params.minSize));
}
if (params.maxSize !== undefined) {
args.push("--max-size", String(params.maxSize));
}
}
const procId = "dsearch-search-" + (params?.type || "all");
Proc.runCommand(procId, args, (stdout, exitCode) => {
if (exitCode === 0) {
try {
const response = JSON.parse(stdout);
searchResultsReceived(response);
if (callback) {
callback({
"result": response
});
}
} catch (e) {
const error = "failed to parse search response";
errorOccurred(error);
if (callback) {
callback({
"error": error
});
}
}
} else if (exitCode === 124) {
const error = "search timed out";
errorOccurred(error);
if (callback) {
callback({
"error": error
});
}
} else {
const error = "search failed";
errorOccurred(error);
if (callback) {
callback({
"error": error
});
}
}
}, 100, 5000);
}
function rediscover() {
checkProcess.running = true;
}
}
|