forked from tony/awesome-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.lua
63 lines (51 loc) · 1.51 KB
/
helpers.lua
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
-- {{{ Helper functions
function run_once(prg,arg_string,pname,screen)
if not prg then
do return nil end
end
if not pname then
pname = prg
end
if not arg_string then
awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
else
awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. " " .. arg_string .. ")",screen)
end
end
function dbg(vars)
local text = ""
if type(vars) == "table" then
for i=1, #vars do text = text .. vars[i] .. " | " end
elseif type(vars) == "string" then
text = vars
end
naughty.notify({ text = text, timeout = 0 })
end
function clean(string)
s = string.gsub(string, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
function file_exists(file)
local cmd = "/bin/bash -c 'if [ -e " .. file .. " ]; then echo true; fi;'"
local fh = io.popen(cmd)
s = clean(fh:read('*a'))
if s == 'true' then return true else return nil end
end
--% Find the path of an application, return nil of doesn't exist
----@ app (string) Text of the first parameter
----@ return string of app path, or nil (remember, only nil and false is false in lua)
function whereis_app(app)
local fh = io.popen('which ' .. app)
s = clean(fh:read('*a'))
if s == "" then return nil else return s end
return s
end
function require_safe(lib)
if file_exists(awful.util.getdir("config") .. '/' .. lib ..'.lua') or
file_exists(awful.util.getdir("config") .. '/' .. lib) then
require(lib)
end
end
-- }}}