Total Lines | 56 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package tree |
||
2 | |||
3 | import ( |
||
4 | "sort" |
||
5 | "strings" |
||
6 | |||
7 | gotree "github.com/DiSiqueira/GoTree" |
||
8 | "github.com/stefanoj3/dirstalk/pkg/scan" |
||
9 | ) |
||
10 | |||
11 | func NewResultTreeProducer() ResultTreeProducer { |
||
12 | return ResultTreeProducer{} |
||
13 | } |
||
14 | |||
15 | type ResultTreeProducer struct{} |
||
16 | |||
17 | func (s ResultTreeProducer) String(results []scan.Result) string { |
||
18 | sort.Slice(results, func(i, j int) bool { |
||
19 | return results[i].Target.Path < results[j].Target.Path |
||
20 | }) |
||
21 | |||
22 | root := gotree.New("/") |
||
23 | |||
24 | for _, r := range results { |
||
25 | currentBranch := root |
||
26 | |||
27 | parts := strings.Split(r.URL.Path, "/") |
||
28 | for _, p := range parts { |
||
29 | if len(p) == 0 { |
||
30 | continue |
||
31 | } |
||
32 | |||
33 | found := false |
||
34 | |||
35 | for _, item := range currentBranch.Items() { |
||
36 | if item.Text() != p { |
||
37 | continue |
||
38 | } |
||
39 | |||
40 | currentBranch = item |
||
41 | found = true |
||
42 | |||
43 | break |
||
44 | } |
||
45 | |||
46 | if found { |
||
47 | continue |
||
48 | } |
||
49 | |||
50 | newTree := gotree.New(p) |
||
51 | currentBranch.AddTree(newTree) |
||
52 | currentBranch = newTree |
||
53 | } |
||
54 | } |
||
55 | |||
56 | return root.Print() |
||
57 | } |
||
58 |