FindAll implemented

This commit is contained in:
milarin 2023-10-07 16:36:43 +02:00
parent 8345de3597
commit 489f32e0c3
2 changed files with 49 additions and 0 deletions

31
find.go Normal file
View File

@ -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
}

18
find_test.go Normal file
View File

@ -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)
}
}