fork/gdu

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

Daniel Mildefeat: allow scanning multiple directories (#653)34f3996

master
9.2 KiB336 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// rowMaxima holds the denominators the bars of one directory listing are
21// scaled against. Which field is used depends on the displayed size metric and
22// on which bar is being drawn. Each field is either the sum over the sibling
23// items or, with ShowRelativeSize, the largest sibling.
24type rowMaxima struct {
25	usage int64
26	size  int64
27	count int64
28}
29
30// getUsagePart returns the percentage (0-100) that the given item's size or
31// usage represents relative to the provided maximum.
32func (ui *UI) getUsagePart(item fs.Item, maxima rowMaxima, ignored bool) float64 {
33	if ignored {
34		return 0
35	}
36	if ui.ShowApparentSize {
37		if size := item.GetSize(); size > 0 {
38			return float64(size) / float64(maxima.size) * 100.0
39		}
40	} else {
41		if usage := item.GetUsage(); usage > 0 {
42			return float64(usage) / float64(maxima.usage) * 100.0
43		}
44	}
45	return 0
46}
47
48// getCountPart returns the percentage (0-100) that the given item's displayed
49// item count represents relative to the provided maximum.
50func (ui *UI) getCountPart(item fs.Item, maxima rowMaxima, ignored bool) float64 {
51	if ignored || maxima.count == 0 {
52		return 0
53	}
54	if count := fs.DisplayedItemCount(item); count > 0 {
55		return float64(count) / float64(maxima.count) * 100.0
56	}
57	return 0
58}
59
60// formatUsagePercentage formats the numeric usage percentage shown next to the size bar.
61func formatUsagePercentage(part float64) string {
62	return fmt.Sprintf(" %5.1f%%", part)
63}
64
65// formatBar renders a single bar filled to the given percentage, in whichever
66// bar style is configured.
67func (ui *UI) formatBar(part int) string {
68	if ui.useOldSizeBar {
69		return " " + getUsageGraphOld(part) + " "
70	}
71	return getUsageGraph(part)
72}
73
74// Visible widths of the columns rendered by formatColumns. The numeric columns
75// are padded to a wider format verb than these because formatSize and
76// formatCount embed a color tag, which is counted by the verb but occupies no
77// screen columns.
78const (
79	flagColumnWidth       = 1
80	sizeColumnWidth       = 10
81	percentageColumnWidth = 7
82	itemCountColumnWidth  = 7
83	mtimeColumnWidth      = 20
84	markedColumnWidth     = 2
85	barColumnWidth        = 12
86	oldBarColumnWidth     = 14
87)
88
89// columnsWidth returns the number of screen columns formatColumns occupies, so
90// that rows which have no columns of their own (the "/.." entry) can be padded
91// to line up their name with the rest of the listing.
92func (ui *UI) columnsWidth() int {
93	barWidth := barColumnWidth
94	if ui.useOldSizeBar {
95		barWidth = oldBarColumnWidth
96	}
97
98	width := flagColumnWidth + sizeColumnWidth + barWidth
99	if ui.showBarPercentage {
100		width += percentageColumnWidth
101	}
102	if ui.showItemCount {
103		width += itemCountColumnWidth
104		if ui.showItemCountBar {
105			width += barWidth
106		}
107	}
108	if ui.showMtime {
109		width += mtimeColumnWidth
110	}
111	if len(ui.markedRows) > 0 {
112		width += markedColumnWidth
113	}
114	return width
115}
116
117// formatColumns renders every column of a row except the trailing name: the
118// type flag, the size, the size bar, and the optional percentage, item count,
119// item count bar, mtime and marked columns.
120//
121// statsItem is the item the numbers are taken from, which is not always the
122// item whose name ends up on the row: a collapsed path displays the name of a
123// whole chain of directories but the stats of its deepest one.
124func (ui *UI) formatColumns(statsItem fs.Item, maxima rowMaxima, marked, ignored bool) string {
125	// numberPrefix is the color tag introducing a numeric column.
126	numberPrefix := func() string {
127		if ui.UseColors && !marked && !ignored {
128			return fmt.Sprintf("[%s::b]", ui.resultRow.NumberColor)
129		}
130		return defaultColorBold
131	}
132
133	row := string(statsItem.GetFlag()) + numberPrefix()
134
135	if ui.ShowApparentSize {
136		row += fmt.Sprintf("%15s", ui.formatSize(statsItem.GetSize(), false, true))
137	} else {
138		row += fmt.Sprintf("%15s", ui.formatSize(statsItem.GetUsage(), false, true))
139	}
140
141	usagePart := ui.getUsagePart(statsItem, maxima, ignored)
142	if ui.showBarPercentage {
143		row += formatUsagePercentage(usagePart)
144	}
145	row += ui.formatBar(int(usagePart))
146
147	if ui.showItemCount {
148		row += numberPrefix()
149		row += fmt.Sprintf("%11s ", ui.formatCount(fs.DisplayedItemCount(statsItem)))
150
151		if ui.showItemCountBar {
152			row += ui.formatBar(int(ui.getCountPart(statsItem, maxima, ignored)))
153		}
154	}
155
156	if ui.showMtime {
157		row += numberPrefix()
158		row += fmt.Sprintf(
159			"%s "+defaultColor,
160			statsItem.GetMtime().Format("2006-01-02 15:04:05"),
161		)
162	}
163
164	if len(ui.markedRows) > 0 {
165		if marked {
166			row += string('✓')
167		} else {
168			row += " "
169		}
170		row += " "
171	}
172
173	return row
174}
175
176// dirRowStyle returns the styling that introduces a directory name in a row.
177func (ui *UI) dirRowStyle(marked, ignored bool) string {
178	if ui.UseColors && !marked && !ignored {
179		return fmt.Sprintf("[%s::b]", ui.resultRow.DirectoryColor)
180	}
181	return defaultColorBold
182}
183
184func (ui *UI) formatFileRow(item fs.Item, maxima rowMaxima, marked, ignored bool) string {
185	row := ui.formatColumns(item, maxima, marked, ignored)
186
187	// Display symlink name in cyan/aqua (like ls --color) and target
188	if name := ui.formatItemName(item, marked, ignored); name != "" {
189		return row + name
190	}
191
192	// Scanned roots are labelled with their absolute path, which already reads
193	// as a path and must not be given another leading separator.
194	if ui.atVirtualRoot() {
195		return row + ui.dirRowStyle(marked, ignored) + tview.Escape(item.GetPath())
196	}
197
198	if item.IsDir() {
199		row += ui.dirRowStyle(marked, ignored) + "/"
200	}
201	row += tview.Escape(item.GetName())
202
203	return row
204}
205
206// formatItemName returns formatted name for special item types (e.g. symlinks).
207// Returns empty string if the item has no special formatting.
208func (ui *UI) formatItemName(item fs.Item, marked, ignored bool) string {
209	if !ui.showSymlinkTarget {
210		return ""
211	}
212	si, ok := item.(fs.SymlinkItem)
213	if !ok {
214		return ""
215	}
216	target := si.GetSymlinkTarget()
217	if target == "" {
218		return ""
219	}
220
221	var name string
222	if ui.UseColors && !marked && !ignored {
223		name = "[aqua::b]" + tview.Escape(item.GetName())
224	} else {
225		name = tview.Escape(item.GetName())
226	}
227	return name + defaultColor + " -> " + tview.Escape(target)
228}
229
230// formatCollapsedRow formats a collapsed directory path for display
231func (ui *UI) formatCollapsedRow(
232	collapsedPath *CollapsedPath, maxima rowMaxima, marked, ignored bool,
233) string {
234	// Use the deepest directory's stats for display
235	row := ui.formatColumns(collapsedPath.DeepestDir, maxima, marked, ignored)
236
237	// Always display as directory with special formatting for collapsed path
238	row += ui.dirRowStyle(marked, ignored)
239	// a collapsed scanned root already starts from an absolute path
240	if !ui.atVirtualRoot() {
241		row += "/"
242	}
243
244	// Display the collapsed path (e.g., "a/b/c")
245	row += tview.Escape(collapsedPath.DisplayName)
246	return row
247}
248
249func (ui *UI) formatSize(size int64, reverseColor, transparentBg bool) string {
250	var color string
251	if reverseColor {
252		if ui.UseColors {
253			color = fmt.Sprintf(
254				"[%s:%s:-]",
255				ui.footerTextColor,
256				ui.footerBackgroundColor,
257			)
258		} else {
259			color = blackOnWhite
260		}
261	} else {
262		if transparentBg {
263			color = defaultColor
264		} else {
265			color = whiteOnBlack
266		}
267	}
268
269	if formatted, ok := ui.FormatBlockSize(size); ok {
270		return formatted + color
271	}
272	if ui.UseSIPrefix {
273		return formatWithDecPrefix(size, color)
274	}
275	return formatWithBinPrefix(float64(size), color)
276}
277
278func (ui *UI) formatCount(count int64) string {
279	row := ""
280	color := defaultColor
281	count64 := float64(count)
282
283	switch {
284	case count64 >= common.G:
285		row += fmt.Sprintf("%.1f%sG", float64(count)/float64(common.G), color)
286	case count64 >= common.M:
287		row += fmt.Sprintf("%.1f%sM", float64(count)/float64(common.M), color)
288	case count64 >= common.K:
289		row += fmt.Sprintf("%.1f%sk", float64(count)/float64(common.K), color)
290	default:
291		row += fmt.Sprintf("%d%s", count, color)
292	}
293	return row
294}
295
296func formatWithBinPrefix(fsize float64, color string) string {
297	asize := math.Abs(fsize)
298
299	switch {
300	case asize >= common.Ei:
301		return fmt.Sprintf("%.1f%s EiB", fsize/common.Ei, color)
302	case asize >= common.Pi:
303		return fmt.Sprintf("%.1f%s PiB", fsize/common.Pi, color)
304	case asize >= common.Ti:
305		return fmt.Sprintf("%.1f%s TiB", fsize/common.Ti, color)
306	case asize >= common.Gi:
307		return fmt.Sprintf("%.1f%s GiB", fsize/common.Gi, color)
308	case asize >= common.Mi:
309		return fmt.Sprintf("%.1f%s MiB", fsize/common.Mi, color)
310	case asize >= common.Ki:
311		return fmt.Sprintf("%.1f%s KiB", fsize/common.Ki, color)
312	default:
313		return fmt.Sprintf("%d%s B", int64(fsize), color)
314	}
315}
316
317func formatWithDecPrefix(size int64, color string) string {
318	fsize := float64(size)
319	asize := math.Abs(fsize)
320	switch {
321	case asize >= common.E:
322		return fmt.Sprintf("%.1f%s EB", fsize/common.E, color)
323	case asize >= common.P:
324		return fmt.Sprintf("%.1f%s PB", fsize/common.P, color)
325	case asize >= common.T:
326		return fmt.Sprintf("%.1f%s TB", fsize/common.T, color)
327	case asize >= common.G:
328		return fmt.Sprintf("%.1f%s GB", fsize/common.G, color)
329	case asize >= common.M:
330		return fmt.Sprintf("%.1f%s MB", fsize/common.M, color)
331	case asize >= common.K:
332		return fmt.Sprintf("%.1f%s kB", fsize/common.K, color)
333	default:
334		return fmt.Sprintf("%d%s B", size, color)
335	}
336}