diff --git a/find.go b/find.go new file mode 100644 index 0000000..34b20c0 --- /dev/null +++ b/find.go @@ -0,0 +1,31 @@ +package sway + +func (n Node) FindAll(condition func(n Node) bool) []Node { + nodes := make([]Node, 0, 10) + + if condition(n) { + nodes = append(nodes, n) + } + + for _, child := range n.Nodes { + nodes = append(nodes, child.FindAll(condition)...) + } + + for _, child := range n.FloatingNodes { + nodes = append(nodes, child.FindAll(condition)...) + } + + return nodes +} + +func IsOutput(n Node) bool { + return n.Type == NodeTypeOutput +} + +func IsWorkspace(n Node) bool { + return n.Type == NodeTypeWorkspace +} + +func IsContainer(n Node) bool { + return n.Type == NodeTypeCon +} diff --git a/find_test.go b/find_test.go new file mode 100644 index 0000000..d1fd432 --- /dev/null +++ b/find_test.go @@ -0,0 +1,18 @@ +package sway + +import ( + "context" + "fmt" + "testing" +) + +func TestFind(t *testing.T) { + root, err := GetTree(context.Background()) + if err != nil { + t.Fatal(err) + } + + for _, node := range root.FindAll(IsWorkspace) { + fmt.Println(node.Type, node.Name) + } +}