mirror of
https://github.com/joshuarubin/go-sway
synced 2024-11-22 12:02:00 +01:00
add a function to traverse in nodes (#3)
This commit is contained in:
parent
7764779055
commit
470ebafea4
14
sway_test.go
14
sway_test.go
@ -148,3 +148,17 @@ func processFocus(ctx context.Context, client sway.Client, node *sway.Node) {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFocused(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client, err := sway.New(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tree, err := client.GetTree(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
f := tree.FocusedNode()
|
||||
printJSON(f)
|
||||
}
|
||||
|
10
types.go
10
types.go
@ -124,6 +124,14 @@ type Node struct {
|
||||
|
||||
// FocusedNode traverses the node tree and returns the focused node
|
||||
func (n *Node) FocusedNode() *Node {
|
||||
focusedNode := n.TraverseNodes(func(n *Node) bool {
|
||||
return n.Focused
|
||||
})
|
||||
return focusedNode
|
||||
}
|
||||
|
||||
// TraverseNodes returns the first Node matching the predicate
|
||||
func (n *Node) TraverseNodes(predicate func(*Node) bool) *Node {
|
||||
queue := []*Node{n}
|
||||
for len(queue) > 0 {
|
||||
n = queue[0]
|
||||
@ -133,7 +141,7 @@ func (n *Node) FocusedNode() *Node {
|
||||
continue
|
||||
}
|
||||
|
||||
if n.Focused {
|
||||
if predicate(n) {
|
||||
return n
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user