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
|
pragma Singleton
import Quickshell
import Quickshell.Io
import Quickshell.Wayland
import qs.components
import qs.utils
import qs.services
import "../utils/string_utils.js" as StringUtils
Searchable {
id: root
key: "data"
algorithm: Searchable.SearchAlgorithm.Include
property list<string> _clipHist: []
property list<string> _clipImg: []
property list<string> _clipImgIds: _clipImg.map(p => p.split("/").pop().split(".").shift())
property var _clipMetadata: ({})
property var _clipDecoded: ({})
function getType(text: string): string {
if (!text)
return;
if (text.startsWith("http://") || text.startsWith("https://"))
return "web";
if (text.startsWith("file://"))
return "file";
return "text";
}
list: {
if (!_clipHist?.length)
return [];
const imgMap = _clipImgIds.slice(0, Math.min(_clipImg.length, _clipImgIds.length)).reduce((map, id, i) => {
if (id)
map[id] = _clipImg[i];
return map;
}, {});
return _clipHist.filter(raw => raw).map((raw, idx) => {
const tab = raw.indexOf("\t");
let idNum = -1;
let payload = raw;
if (tab > 0) {
const n = parseInt(raw.slice(0, tab), 10);
if (!Number.isNaN(n)) {
idNum = n;
payload = raw.slice(tab + 1);
}
}
const imgPath = idNum >= 0 ? imgMap[idNum] || "" : "";
const isImg = !!imgPath;
const metadata = _clipMetadata[idNum] || {};
return {
index: idNum,
type: isImg ? "image" : root.getType(payload),
data: isImg ? imgPath : payload,
raw: raw,
timestamp: metadata.timestamp || "",
appId: metadata.appId || "",
appTitle: metadata.appTitle || "",
appIcon: Quickshell.iconPath(AppSearch.guessIcon(metadata.appId), "image-missing"),
image: imgPath,
decoded: root._clipDecoded[idx] || ""
};
});
}
Persistent {
id: clipMetaStore
filePath: Directories.clipHistMetaDataPath
adapter: JsonAdapter {
property var metadata: ({})
}
fileView.onLoaded: {
const meta = adapter.metadata;
for (const k in meta) {
if (meta[k].timestamp && typeof meta[k].timestamp === "string") {
meta[k].timestamp = new Date(meta[k].timestamp);
}
}
root._clipMetadata = meta;
}
}
on_ClipMetadataChanged: {
clipMetaStore.adapter.metadata = _clipMetadata;
}
function transformSearch(search) {
return search.toLowerCase();
}
function decodeAndCopy(text) {
Quickshell.execDetached(["sh", "-c", `cliphist decode '${StringUtils.shellSingleQuoteEscape(text)}' | wl-copy`]);
}
function copy(text) {
Quickshell.execDetached(["wl-copy", text]);
}
function _update() {
clipProc.running = true;
clipDecodeProc.running = true;
imgProc.running = true;
}
function _captureMetadata() {
const now = new Date();
const activeToplevel = ToplevelManager.activeToplevel;
Qt.callLater(() => {
clipProc.running = true;
clipProc.stdout.onceFinished = () => {
const lines = clipProc.stdout.text.split(/\r?\n/);
const firstLine = lines[0] || "";
const tab = firstLine.indexOf("\t");
if (tab > 0) {
const idStr = firstLine.slice(0, tab);
const n = parseInt(idStr, 10);
if (!Number.isNaN(n)) {
let newMetadata = {};
for (var k in _clipMetadata) {
if (_clipMetadata.hasOwnProperty(k))
newMetadata[k] = _clipMetadata[k];
}
newMetadata[idStr] = {
timestamp: now,
appId: activeToplevel ? activeToplevel.appId : "",
appTitle: activeToplevel ? activeToplevel.title : ""
};
_clipMetadata = newMetadata;
}
}
};
});
}
Process {
id: clipProc
running: true
command: ["cliphist", "-preview-width", 9 ** 9, "-max-items", 9 ** 9, "list"]
stdout: StdioCollector {
property bool pending: false
property var onceFinished: null
onStreamFinished: () => {
pending = true;
Qt.callLater(() => {
if (pending) {
pending = false;
_clipHist = this.text.split(/\r?\n/);
if (onceFinished) {
onceFinished();
onceFinished = null;
}
}
});
}
}
}
Process {
id: clipDecodeProc
property var tmp: ({})
property int index: 0
running: true
command: ["sh", "-c", 'cliphist list | while IFS= read -r line; do\n' + ' cliphist decode "$line"; ' + 'printf "THIS IS A SEPERATOR 123 -----";\n' + 'done']
onStarted: {
clipDecodeProc.tmp = [];
clipDecodeProc.index = 0;
}
onExited: {
if (clipDecodeProc.tmp)
root._clipDecoded = clipDecodeProc.tmp;
}
stdout: SplitParser {
onRead: data => {
data = data += '\n';
if (data.includes("THIS IS A SEPERATOR 123 -----")) {
const split = data.split("THIS IS A SEPERATOR 123 -----");
const first = split[0] || "";
const second = split[1] || "";
if (first)
clipDecodeProc.tmp[clipDecodeProc.index] += first;
clipDecodeProc.index++;
if (second)
clipDecodeProc.tmp[clipDecodeProc.index] = second;
return;
}
if (!clipDecodeProc.tmp[clipDecodeProc.index])
clipDecodeProc.tmp[clipDecodeProc.index] = data;
else
clipDecodeProc.tmp[clipDecodeProc.index] += data;
}
}
}
Process {
id: imgProc
running: true
command: ["sh", Quickshell.shellDir + "/utils/cliphist-img.sh"]
stdout: StdioCollector {
property bool pending: false
onStreamFinished: () => {
pending = true;
Qt.callLater(() => {
if (pending) {
pending = false;
_clipImg = this.text.split(/\r?\n/);
}
});
}
}
}
Process {
id: watchProc
running: true
command: ["wl-paste", "-w", "echo"]
onRunningChanged: if (!running)
running = true
stdout: SplitParser {
onRead: line => {
_captureMetadata();
_update();
}
}
}
}
|