fork/gdu

git clone https://git.julia.blue/fork/gdu

yurenchen000Feat: Show symlink target #260 (#612)1eaa266

subvolume
7.5 KiB321 linesraw
1package tui
2
3import (
4	"fmt"
5	"math"
6
7	"github.com/dundee/gdu/v5/internal/common"
8	"github.com/dundee/gdu/v5/pkg/fs"
9	"github.com/rivo/tview"
10)
11
12const (
13	blackOnWhite = "[black:white:-]"
14	whiteOnBlack = "[white:black:-]"
15
16	defaultColor     = "[-::]"
17	defaultColorBold = "[::b]"
18)
19
20// getUsagePart returns the percentage (0-100) that the given item's size or
21// usage represents relative to the provided maximum.
22func (ui *UI) getUsagePart(item fs.Item, maxUsage, maxSize int64, ignored bool) float64 {
23	if ignored {
24		return 0
25	}
26	if ui.ShowApparentSize {
27		if size := item.GetSize(); size > 0 {
28			return float64(size) / float64(maxSize) * 100.0
29		}
30	} else {
31		if usage := item.GetUsage(); usage > 0 {
32			return float64(usage) / float64(maxUsage) * 100.0
33		}
34	}
35	return 0
36}
37
38// formatUsagePercentage formats the numeric usage percentage shown next to the size bar.
39func formatUsagePercentage(part float64) string {
40	return fmt.Sprintf(" %5.1f%%", part)
41}
42
43func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignored bool) string {
44	partFloat := ui.getUsagePart(item, maxUsage, maxSize, ignored)
45	part := int(partFloat)
46
47	row := string(item.GetFlag())
48
49	numberColor := fmt.Sprintf(
50		"[%s::b]",
51		ui.resultRow.NumberColor,
52	)
53
54	if ui.UseColors && !marked && !ignored {
55		row += numberColor
56	} else {
57		row += defaultColorBold
58	}
59
60	if ui.ShowApparentSize {
61		row += fmt.Sprintf("%15s", ui.formatSize(item.GetSize(), false, true))
62	} else {
63		row += fmt.Sprintf("%15s", ui.formatSize(item.GetUsage(), false, true))
64	}
65
66	if ui.showBarPercentage {
67		row += formatUsagePercentage(partFloat)
68	}
69	if ui.useOldSizeBar {
70		row += " " + getUsageGraphOld(part) + " "
71	} else {
72		row += getUsageGraph(part)
73	}
74
75	if ui.showItemCount {
76		if ui.UseColors && !marked && !ignored {
77			row += numberColor
78		} else {
79			row += defaultColorBold
80		}
81
82		countToDisplay := item.GetItemCount()
83		if item.IsDir() {
84			countToDisplay--
85		}
86		row += fmt.Sprintf("%11s ", ui.formatCount(countToDisplay))
87	}
88
89	if ui.showMtime {
90		if ui.UseColors && !marked && !ignored {
91			row += numberColor
92		} else {
93			row += defaultColorBold
94		}
95		row += fmt.Sprintf(
96			"%s "+defaultColor,
97			item.GetMtime().Format("2006-01-02 15:04:05"),
98		)
99	}
100
101	if len(ui.markedRows) > 0 {
102		if marked {
103			row += string('✓')
104		} else {
105			row += " "
106		}
107		row += " "
108	}
109
110	// Display symlink name in cyan/aqua (like ls --color) and target
111	if name := ui.formatItemName(item, marked, ignored); name != "" {
112		return row + name
113	}
114
115	if item.IsDir() {
116		if ui.UseColors && !marked && !ignored {
117			row += fmt.Sprintf("[%s::b]/", ui.resultRow.DirectoryColor)
118		} else {
119			row += defaultColorBold + "/"
120		}
121	}
122	row += tview.Escape(item.GetName())
123
124	return row
125}
126
127// formatItemName returns formatted name for special item types (e.g. symlinks).
128// Returns empty string if the item has no special formatting.
129func (ui *UI) formatItemName(item fs.Item, marked, ignored bool) string {
130	if !ui.showSymlinkTarget {
131		return ""
132	}
133	si, ok := item.(fs.SymlinkItem)
134	if !ok {
135		return ""
136	}
137	target := si.GetSymlinkTarget()
138	if target == "" {
139		return ""
140	}
141
142	var name string
143	if ui.UseColors && !marked && !ignored {
144		name = "[aqua::b]" + tview.Escape(item.GetName())
145	} else {
146		name = tview.Escape(item.GetName())
147	}
148	return name + defaultColor + " -> " + tview.Escape(target)
149}
150
151// formatCollapsedRow formats a collapsed directory path for display
152func (ui *UI) formatCollapsedRow(collapsedPath *CollapsedPath, maxUsage, maxSize int64, marked, ignored bool) string {
153	// Use the deepest directory's stats for display
154	item := collapsedPath.DeepestDir
155
156	partFloat := ui.getUsagePart(item, maxUsage, maxSize, ignored)
157	part := int(partFloat)
158
159	row := string(item.GetFlag())
160
161	numberColor := fmt.Sprintf(
162		"[%s::b]",
163		ui.resultRow.NumberColor,
164	)
165
166	if ui.UseColors && !marked && !ignored {
167		row += numberColor
168	} else {
169		row += defaultColorBold
170	}
171
172	if ui.ShowApparentSize {
173		row += fmt.Sprintf("%15s", ui.formatSize(item.GetSize(), false, true))
174	} else {
175		row += fmt.Sprintf("%15s", ui.formatSize(item.GetUsage(), false, true))
176	}
177
178	if ui.showBarPercentage {
179		row += formatUsagePercentage(partFloat)
180	}
181	if ui.useOldSizeBar {
182		row += " " + getUsageGraphOld(part) + " "
183	} else {
184		row += getUsageGraph(part)
185	}
186
187	if ui.showItemCount {
188		if ui.UseColors && !marked && !ignored {
189			row += numberColor
190		} else {
191			row += defaultColorBold
192		}
193
194		countToDisplay := item.GetItemCount()
195		if item.IsDir() {
196			countToDisplay--
197		}
198		row += fmt.Sprintf("%11s ", ui.formatCount(countToDisplay))
199	}
200
201	if ui.showMtime {
202		if ui.UseColors && !marked && !ignored {
203			row += numberColor
204		} else {
205			row += defaultColorBold
206		}
207		row += fmt.Sprintf(
208			"%s "+defaultColor,
209			item.GetMtime().Format("2006-01-02 15:04:05"),
210		)
211	}
212
213	if len(ui.markedRows) > 0 {
214		if marked {
215			row += string('✓')
216		} else {
217			row += " "
218		}
219		row += " "
220	}
221
222	// Always display as directory with special formatting for collapsed path
223	if ui.UseColors && !marked && !ignored {
224		row += fmt.Sprintf("[%s::b]/", ui.resultRow.DirectoryColor)
225	} else {
226		row += defaultColorBold + "/"
227	}
228
229	// Display the collapsed path (e.g., "a/b/c")
230	row += tview.Escape(collapsedPath.DisplayName)
231	return row
232}
233
234func (ui *UI) formatSize(size int64, reverseColor, transparentBg bool) string {
235	var color string
236	if reverseColor {
237		if ui.UseColors {
238			color = fmt.Sprintf(
239				"[%s:%s:-]",
240				ui.footerTextColor,
241				ui.footerBackgroundColor,
242			)
243		} else {
244			color = blackOnWhite
245		}
246	} else {
247		if transparentBg {
248			color = defaultColor
249		} else {
250			color = whiteOnBlack
251		}
252	}
253
254	if formatted, ok := ui.FormatBlockSize(size); ok {
255		return formatted + color
256	}
257	if ui.UseSIPrefix {
258		return formatWithDecPrefix(size, color)
259	}
260	return formatWithBinPrefix(float64(size), color)
261}
262
263func (ui *UI) formatCount(count int64) string {
264	row := ""
265	color := defaultColor
266	count64 := float64(count)
267
268	switch {
269	case count64 >= common.G:
270		row += fmt.Sprintf("%.1f%sG", float64(count)/float64(common.G), color)
271	case count64 >= common.M:
272		row += fmt.Sprintf("%.1f%sM", float64(count)/float64(common.M), color)
273	case count64 >= common.K:
274		row += fmt.Sprintf("%.1f%sk", float64(count)/float64(common.K), color)
275	default:
276		row += fmt.Sprintf("%d%s", count, color)
277	}
278	return row
279}
280
281func formatWithBinPrefix(fsize float64, color string) string {
282	asize := math.Abs(fsize)
283
284	switch {
285	case asize >= common.Ei:
286		return fmt.Sprintf("%.1f%s EiB", fsize/common.Ei, color)
287	case asize >= common.Pi:
288		return fmt.Sprintf("%.1f%s PiB", fsize/common.Pi, color)
289	case asize >= common.Ti:
290		return fmt.Sprintf("%.1f%s TiB", fsize/common.Ti, color)
291	case asize >= common.Gi:
292		return fmt.Sprintf("%.1f%s GiB", fsize/common.Gi, color)
293	case asize >= common.Mi:
294		return fmt.Sprintf("%.1f%s MiB", fsize/common.Mi, color)
295	case asize >= common.Ki:
296		return fmt.Sprintf("%.1f%s KiB", fsize/common.Ki, color)
297	default:
298		return fmt.Sprintf("%d%s B", int64(fsize), color)
299	}
300}
301
302func formatWithDecPrefix(size int64, color string) string {
303	fsize := float64(size)
304	asize := math.Abs(fsize)
305	switch {
306	case asize >= common.E:
307		return fmt.Sprintf("%.1f%s EB", fsize/common.E, color)
308	case asize >= common.P:
309		return fmt.Sprintf("%.1f%s PB", fsize/common.P, color)
310	case asize >= common.T:
311		return fmt.Sprintf("%.1f%s TB", fsize/common.T, color)
312	case asize >= common.G:
313		return fmt.Sprintf("%.1f%s GB", fsize/common.G, color)
314	case asize >= common.M:
315		return fmt.Sprintf("%.1f%s MB", fsize/common.M, color)
316	case asize >= common.K:
317		return fmt.Sprintf("%.1f%s kB", fsize/common.K, color)
318	default:
319		return fmt.Sprintf("%d%s B", size, color)
320	}
321}