forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
92 lines (78 loc) · 2.67 KB
/
__init__.py
File metadata and controls
92 lines (78 loc) · 2.67 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
# -*- coding: utf-8 -*-
import json
import subprocess
from albert import *
md_iid = '2.0'
md_version = "1.4"
md_name = "CopyQ"
md_description = "Access CopyQ clipboard"
md_license = "BSD-2-Clause"
md_url = "https://github.com/albertlauncher/python"
md_bin_dependencies = ["copyq"]
md_maintainers = "@BarrensZeppelin"
copyq_script_getAll = r"""
var result=[];
for ( var i = 0; i < size(); ++i ) {
var obj = {};
obj.row = i;
obj.mimetypes = str(read("?", i)).split("\n");
obj.mimetypes.pop();
obj.text = str(read(i));
result.push(obj);
}
JSON.stringify(result);
"""
copyq_script_getMatches = r"""
var result=[];
var match = "%s";
for ( var i = 0; i < size(); ++i ) {
if (str(read(i)).search(new RegExp(match, "i")) !== -1) {
var obj = {};
obj.row = i;
obj.mimetypes = str(read("?", i)).split("\n");
obj.mimetypes.pop();
obj.text = str(read(i));
result.push(obj);
}
}
JSON.stringify(result);
"""
class Plugin(PluginInstance, TriggerQueryHandler):
def __init__(self):
TriggerQueryHandler.__init__(self,
id=md_id,
name=md_name,
description=md_description,
synopsis="<filter>",
defaultTrigger='cq ')
PluginInstance.__init__(self, extensions=[self])
def handleTriggerQuery(self, query):
items = []
q_string = query.string
script = copyq_script_getMatches % q_string if q_string else copyq_script_getAll
proc = subprocess.run(["copyq", "-"], input=script.encode(), stdout=subprocess.PIPE)
json_arr = json.loads(proc.stdout.decode())
for json_obj in json_arr:
row = json_obj["row"]
text = json_obj["text"]
if not text:
text = "No text"
else:
text = " ".join(filter(None, text.replace("\n", " ").split(" ")))
act = lambda script, row=row: (
lambda: runDetachedProcess(["copyq", script % row])
)
items.append(
StandardItem(
id=md_id,
iconUrls=["xdg:copyq"],
text=text,
subtext="%s: %s" % (row, ", ".join(json_obj["mimetypes"])),
actions=[
Action("paste", "Paste", act("select(%s); sleep(60); paste();")),
Action("copy", "Copy", act("select(%s);")),
Action("remove", "Remove", act("remove(%s);")),
],
)
)
query.add(items)