-
Notifications
You must be signed in to change notification settings - Fork 99
/
rofi.py
30 lines (24 loc) · 862 Bytes
/
rofi.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
import subprocess
def rofi(prompt, options, rofi_args=[], fuzzy=True):
optionstr = '\n'.join(option.replace('\n', ' ') for option in options)
args = ['rofi', '-sort', '-no-levenshtein-sort']
if fuzzy:
args += ['-matching', 'fuzzy']
args += ['-dmenu', '-p', prompt, '-format', 's', '-i']
args += rofi_args
args = [str(arg) for arg in args]
result = subprocess.run(args, input=optionstr, stdout=subprocess.PIPE, universal_newlines=True)
returncode = result.returncode
stdout = result.stdout.strip()
selected = stdout.strip()
try:
index = [opt.strip() for opt in options].index(selected)
except ValueError:
index = -1
if returncode == 0:
key = 0
elif returncode == 1:
key = -1
elif returncode > 9:
key = returncode - 9
return key, index, selected