[GH-ISSUE #187] How to visit the children(files) of a node? #149

Closed
opened 2026-03-04 01:02:23 +03:00 by kerem · 2 comments
Owner

Originally created by @thedevsaddam on GitHub (Nov 20, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/187

I really appreciate your work 👍 . Using the package to build a terminal GUI app.
Need to visit all the directory and files inside the directory. Basically, I want to take action against the directory or file. How can I open a modal on a file or directory using a Keyboard shortcut like (left arrow to Collapse, right arrow to Expand, Up/Down for up-down tree view, key A to take action etc)?


// Demo code for the TreeView primitive.
package main

import (
	"io/ioutil"
	"log"
	"os/user"
	"path/filepath"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

// Show a navigable tree view of the current directory.
func main() {
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	rootDir := usr.HomeDir
	root := tview.NewTreeNode(rootDir).
		SetColor(tcell.ColorRed)
	tree := tview.NewTreeView().
		SetRoot(root).
		SetCurrentNode(root)

	// 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)
		if err != nil {
			panic(err)
		}
		for _, file := range files {
			node := tview.NewTreeNode(file.Name()).
				SetReference(filepath.Join(path, file.Name())).
				SetSelectable(file.IsDir())
			if file.IsDir() {
				node.SetColor(tcell.ColorGreen)
			}
			target.AddChild(node)
		}
	}

	// Add the current directory to the root node.
	add(root, rootDir)

	// If a directory was selected, open it.
	tree.SetSelectedFunc(func(node *tview.TreeNode) {
		reference := node.GetReference()
		if reference == nil {
			return // Selecting the root node does nothing.
		}
		children := node.GetChildren()
		if len(children) == 0 {
			// Load and show files in this directory.
			path := reference.(string)
			add(node, path)
		} else {
			// Collapse if visible, expand if collapsed.
			node.SetExpanded(!node.IsExpanded())
		}
	})

	if err := tview.NewApplication().SetRoot(tree, true).Run(); err != nil {
		panic(err)
	}
}

Thanks again for your work 👍

Originally created by @thedevsaddam on GitHub (Nov 20, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/187 I really appreciate your work 👍 . Using the package to build a terminal GUI app. Need to visit all the directory and files inside the directory. Basically, I want to take action against the directory or file. How can I open a modal on a file or directory using a Keyboard shortcut like (left arrow to Collapse, right arrow to Expand, Up/Down for up-down tree view, key A to take action etc)? ```go // Demo code for the TreeView primitive. package main import ( "io/ioutil" "log" "os/user" "path/filepath" "github.com/gdamore/tcell" "github.com/rivo/tview" ) // Show a navigable tree view of the current directory. func main() { usr, err := user.Current() if err != nil { log.Fatal(err) } rootDir := usr.HomeDir root := tview.NewTreeNode(rootDir). SetColor(tcell.ColorRed) tree := tview.NewTreeView(). SetRoot(root). SetCurrentNode(root) // 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) if err != nil { panic(err) } for _, file := range files { node := tview.NewTreeNode(file.Name()). SetReference(filepath.Join(path, file.Name())). SetSelectable(file.IsDir()) if file.IsDir() { node.SetColor(tcell.ColorGreen) } target.AddChild(node) } } // Add the current directory to the root node. add(root, rootDir) // If a directory was selected, open it. tree.SetSelectedFunc(func(node *tview.TreeNode) { reference := node.GetReference() if reference == nil { return // Selecting the root node does nothing. } children := node.GetChildren() if len(children) == 0 { // Load and show files in this directory. path := reference.(string) add(node, path) } else { // Collapse if visible, expand if collapsed. node.SetExpanded(!node.IsExpanded()) } }) if err := tview.NewApplication().SetRoot(tree, true).Run(); err != nil { panic(err) } } ``` Thanks again for your work 👍
kerem closed this issue 2026-03-04 01:02:24 +03:00
Author
Owner

@rivo commented on GitHub (Nov 26, 2018):

As you've probably seen in the demo code, the Up/Down arrow keys as well as the Enter key are already implemented. Up/Left will navigate up, Down/Right will navigate down. Enter will fire the "Selected" event.

If you want to change the meaning of the keys or add your own shortcuts, you'll want to use SetInputCapture(). As TreeView inherits from Box, it will work there as well.

Let me know if this is what you are looking for.

<!-- gh-comment-id:441575383 --> @rivo commented on GitHub (Nov 26, 2018): As you've probably seen in the demo code, the Up/Down arrow keys as well as the Enter key are already implemented. Up/Left will navigate up, Down/Right will navigate down. Enter will fire the "Selected" event. If you want to change the meaning of the keys or add your own shortcuts, you'll want to use [`SetInputCapture()`](https://godoc.org/github.com/rivo/tview#Box.SetInputCapture). As `TreeView` inherits from `Box`, it will work there as well. Let me know if this is what you are looking for.
Author
Owner

@thedevsaddam commented on GitHub (Nov 26, 2018):

Yeah it works 👍 Thank you

<!-- gh-comment-id:441608295 --> @thedevsaddam commented on GitHub (Nov 26, 2018): Yeah it works 👍 Thank you
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#149
No description provided.