forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyq.py
96 lines (80 loc) · 2.64 KB
/
copyq.py
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
# -*- coding: utf-8 -*-
"""Access CopyQ clipboard.
Synopsis: <trigger> [filter]"""
import html
import json
import re
import subprocess
from shutil import which
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "CopyQ"
__version__ = "1.1"
__trigger__ = "cq "
__author__ = "Manuel Schneider"
__dependencies__ = ["copyq"]
if which("copyq") is None:
raise Exception("'copyq' is not in $PATH.")
iconPath = iconLookup('copyq')
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);
"""
def copyq_get_matches(substring):
script = copyq_script_getMatches % substring
proc = subprocess.run(['copyq', '-'], input=script.encode(), stdout=subprocess.PIPE)
return json.loads(proc.stdout.decode())
def copyq_get_all():
proc = subprocess.run(['copyq', '-'], input=copyq_script_getAll.encode(), stdout=subprocess.PIPE)
return json.loads(proc.stdout.decode())
def handleQuery(query):
if query.isTriggered:
items = []
pattern = re.compile(query.string, re.IGNORECASE)
json_arr = copyq_get_matches(query.string) if query.string else copyq_get_all()
for json_obj in json_arr:
row = json_obj['row']
text = json_obj['text']
if not text:
text = "<i>No text</i>"
else:
text = html.escape(" ".join(filter(None, text.replace("\n", " ").split(" "))))
if query.string:
text = pattern.sub(lambda m: "<u>%s</u>" % m.group(0), text)
items.append(
Item(
id=__prettyname__,
icon=iconPath,
text=text,
subtext="%s: %s" % (row, ", ".join(json_obj['mimetypes'])),
actions=[
ProcAction("Paste", ["copyq", "select(%s); sleep(60); paste();" % row]),
ProcAction("Copy", ["copyq", "select(%s);" % row]),
ProcAction("Remove", ["copyq", "remove(%s);" % row]),
]
)
)
return items