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