-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmenu.py
78 lines (50 loc) · 1.59 KB
/
dmenu.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
from subprocess import Popen, PIPE, STDOUT
from shlex import split
from base import *
__all__ = (
"run",
"select",
"inp",
"progs"
"password"
)
opt = "-fn -adobe-helvetica-bold-r-normal-*-25-180-100-100-p-138-iso8859-1"
bin = "dmenu %s" % opt
def progs():
cmd("dmenu_run %s" % opt)
def parse_opts(**kwargs):
return " ".join('-%s "%s"' % (key, value)
for key, value in kwargs.items())
def run(choices=None, **kwargs):
cmd = split("%s %s" % (bin, parse_opts(**kwargs)))
if choices is not None:
p = Popen(cmd, stdin=PIPE, stdout=PIPE)
else:
p = Popen(cmd, stdout=PIPE)
out, err = p.communicate()
if p.returncode != 0:
raise RuntimeError("None zero returncode!")
return out.decode()
def select(*args, **kwargs):
p = kwargs.pop("p", None)
choices = b"\n".join(key.encode("ascii") for key in kwargs.keys())
if len(args) > 0:
choices = \
b"\n".join((choices, b"\n".join(elem.encode("ascii")
for elem in args)))
key = run(choices=choices.strip(), p=p).strip("\n").strip()
return key
def inp(**kwargs):
cmd = "%s %s" % (bin, parse_opts(**kwargs))
p = Popen(split(cmd), stdin=PIPE, stdout=PIPE)
out, err = p.communicate()
p.stdin.close()
if p.returncode != 0:
raise RuntimeError("None zero returncode!")
return out.decode().strip("\n").strip()
def password(**kwargs):
kwargs.update({
"nf": "black",
"nb": "black"
})
return inp(**kwargs)