FindAll implemented
This commit is contained in:
parent
8345de3597
commit
489f32e0c3
31
find.go
Normal file
31
find.go
Normal 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
18
find_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user