render fs tree
This commit is contained in:
+6
-1
@@ -28,6 +28,11 @@ func walkFileSystemAndBuildTree(rootPath string, node *FsNode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
|
// dot folders and files skipped
|
||||||
|
if len(entry.Name()) > 0 && entry.Name()[0] == '.' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
childPath := filepath.Join(rootPath, entry.Name())
|
childPath := filepath.Join(rootPath, entry.Name())
|
||||||
|
|
||||||
var nodeType FsNodeType
|
var nodeType FsNodeType
|
||||||
@@ -42,7 +47,7 @@ func walkFileSystemAndBuildTree(rootPath string, node *FsNode) error {
|
|||||||
path: childPath,
|
path: childPath,
|
||||||
children: make([]*FsNode, 0),
|
children: make([]*FsNode, 0),
|
||||||
parent: node,
|
parent: node,
|
||||||
expanded: false,
|
expanded: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
node.children = append(node.children, childNode)
|
node.children = append(node.children, childNode)
|
||||||
|
|||||||
+29
-2
@@ -11,6 +11,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"mend/utils"
|
"mend/utils"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// enums for node types
|
// enums for node types
|
||||||
@@ -72,7 +73,7 @@ func (t *FsTree) CreateNode(parent *FsNode, name string, nodeType FsNodeType) er
|
|||||||
path: filepath.Join(parent.path, name),
|
path: filepath.Join(parent.path, name),
|
||||||
children: make([]*FsNode, 0),
|
children: make([]*FsNode, 0),
|
||||||
parent: parent,
|
parent: parent,
|
||||||
expanded: false,
|
expanded: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
parent.children = append(parent.children, newNode)
|
parent.children = append(parent.children, newNode)
|
||||||
@@ -92,7 +93,6 @@ func (t *FsTree) DeleteNode(node *FsNode) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToggleExpand toggles the expanded state of a node
|
|
||||||
func (t *FsTree) ToggleExpand(node *FsNode) error {
|
func (t *FsTree) ToggleExpand(node *FsNode) error {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return errors.New("node cannot be nil")
|
return errors.New("node cannot be nil")
|
||||||
@@ -104,3 +104,30 @@ func (t *FsTree) ToggleExpand(node *FsNode) error {
|
|||||||
node.expanded = !node.expanded
|
node.expanded = !node.expanded
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *FsTree) Render() string {
|
||||||
|
builder := &strings.Builder{}
|
||||||
|
t.renderNode(t.root, 0, builder)
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *FsTree) renderNode(node *FsNode, depth int, builder *strings.Builder) {
|
||||||
|
if node == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
indent := strings.Repeat(" ", depth)
|
||||||
|
icon := "-"
|
||||||
|
if node.nodeType == FolderNode {
|
||||||
|
icon = "> "
|
||||||
|
}
|
||||||
|
|
||||||
|
line := indent + icon + " " + node.FileName() + "\n"
|
||||||
|
builder.WriteString(line)
|
||||||
|
|
||||||
|
if node.expanded {
|
||||||
|
for _, child := range node.children {
|
||||||
|
t.renderNode(child, depth+1, builder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func createModel() model {
|
|||||||
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
||||||
|
|
||||||
return model{
|
return model{
|
||||||
width: 20, // char count
|
width: 30, // char count
|
||||||
tree: nil,
|
tree: nil,
|
||||||
spinner: s,
|
spinner: s,
|
||||||
loading: true,
|
loading: true,
|
||||||
@@ -67,7 +67,7 @@ type treeLoadedMsg struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadTreeCmd() tea.Msg {
|
func loadTreeCmd() tea.Msg {
|
||||||
time.Sleep(3 * time.Second) // delay sim
|
time.Sleep(1 * time.Second) // delay sim
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,14 +107,16 @@ func (m model) View() string {
|
|||||||
|
|
||||||
left := lipgloss.NewStyle().
|
left := lipgloss.NewStyle().
|
||||||
Width(m.width).
|
Width(m.width).
|
||||||
Render("section1")
|
Render(m.tree.Render())
|
||||||
|
|
||||||
right := lipgloss.NewStyle().
|
right := lipgloss.NewStyle().
|
||||||
Render("section2")
|
Render("section2")
|
||||||
|
|
||||||
|
leftHeight := lipgloss.Height(left)
|
||||||
|
|
||||||
divider := lipgloss.NewStyle().
|
divider := lipgloss.NewStyle().
|
||||||
Width(1).
|
Width(1).
|
||||||
Height(5).
|
Height(leftHeight).
|
||||||
Background(styles.Primary).
|
Background(styles.Primary).
|
||||||
Render("") // everything is a string in tui
|
Render("") // everything is a string in tui
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user