fork/gdu

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

Julia RyanDan's preferred ignore behavior04c9958

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