-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.janet
56 lines (50 loc) · 1.84 KB
/
init.janet
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
#!/usr/bin/env janet
(import spork/json :as json)
(import ./src/save :prefix "")
(defn- read-from-file!
"Reads contents of file as a buffer."
[file-path]
(with [fl (file/open file-path)]
(var lines @[])
(loop [line :iterate (file/read fl :line)]
(array/push lines line))
(string/join lines)))
(defn attach-container-to-workspace
"Attach container(by id) to a specific workspace(by name)."
[workspace-name container-id]
(let [workspace-name (string/replace-all "\"" "'" workspace-name)
p (os/spawn @("i3-msg" (string "[con_id=" container-id "]"
" move workspace "
"\"" workspace-name "\""))
:p)]
(:wait p)))
(defn- move-workspace-to-output
"Move workspace(by name) to the specific output."
[workspace-name output]
(let [p (os/spawn @("i3-msg" (string "[workspace=" "\"" workspace-name "\"" "]"
" move workspace to output "
output))
:p)]
(:wait p)))
(defn- load-containers
"Load containers, by moving them to workspace and attaching workspace to the output."
[entry]
(let [[output workspaces] (kvs entry)]
(map (fn [{"containers" containers "name" name}]
(do (map (fn [{"id" container-id}]
(attach-container-to-workspace name container-id))
containers)
(move-workspace-to-output name output)))
workspaces)))
(defn load!
"Loades and parses a layout from file."
[]
(->> (read-from-file! "/tmp/i3-layout")
(json/decode)
(map load-containers)))
(defn main [& args]
(let [[_ action] (dyn :args)]
(case action
"--save" (save!)
"--load" (load!)
(print "Invalid action. Use \"--save\" or \"--load\""))))