diff --git a/demos/treeview/main.go b/demos/treeview/main.go index ee81d4a..36066c2 100644 --- a/demos/treeview/main.go +++ b/demos/treeview/main.go @@ -2,7 +2,7 @@ package main import ( - "io/ioutil" + "os" "path/filepath" "github.com/gdamore/tcell/v2" @@ -21,7 +21,7 @@ func main() { // A helper function which adds the files and directories of the given path // to the given target node. add := func(target *tview.TreeNode, path string) { - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { panic(err) } diff --git a/treeview.go b/treeview.go index ad491e3..c473e0f 100644 --- a/treeview.go +++ b/treeview.go @@ -362,6 +362,35 @@ func (t *TreeView) GetCurrentNode() *TreeNode { return t.currentNode } +// GetPath returns all nodes located on the path from the root to the given +// node, including the root and the node itself. If there is no root node, nil +// is returned. If there are multiple paths to the node, a random one is chosen +// and returned. +func (t *TreeView) GetPath(node *TreeNode) []*TreeNode { + if t.root == nil { + return nil + } + + var f func(current *TreeNode, path []*TreeNode) []*TreeNode + f = func(current *TreeNode, path []*TreeNode) []*TreeNode { + if current == node { + return path + } + + for _, child := range current.children { + newPath := make([]*TreeNode, len(path), len(path)+1) + copy(newPath, path) + if p := f(child, append(newPath, child)); p != nil { + return p + } + } + + return nil + } + + return f(t.root, []*TreeNode{t.root}) +} + // SetTopLevel sets the first tree level that is visible with 0 referring to the // root, 1 to the root's child nodes, and so on. Nodes above the top level are // not displayed.