-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.go
More file actions
125 lines (109 loc) · 3.05 KB
/
tree.go
File metadata and controls
125 lines (109 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package resolve
import (
"strings"
)
// TreeLine is a parsed line from a text tree.
type TreeLine struct {
Depth int
Content string
}
// TreeOptions configures how tree lines are parsed.
type TreeOptions struct {
// Prefixes are the tree-drawing characters that indicate a child node.
// e.g. "├── ", "└── ", "+- ", "\- "
Prefixes []string
// Continuations are the tree-drawing characters that indicate depth continuation.
// e.g. "│ ", "| ", "│ "
Continuations []string
}
// BoxDrawingOptions returns TreeOptions for Unicode box-drawing trees (├── └── │).
func BoxDrawingOptions() TreeOptions {
return TreeOptions{
Prefixes: []string{"├── ", "└── "},
Continuations: []string{"│ ", " "},
}
}
// ParseTreeLines reads indented tree output and returns depth + content for each line.
func ParseTreeLines(lines []string, opts TreeOptions) []TreeLine {
var result []TreeLine
for _, line := range lines {
if line == "" {
continue
}
depth := 0
remaining := line
for {
found := false
for _, cont := range opts.Continuations {
if strings.HasPrefix(remaining, cont) {
depth++
remaining = remaining[len(cont):]
found = true
break
}
}
if !found {
break
}
}
for _, prefix := range opts.Prefixes {
if strings.HasPrefix(remaining, prefix) {
remaining = remaining[len(prefix):]
break
}
}
content := strings.TrimRight(remaining, " \t\r\n")
if content == "" {
continue
}
result = append(result, TreeLine{Depth: depth, Content: content})
}
return result
}
// TabContentParser splits tab-separated "name\tversion" content for BuildTree.
// Used by parsers that encode name and version as a tab-delimited pair.
func TabContentParser(content string) (string, string, bool) {
name, version, ok := strings.Cut(content, "\t")
if !ok {
return "", "", false
}
return name, version, true
}
// BuildTree takes tree lines and a content-parser function, and builds a []*Dep tree.
// The contentParser receives the content string and returns (name, version, deps-placeholder).
// Deps is set to non-nil empty slice to indicate tree structure is available.
func BuildTree(lines []TreeLine, ecosystem string, contentParser func(string) (string, string, bool)) []*Dep {
if len(lines) == 0 {
return nil
}
type stackEntry struct {
dep *Dep
depth int
}
var roots []*Dep
var stack []stackEntry
for _, line := range lines {
name, version, ok := contentParser(line.Content)
if !ok {
continue
}
dep := &Dep{
PURL: MakePURL(ecosystem, name, version),
Name: name,
Version: version,
Deps: []*Dep{}, // non-nil to indicate tree structure
}
// Pop stack entries that are at the same depth or deeper
for len(stack) > 0 && stack[len(stack)-1].depth >= line.Depth {
stack = stack[:len(stack)-1]
}
if len(stack) == 0 {
roots = append(roots, dep)
} else {
parent := stack[len(stack)-1].dep
parent.Deps = append(parent.Deps, dep)
}
stack = append(stack, stackEntry{dep: dep, depth: line.Depth})
}
return roots
}