From 489f32e0c3af546b493de368ed4b69ee54bacd1f Mon Sep 17 00:00:00 2001 From: milarin Date: Sat, 7 Oct 2023 16:36:43 +0200 Subject: [PATCH] FindAll implemented --- find.go | 31 +++++++++++++++++++++++++++++++ find_test.go | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 find.go create mode 100644 find_test.go 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) + } +}