-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsxhkd-helper
executable file
·183 lines (171 loc) · 5 KB
/
sxhkd-helper
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
#!/usr/bin/env python3
import os
import re
import sys
from rich import print as pprint
CONFIG_FILE = f"{os.path.expanduser('~')}/.config/sxhkd/sxhkdrc"
CONFIG_FILE_ = f"{os.path.expanduser('~')}/temp/sxtest"
DST = "bold"
KST = "i green"
CST = ""
def reader(path: str):
content = ""
with open(path, 'r', encoding="utf-8") as f:
content = f.read()
return content
def globber(content: str):
out = []
for _ in content.split("\n\n"):
_ = _.strip()
if _[:4] in ["#== ", "#-- "]:
out.append(_)
return out
def breaker(line: str, override: bool = False):
pattern = r"{(.*?)}"
matches = re.search(pattern, line)
out = [line]
if matches and (not override):
parts = matches.group(1).split(",")
out = [re.sub(r'{.*}', _, line).strip() for _ in parts]
return out
return out
def classifier(lines: str, clss: str):
lines = [_.strip() for _ in lines.split("\n") if _.strip()]
defn = [_[4:] for _ in breaker(lines.pop(0))]
keys = []
cmds = []
override = False
if len(defn) == 1:
override = True
while lines:
key = lines.pop(0)
cmd = lines.pop(0)
keys += breaker(key, override = override)
cmds += breaker(cmd, override = override)
alt = False
if (len(keys) == len(cmds)) and (len(keys) // len(defn) > 1):
alt = True
return {
"defn": defn,
"keys": keys,
"cmds": cmds,
"alt": alt,
"cls": clss
}
def printer(hk: dict, ifcmd = False):
defn = hk['defn']
keys = hk['keys']
cmds = hk['cmds']
alt = hk['alt']
if not alt:
N = len(defn)
for _ in range(N):
pprint(f"[{DST}]{defn[_]}[/{DST}]")
pprint(f"[{KST}]{keys[_]}[/{KST}]", "\n")
else:
pprint(f"[{DST}]{defn[0]}[/{DST}]")
pprint(" \[or] ".join([f"[{KST}]{_}[/{KST}]" for _ in keys]), "\n")
def runner(path: str):
clss = ""
out = []
for _ in globber(reader(path)):
if _[:4] == "#-- ":
clss = _[4:]
continue
else:
out.append(classifier(_, clss))
return out
def flatcheck(dicts: list):
return not sum([_['alt'] for _ in dicts])
def flattener(dicts: list):
if flatcheck(dicts):
out = {}
keys = list(dicts[0])
for key in keys:
out[key] = []
for d in dicts:
if key in ['alt', 'cls']:
n = len(d['defn'])
out[key] += [d[key]]*n
else:
out[key] += d[key]
keys = out['keys']
for _, key in enumerate(keys):
key = re.sub(r"_", "", key)
key = re.sub(r"\+\s\s\+", r"+", key)
keys[_] = key
out['keys'] = keys
return out
CMDC = "#ebdbb2"
CMDF = "/home/rajeshkumar/data/sxhkd_catalog"
CLSC = "#a89984"
def rofiprint(path: str, symb = False):
data = flattener(runner(path))
defns = data['defn']
keys = data['keys']
cmds = data['cmds']
clss = data['cls']
N = len(defns)
dN = max(len(_) for _ in defns)
dC = max(len(_) for _ in clss)
outstr = ""
if symb:
subs = {
"@": "",
"shift": "⇧",
"super": "⊞",
"alt": "⌥",
"ctrl": "⌃",
"space": "␣",
"Return": "↵",
"Tab": "↹",
"Escape": "⮹",
"comma": ",",
"period": ".",
"grave": "`",
"bracketleft": "[",
"bracketright": "]",
"XF86Calculator": '🖩',
"XF86AudioMute": "Fn + F5",
"XF86AudioLowerVolume": "Fn + F6",
"XF86AudioRaiseVolume": "Fn + F7",
"XF86AudioPrev": "Fn + F8",
"XF86AudioPlay": "Fn + F9",
"XF86AudioNext": "Fn + F10",
"XF86MonBrightnessDown": "Fn + F2",
"XF86MonBrightnessUp": "Fn + F3",
"XF86Launch2": "🞛",
"{1-9,0}": "#",
"{1-9}": "#",
"Left": "←",
"Right": "→",
"Down": "↓",
"Up": "↑",
"Prior": "⭽",
"Next": "⭻",
"backslash": r"\\",
"Print": "⎙",
"Delete": "⌦",
"button1": "🖱1",
"button2": "🖱2",
"button8": "🖱>",
"button9": "<🖱",
}
for _, key in enumerate(keys):
for skey, ssub in subs.items():
key = re.sub(skey, ssub, key)
keys[_] = key
# for _ in range(N):
# outstr += "<b>"+ defns[_] + "</b>"+ "\n"
# outstr += f"<span color='{CMDC}'><i>" + keys[_] + f"</i></span>|"
with open(CMDF, "w") as file:
file.write("\n".join(cmds))
for _ in range(N):
outstr += f"<span color='{CLSC}'>{clss[_].rjust(dC)}</span> <b>{defns[_].ljust(dN)}</b> {keys[_]}|"
print(outstr[:-1])
if __name__ == "__main__":
n = len(sys.argv)
symb = False
if n > 1:
symb = True
rofiprint(CONFIG_FILE, symb=symb)