forked from zzamboni/dot-hammerspoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomh-lib.lua
77 lines (68 loc) · 2.16 KB
/
omh-lib.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
omh={}
-- Some useful global variables
hostname = hs.host.localizedName()
logger = hs.logger.new('oh-my-hs')
hs_config_dir = os.getenv("HOME") .. "/.hammerspoon/"
-- Display a notification
function notify(title, message)
hs.notify.new({title=title, informativeText=message}):send()
end
-- Algorithm to choose whether white/black as the most contrasting to a given
-- color, from http://gamedev.stackexchange.com/a/38561/73496
function chooseContrastingColor(c)
local L = 0.2126*(c.red*c.red) + 0.7152*(c.green*c.green) + 0.0722*(c.blue*c.blue)
local black = { ["red"]=0.000,["green"]=0.000,["blue"]=0.000,["alpha"]=1 }
local white = { ["red"]=1.000,["green"]=1.000,["blue"]=1.000,["alpha"]=1 }
if L>0.5 then
return black
else
return white
end
end
-- Return the sorted keys of a table
function sortedkeys(tab)
local keys={}
-- Create sorted list of keys
for k,v in pairs(tab) do table.insert(keys, k) end
table.sort(keys)
return keys
end
-- Capture command output
-- From http://stackoverflow.com/a/326715/5562
-- function capture(cmd, raw)
-- local f = assert(io.popen(cmd, 'r'))
-- local s = assert(f:read('*a'))
-- f:close()
-- if raw then return s end
-- s = string.gsub(s, '^%s+', '')
-- s = string.gsub(s, '%s+$', '')
-- s = string.gsub(s, '[\n\r]+', ' ')
-- return s
-- end
-- Reimplemented version of capture() because sometimes Lua
-- fails with "interrupted system call" when using io.popen() on OS X
-- This is ugly, don't use it! Better use hs.task
function omh.capture(cmd, raw)
local tmpfile = os.tmpname()
os.execute(cmd .. ">" .. tmpfile)
local f=io.open(tmpfile)
local s=f:read("*a")
f:close()
os.remove(tmpfile)
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
-- Bind a key, simply a bridge between the OMH config format and hs.hotkey.bind
function omh.bind(keyspec, fun)
hs.hotkey.bind(keyspec[1], keyspec[2], fun)
end
-- Sleep for (possibly fractional) number of seconds
local clock = os.clock
function omh.sleep(n) -- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
return omh