-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
93 lines (76 loc) · 2.94 KB
/
__init__.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
# -*- coding: utf-8 -*-
import subprocess
from albert import *
__doc__="""
Qalculate!
Synopsis: `<trigger> <expression>`
"""
md_iid = "0.5"
md_version = "0.1"
md_name = "Qalculate!"
md_description = "Qalculate!"
md_license = "MIT"
md_url = "https://github.com/hankliao87/albert-qalculate"
md_maintainers = "@hankliao87"
md_bin_dependencies = ["qalc"]
trigger = "qc "
class Plugin(QueryHandler):
iconPath = ["xdg:qalculate"]
def id(self):
return __name__
def name(self):
return md_name
def description(self):
return md_description
def synopsis(self) -> str:
return "<expression>"
def defaultTrigger(self) -> str:
return trigger
def handleQuery(self, query):
query_string = query.string.strip()
if query_string == '':
item = Item(id=__name__, icon=self.iconPath)
item.text = "Enter an expression"
item.subtext = ""
query.add(item)
else:
try:
proc_result = subprocess.run(['qalc', '-s', 'upxrates 0', '-t', query_string], stdout=subprocess.PIPE, text=True, timeout=10)
proc_expression = subprocess.run(['qalc', '-s', 'upxrates 0', query_string], stdout=subprocess.PIPE, text=True, timeout=10)
result = proc_result.stdout.replace('\n','')
result_with_expression = proc_expression.stdout.replace('\n','')
item = Item(
id=__name__,
icon=self.iconPath,
actions=[
Action(
id="copy-clipboard",
text="Copy result to clipboard",
callable=lambda u=str(result): setClipboardText(u)),
Action(
id="copy-clipboard-expression",
text="Copy result with expression to clipboard",
callable=lambda u=str(result_with_expression): setClipboardText(u)),
]
)
item.text = str(result)
item.subtext = query_string
query.add(item)
except Exception as ex:
item = Item(
id=__name__,
icon=self.iconPath,
actions=[
Action(
id="copy-clipboard",
text="Copy error message to clipboard",
callable=lambda u=str(ex): setClipboardText(u)),
Action(
id="copy-repo-url",
text="Copy repo url to clipboard",
callable=lambda: setClipboardText(md_url)),
]
)
item.text = "Error: " + str(ex)
item.subtext = f"Please create an issue in {md_url}"
query.add(item)