-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitty-hintsconfig.py
72 lines (58 loc) · 2.24 KB
/
kitty-hintsconfig.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
import re
import os
import yaml
def config(file='~/.config/kitty/hints.yaml'):
file = os.path.expanduser(file)
if os.path.isfile(file):
with open(file, 'r') as f:
try:
return yaml.safe_load(f)
except yaml.YAMLError as exc:
print(f"=> {exc}")
else: # simple default config
return {
'links': [
{'regexp': r'((www|http:|https:)+[^\s]+[\w])', 'link': '{}'},
{'regexp': r'file://([^\s]*)', 'process': 'xdg-open {}'},
],
'markers': [
{'regexp': 'error', 'marker': 3},
{'regexp': 'warning', 'marker': 2},
{'regexp': 'info', 'marker': 1}
]
}
def config_links():
return config()['links']
def config_markers():
return config()['markers']
def mark(text, args, Mark, extra_cli_args, *a):
marks = [ m for m in _mark(text) ]
sorted_marks = sorted(marks, key=lambda m: m['start'])
return [Mark(idx, m['start'], m['end'], m['text'], m['data']) for idx, m in enumerate(sorted_marks)]
def _mark(text):
for rule in config_links():
for m in re.finditer(rule['regexp'], text):
start, end = m.span()
mark_text = m.group(1).replace('\n', '').replace('\0', '')
if 'link' in rule:
link = rule['link'].format(mark_text)
data = {'link': link}
if 'process' in rule:
process = rule['process'].format(mark_text)
data = {'process': process}
yield {'start': start, 'end': end, 'text': m.group(0), 'data': data}
def handle_result(args, data, target_window_id, boss, extra_cli_args, *a):
matches, groupdicts = [], []
for m, g in zip(data['match'], data['groupdicts']):
if m:
matches.append(m), groupdicts.append(g)
for match_data in groupdicts:
if 'link' in match_data:
boss.open_url(match_data['link'])
elif 'process' in match_data:
os.system(match_data['process'])
def marker(text):
for rule in config_markers():
for m in re.finditer(rule['regexp'], text, re.IGNORECASE):
start, end = m.span()
yield start, end, rule['marker']