-
Notifications
You must be signed in to change notification settings - Fork 0
/
yabai_helper.lua
88 lines (70 loc) · 2.03 KB
/
yabai_helper.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
78
79
80
81
82
83
84
85
86
87
88
local M = {}
local mt = { __index = M }
local log = hs.logger.new('yabai')
log.setLogLevel('debug')
local json_encode = hs.json.encode
local json_decode = hs.json.decode
local yabaiPath = '/opt/local/bin/yabai'
function M:start()
local task = self.deepest_task:start()
self.task = task
return self
end
function M:catch(argstr)
local task = hs.task.new(yabaiPath, nil, hs.fnutils.split(argstr, '%s'))
self.task:setCallback(function(exitCode, stdOut, stdErr) -- next task's callback
-- self.result = {
-- exitCode = exitCode,
-- stdOut = stdOut,
-- stdErr = stdErr
-- }
if exitCode ~= 0 then
task:start() -- execute catch's task
end
end)
self.task = task
return self
end
function M:next(argstr)
-- next task
local task = hs.task.new(yabaiPath, nil, hs.fnutils.split(argstr, '%s'))
self.task:setCallback(function(exitCode, stdOut, stdErr) -- default task's callback
-- self.result = {
-- exitCode = exitCode,
-- stdOut = stdOut,
-- stdErr = stdErr
-- }
if exitCode == 0 then -- execute next task
task:start()
end
end)
self.task = task
return self
end
function M:call(argstr)
local task = hs.task.new(yabaiPath, nil, hs.fnutils.split(argstr, '%s'))
return setmetatable({
deepest_task = task,
task = task,
result = nil, -- final result
}, mt)
end
function M:waitUntilFinished()
if not self.task then return end
self.task:setCallback(function(exitCode, stdOut, stdErr) -- default task's callback
local json
if stdOut and stdOut ~= '' then
json = json_decode(stdOut)
end
self.result = {
exitCode = exitCode,
stdOut = stdOut,
stdErr = stdErr,
json = json
}
-- log.df("exitCode = %d stdOut = %s, stdErr = %s", exitCode, stdOut, stdErr)
end)
self.task:waitUntilExit()
return self
end
return M