MCPcopy Create free account
hub / github.com/chainreactors/spray / RenderTree

Method RenderTree

core/format.go:62–115  ·  view source on GitHub ↗

RenderTree renders the tree structure

(prefix string, isLast bool, color bool)

Source from the content-addressed store, hash-verified

60
61// RenderTree renders the tree structure
62func (tn *TreeNode) RenderTree(prefix string, isLast bool, color bool) string {
63 var sb strings.Builder
64
65 if tn.Name != "" {
66 connector := "├── "
67 if isLast {
68 connector = "└── "
69 }
70
71 sb.WriteString(prefix)
72 sb.WriteString(connector)
73
74 if tn.IsLeaf && tn.Result != nil {
75 // Leaf node with result
76 if color {
77 sb.WriteString(tn.Result.ColorString())
78 } else {
79 sb.WriteString(tn.Result.String())
80 }
81 } else {
82 // Directory node
83 sb.WriteString(tn.Name)
84 if !strings.HasSuffix(tn.Name, "/") {
85 sb.WriteString("/")
86 }
87 }
88 sb.WriteString("\n")
89 }
90
91 // Sort children for consistent output
92 var keys []string
93 for k := range tn.Children {
94 keys = append(keys, k)
95 }
96 sort.Strings(keys)
97
98 for i, key := range keys {
99 child := tn.Children[key]
100 isLastChild := i == len(keys)-1
101
102 newPrefix := prefix
103 if tn.Name != "" {
104 if isLast {
105 newPrefix += " "
106 } else {
107 newPrefix += "│ "
108 }
109 }
110
111 sb.WriteString(child.RenderTree(newPrefix, isLastChild, color))
112 }
113
114 return sb.String()
115}
116
117func Format(opts Option) {
118 var content []byte

Callers 1

formatTreeFunction · 0.95

Calls 2

ColorStringMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected