forked from nwhirschfeld/i3icons2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i3_utils.go
76 lines (60 loc) · 1.76 KB
/
i3_utils.go
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
package main
// stolen from github.com/mdirkse/i3ipc-go/blob/master/tree_utils.go
import (
"go.i3wm.org/i3/v4"
)
// Returns a slice of all descendent nodes.
func Descendents(tree i3.Tree, node_id int64) []*i3.Node {
node := tree.Root.FindChild(func(n *i3.Node) bool { return n.ID == i3.NodeID(node_id) })
if node == nil {
return nil
}
var collectDescendents func(*i3.Node, []*i3.Node) []*i3.Node
// Collects descendent nodes recursively
collectDescendents = func(n *i3.Node, collected []*i3.Node) []*i3.Node {
for i := range n.Nodes {
if n.Type != "dockarea" {
collected = append(collected, n.Nodes[i])
collected = collectDescendents(n.Nodes[i], collected)
}
}
for i := range n.FloatingNodes {
if n.Type != "dockarea" {
collected = append(collected, n.FloatingNodes[i])
collected = collectDescendents(n.FloatingNodes[i], collected)
}
}
return collected
}
return collectDescendents(node, nil)
}
// Returns nodes that has no children nodes (leaves).
func Leaves(tree i3.Tree, node_id int64) (leaves []*i3.Node) {
nodes := Descendents(tree, node_id)
for i := range nodes {
node := nodes[i]
if len(node.Nodes) == 0 && node.Type == "con" {
leaves = append(leaves, node)
}
}
return
}
// i3-msg -t get_workspaces doesn't fill IDs for the workspaces
// This function as a workaround
func GetWorkspaces(tree i3.Tree) []i3.Node {
predicate := func(node *i3.Node) bool {
return node.Type == "workspace" && node.Name != "__i3_scratch"
}
wss := []i3.Node{}
wss = AppendChild(wss, tree.Root, predicate)
return wss
}
func AppendChild(wss []i3.Node, n *i3.Node, predicate func(*i3.Node) bool) []i3.Node {
if predicate(n) {
wss = append(wss, *n)
}
for _, c := range n.Nodes {
wss = AppendChild(wss, c, predicate)
}
return wss
}