-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomnibox
executable file
·115 lines (102 loc) · 3.17 KB
/
omnibox
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
#!/usr/bin/python3
import sys
import os
import shlex
from shutil import which
from subprocess import Popen, PIPE
from collections import defaultdict
zoom = 1.2
if len(sys.argv) > 1:
zoom = float(sys.argv[1])
# if len(sys.argv
def is_url(string):
clean = string.strip()
if ' ' in clean:
return False
if len(clean.split(".")) > 1:
return True
return False
def url_format(search):
nomodify = search
replacements = [(" ", "%20"),
("!","%21"),
("#","%23"),
("$","%24"),
("%","%25"),
("&","%26"),
("'","%27"),
("(","%28"),
(")","%29"),
("*","%2A"),
("+","%2B"),
(",","%2C"),
("/","%2F"),
(":","%3A"),
(";","%3B"),
("=","%3D"),
("?","%3F"),
("@", "%40"),
("[","%5B"),
("]", "%5D")]
for r in replacements:
nomodify.replace(r[0], r[1])
return nomodify
def dmenu(list_strings):
fsize = 15
if zoom > 1.5:
fsize = 22
process = Popen(["sh", "-c", 'echo "{}" | dmenu -p "omnibox" -fn Input:pixelsize={}:antialias=true:autohint=true:style=Regular'.format("\n".join(list_strings), fsize)], stdout=PIPE)
exit_code = process.wait()
comm = process.communicate()
if exit_code == 1:
return
# list_returned = comm[0].decode('utf-8').split('\n')
# if len(list_returned) < 2:
# return None
# last = list_returned[-2].strip()
return comm[0].decode('utf-8').strip()
def run(command):
commands = command.split(" ")
os.execlp(commands[0], commands[0], *commands[1:])
def surf(string):
print(string)
if is_url(string):
os.execlp("surf", "surf", "-z", str(zoom), "-aA", string)
else:
search = "https://duckduckgo.com/?q={}&kae=d&ko=-1".format(url_format(string))
print(search)
os.execlp("surf", "surf", "-z", str(zoom), search)
## os.environ["BROWSER"] = "/usr/local/bin/surf"
## os.execlp("st", "st", "ddgr", "--colors", "bjdxxy", *shlex.split(string))
# GET list of runable programs ######
programs = set()
for path in os.environ["PATH"].split(":"):
for program in os.listdir(path):
programs.add(program)
programs = list(programs)
####################################
usin = ""
lines = []
with open("/home/varun/.recent", "r") as f:
lines = [l.strip().replace("'","") for l in f.readlines()]
weights = defaultdict(int)
for l in lines:
weights[l] += 1
sorted_lines = sorted(weights, key=weights.__getitem__)[::-1]
usin = dmenu(sorted_lines + programs)
# check if a command is actually entered
if not usin or len(usin) == 0:
sys.exit(0)
with open("/home/varun/.recent", "w") as f:
f.write(usin + "\n")
f.write("\n".join(lines[:1000]))
# check first characters
if usin[0] == "?":
surf(usin[1:])
elif usin[0] == "$":
run(usin[1:])
elif which(usin.split(" ")[0]) is not None:
run(usin)
else:
surf(usin)
sys.exit(0)