-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
152 lines (130 loc) · 3.4 KB
/
Copy pathrender.go
File metadata and controls
152 lines (130 loc) · 3.4 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
)
var (
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#7D56F4"))
dirStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00BFFF")).Bold(true)
fileStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#DDDDDD"))
sizeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500")).Bold(true)
pctStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#888888"))
barStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#7D56F4"))
errStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF5555"))
totalStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#50FA7B"))
treeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#555555"))
)
const barWidth = 20
type RenderOptions struct {
Top int
MaxDepth int
Min int64
Unit Unit
}
func Render(root string, res *ScanResult, ro RenderOptions) {
fmt.Println()
fmt.Println(titleStyle.Render("📁 " + root))
fmt.Println()
if len(res.Root.Children) == 0 {
fmt.Println(pctStyle.Render(T("render.empty")))
fmt.Println()
return
}
renderLevel(res.Root, ro, 1, "")
fmt.Println()
fmt.Printf(" %s %s %s\n",
totalStyle.Render(T("render.total")),
sizeStyle.Render(humanSizeU(res.Root.Size, ro.Unit)),
pctStyle.Render(T("render.files", res.Root.Count)),
)
if len(res.Errors) > 0 {
fmt.Println(errStyle.Render(T("render.inacc_warn", len(res.Errors))))
}
fmt.Println()
}
func renderLevel(parent *Node, ro RenderOptions, depth int, prefix string) {
children := parent.Children
if ro.Min > 0 {
filtered := make([]*Node, 0, len(children))
for _, c := range children {
if c.Size >= ro.Min {
filtered = append(filtered, c)
}
}
children = filtered
}
total := len(children)
limit := total
if ro.Top > 0 && ro.Top < limit {
limit = ro.Top
}
nameW := 0
for i := 0; i < limit; i++ {
if l := displayLen(children[i]); l > nameW {
nameW = l
}
}
if nameW > 40 {
nameW = 40
}
for i := 0; i < limit; i++ {
e := children[i]
last := i == limit-1
pct := 0.0
if parent.Size > 0 {
pct = float64(e.Size) / float64(parent.Size) * 100
}
connector := "├─ "
if last {
connector = "└─ "
}
name := e.Name
if e.IsDir {
name += "/"
}
runes := []rune(name)
if len(runes) > nameW {
name = string(runes[:nameW-1]) + "…"
}
styled := fileStyle.Render(name)
if e.IsDir {
styled = dirStyle.Render(name)
}
pad := strings.Repeat(" ", nameW-len([]rune(name))+1)
fmt.Printf(" %s%s%s%s %s %s\n",
treeStyle.Render(prefix+connector),
styled,
pad,
renderBar(pct),
sizeStyle.Render(fmt.Sprintf("%10s", humanSizeU(e.Size, ro.Unit))),
pctStyle.Render(fmt.Sprintf("%5.1f%%", pct)),
)
if e.IsDir && depth < ro.MaxDepth && len(e.Children) > 0 {
childPrefix := prefix + "│ "
if last {
childPrefix = prefix + " "
}
renderLevel(e, ro, depth+1, childPrefix)
}
}
if limit < total {
more := treeStyle.Render(prefix + " ")
fmt.Printf(" %s%s\n", more, pctStyle.Render(T("render.more_items", total-limit)))
}
}
func displayLen(n *Node) int {
l := len([]rune(n.Name))
if n.IsDir {
l++
}
return l
}
func renderBar(pct float64) string {
filled := int(pct / 100 * barWidth)
if filled > barWidth {
filled = barWidth
}
bar := strings.Repeat("█", filled) + strings.Repeat("░", barWidth-filled)
return barStyle.Render(bar)
}