unfiltered walker tree

This commit is contained in:
2026-09-15 11:16:27 +01:00
parent 6c365e9cde
commit 4396a6f619
4 changed files with 252 additions and 175 deletions
+23 -63
View File
@@ -1,76 +1,36 @@
package tree
import (
"sort"
"strings"
"git.flo.fo/FLO/dirmd/internal/walker"
)
type Node struct {
Name string
IsDir bool
Children map[string]*Node
Name string
IsDir bool
Children map[string]*Node
Annotation string
Ref string
}
func Build(entries []walker.Entry) *Node {
root := &Node{IsDir: true, Children: make(map[string]*Node)}
for _, e := range entries {
parts := strings.Split(e.RelPath, "/")
cur := root
for i, part := range parts {
isLeaf := i == len(parts)-1
if _, ok := cur.Children[part]; !ok {
cur.Children[part] = &Node{Name: part, IsDir: !isLeaf, Children: make(map[string]*Node)}
func splitPath(relPath string) []string {
var parts []string
start := 0
for i := 0; i <= len(relPath); i++ {
if i == len(relPath) || relPath[i] == '/' || relPath[i] == '\\' {
if i > start {
parts = append(parts, relPath[start:i])
}
cur = cur.Children[part]
start = i + 1
}
}
return root
return parts
}
func Render(root *Node, rootName string) string {
var sb strings.Builder
sb.WriteString(rootName)
sb.WriteString("/\n")
renderNode(&sb, root, "")
return sb.String()
}
func renderNode(sb *strings.Builder, node *Node, prefix string) {
keys := make([]string, 0, len(node.Children))
for k := range node.Children {
keys = append(keys, k)
}
sort.Strings(keys)
for i, key := range keys {
child := node.Children[key]
isLast := i == len(keys)-1
if isLast {
sb.WriteString(prefix + "└── ")
} else {
sb.WriteString(prefix + "├── ")
func addPath(root *Node, rel string, isDir bool) *Node {
parts := splitPath(rel)
cur := root
for i, part := range parts {
if _, ok := cur.Children[part]; !ok {
cur.Children[part] = &Node{Name: part, IsDir: i < len(parts)-1, Children: make(map[string]*Node)}
}
sb.WriteString(child.Name)
if child.IsDir {
sb.WriteString("/")
}
sb.WriteString("\n")
if child.IsDir {
newPrefix := prefix
if isLast {
newPrefix += " "
} else {
newPrefix += "│ "
}
renderNode(sb, child, newPrefix)
}
cur = cur.Children[part]
}
cur.IsDir = isDir
return cur
}