fork/gdu

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

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

master
15.5 KiB719 linesraw
1package stdout
2
3import (
4	"fmt"
5	"io"
6	"math"
7	"os"
8	"path/filepath"
9	"runtime"
10	"sync"
11	"time"
12
13	"github.com/dundee/gdu/v5/internal/common"
14	"github.com/dundee/gdu/v5/pkg/analyze"
15	"github.com/dundee/gdu/v5/pkg/device"
16	"github.com/dundee/gdu/v5/pkg/fs"
17	"github.com/dundee/gdu/v5/report"
18	"github.com/fatih/color"
19)
20
21// UI struct
22type UI struct {
23	output io.Writer
24	*common.UI
25	red               *color.Color
26	orange            *color.Color
27	blue              *color.Color
28	cyan              *color.Color
29	showItemCnt       bool
30	showSymlinkTarget bool
31	top               int
32	depth             int
33	summarize         bool
34	noPrefix          bool
35	fixedBase         float64
36	fixedSuffix       string
37	reverseSort       bool
38}
39
40var (
41	progressRunes      = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
42	progressRunesOld   = []rune(`-\\|/`)
43	progressRunesCount = len(progressRunes)
44)
45
46// CreateStdoutUI creates UI for stdout
47func CreateStdoutUI(
48	output io.Writer,
49	useColors bool,
50	showProgress bool,
51	showApparentSize bool,
52	showRelativeSize bool,
53	summarize bool,
54	useSIPrefix bool,
55	noPrefix bool,
56	fixedUnit string,
57	top int,
58	reverseSort bool,
59	depth int,
60) *UI {
61	ui := &UI{
62		UI: &common.UI{
63			UseColors:        useColors,
64			ShowProgress:     showProgress,
65			ShowApparentSize: showApparentSize,
66			ShowRelativeSize: showRelativeSize,
67			Analyzer:         analyze.CreateTopDirAnalyzer(),
68			UseSIPrefix:      useSIPrefix,
69		},
70		output:      output,
71		summarize:   summarize,
72		noPrefix:    noPrefix,
73		top:         top,
74		reverseSort: reverseSort,
75		depth:       depth,
76	}
77	if fixedUnit != "" {
78		ui.SetFixedUnit(fixedUnit)
79	} else if !noPrefix && !useSIPrefix {
80		ui.SetBlockSizeFromEnvironment()
81	}
82	ui.red = color.New(color.FgRed).Add(color.Bold)
83	ui.orange = color.New(color.FgYellow).Add(color.Bold)
84	ui.blue = color.New(color.FgBlue).Add(color.Bold)
85	ui.cyan = color.New(color.FgCyan).Add(color.Bold)
86
87	if ui.top > 0 || ui.depth > 0 {
88		ui.Analyzer = analyze.CreateAnalyzer()
89	}
90
91	if !useColors {
92		color.NoColor = true
93	}
94
95	return ui
96}
97func (ui *UI) SetFixedUnit(unitChar string) {
98	k, m, g := common.Ki, common.Mi, common.Gi
99	suffixMap := map[string]string{"k": " KiB", "m": " MiB", "g": " GiB"}
100
101	if ui.UseSIPrefix {
102		k, m, g = common.K, common.M, common.G
103		suffixMap = map[string]string{"k": " kB", "m": " MB", "g": " GB"}
104	}
105
106	switch unitChar {
107	case "k":
108		ui.fixedBase = k
109		ui.fixedSuffix = suffixMap["k"]
110	case "m":
111		ui.fixedBase = m
112		ui.fixedSuffix = suffixMap["m"]
113	case "g":
114		ui.fixedBase = g
115		ui.fixedSuffix = suffixMap["g"]
116	}
117}
118
119func (ui *UI) SetShowItemCount() {
120	ui.showItemCnt = true
121}
122
123// SetShowSymlinkTarget enables displaying the symlink target (name -> target).
124func (ui *UI) SetShowSymlinkTarget(value bool) {
125	ui.showSymlinkTarget = value
126}
127
128func (ui *UI) UseOldProgressRunes() {
129	progressRunes = progressRunesOld
130	progressRunesCount = len(progressRunes)
131}
132
133// StartUILoop stub
134func (ui *UI) StartUILoop() error {
135	return nil
136}
137
138// SetCollapsePath sets the flag to collapse paths
139func (ui *UI) SetCollapsePath(value bool) {
140}
141
142// ListDevices lists mounted devices and shows their disk usage
143func (ui *UI) ListDevices(getter device.DevicesInfoGetter) error {
144	devices, err := getter.GetDevicesInfo()
145	if err != nil {
146		return err
147	}
148
149	maxDeviceNameLength := maxInt(maxLength(
150		devices,
151		func(device *device.Device) string { return device.Name },
152	), len("Devices"))
153
154	var sizeLength, percentLength int
155	if ui.UseColors {
156		sizeLength = 20
157		percentLength = 16
158	} else {
159		sizeLength = 9
160		percentLength = 5
161	}
162
163	lineFormat := fmt.Sprintf(
164		"%%%ds %%%ds %%%ds %%%ds %%%ds %%s\n",
165		maxDeviceNameLength,
166		sizeLength,
167		sizeLength,
168		sizeLength,
169		percentLength,
170	)
171
172	fmt.Fprintf(
173		ui.output,
174		fmt.Sprintf("%%%ds %%9s %%9s %%9s %%5s %%s\n", maxDeviceNameLength),
175		"Device",
176		"Size",
177		"Used",
178		"Free",
179		"Used%",
180		"Mount point",
181	)
182
183	for _, device := range devices {
184		usedPercent := math.Round(float64(device.Size-device.Free) / float64(device.Size) * 100)
185
186		fmt.Fprintf(
187			ui.output,
188			lineFormat,
189			device.Name,
190			ui.formatSize(device.Size),
191			ui.formatSize(device.Size-device.Free),
192			ui.formatSize(device.Free),
193			ui.red.Sprintf("%.f%%", usedPercent),
194			device.MountPoint)
195	}
196
197	return nil
198}
199
200// AnalyzePath analyzes recursively disk usage in given path
201func (ui *UI) AnalyzePath(path string, _ fs.Item) error {
202	// When path is a regular file, create a File item directly so that
203	// apparent-size (GetSize) and disk-usage (GetUsage) are both correct.
204	// Running a file through AnalyzeDir + UpdateStats would report the
205	// default 4096-byte directory overhead instead of the real values.
206	info, err := os.Stat(path)
207	if err == nil && info.Mode().IsRegular() {
208		file := analyze.CreateFileItem(filepath.Base(path), info)
209		ui.printTotalItem(file)
210		return nil
211	}
212
213	var (
214		dir             fs.Item
215		wait            sync.WaitGroup
216		updateStatsDone chan struct{}
217	)
218	updateStatsDone = make(chan struct{}, 1)
219
220	if ui.ShowProgress {
221		wait.Add(1)
222		go func() {
223			defer wait.Done()
224			ui.updateProgress(updateStatsDone)
225		}()
226	}
227
228	wait.Add(1)
229	go func() {
230		defer wait.Done()
231		dir = ui.Analyzer.AnalyzeDir(path, ui.CreateIgnoreFunc(), ui.CreateFileTypeFilter())
232		if ui.IsFilteringFiles() {
233			dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems, 10))
234		} else {
235			dir.UpdateStats(make(fs.HardLinkedItems, 10))
236		}
237		updateStatsDone <- struct{}{}
238	}()
239
240	wait.Wait()
241
242	ui.printAnalyzedDir(dir)
243
244	return nil
245}
246
247// AnalyzePaths analyzes several paths and reports them together, grouped under
248// a virtual top level directory.
249func (ui *UI) AnalyzePaths(paths []string) error {
250	if len(paths) == 1 {
251		return ui.AnalyzePath(paths[0], nil)
252	}
253
254	// The top dir analyzer returns SimpleDir, a reduced item that cannot be
255	// re-parented, so it cannot live under a virtual root. Trade its lower
256	// memory use for the full tree, as --top and --depth already do.
257	if _, ok := ui.Analyzer.(*analyze.TopDirAnalyzer); ok {
258		ui.Analyzer = analyze.CreateAnalyzer()
259	}
260
261	roots := make([]fs.Item, 0, len(paths))
262	for _, path := range paths {
263		// the analyzer owns one set of progress channels per scan, so it has to
264		// be reset before each root - and before updateProgress reads its done
265		// channel
266		ui.Analyzer.ResetProgress()
267
268		var wait sync.WaitGroup
269		updateStatsDone := make(chan struct{}, 1)
270
271		if ui.ShowProgress {
272			wait.Add(1)
273			go func() {
274				defer wait.Done()
275				ui.updateProgress(updateStatsDone)
276			}()
277		}
278
279		root := ui.Analyzer.AnalyzeDir(path, ui.CreateIgnoreFunc(), ui.CreateFileTypeFilter())
280		updateStatsDone <- struct{}{}
281		wait.Wait()
282
283		roots = append(roots, root)
284	}
285
286	dir := analyze.CreateVirtualRootDir(roots...)
287	if ui.IsFilteringFiles() {
288		dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems, 10))
289	} else {
290		dir.UpdateStats(make(fs.HardLinkedItems, 10))
291	}
292
293	ui.printAnalyzedDir(dir)
294
295	return nil
296}
297
298func (ui *UI) printAnalyzedDir(dir fs.Item) {
299	switch {
300	case ui.top > 0:
301		ui.printTopFiles(dir)
302	case ui.depth > 0:
303		ui.printDirWithDepth(dir, 0)
304	case ui.summarize:
305		ui.printTotalItem(dir)
306	default:
307		ui.showDir(dir)
308	}
309}
310
311// ReadFromStorage reads analysis data from persistent key-value storage
312func (ui *UI) ReadFromStorage(storagePath, path string) error {
313	storage := analyze.NewStorage(storagePath, path)
314	closeFn := storage.Open()
315	defer closeFn()
316
317	dir, err := storage.GetDirForPath(path)
318	if err != nil {
319		return err
320	}
321
322	switch {
323	case ui.top > 0:
324		ui.printTopFiles(dir)
325	case ui.summarize:
326		ui.printTotalItem(dir)
327	default:
328		ui.showDir(dir)
329	}
330	return nil
331}
332
333func (ui *UI) showDir(dir fs.Item) {
334	sortOrder := fs.SortDesc
335	if ui.reverseSort {
336		sortOrder = fs.SortAsc
337	}
338
339	sort := fs.SortBySize
340	if ui.ShowApparentSize {
341		sort = fs.SortByApparentSize
342	}
343
344	for file := range dir.GetFiles(sort, sortOrder) {
345		ui.printItem(dir, file)
346	}
347}
348
349func (ui *UI) printTopFiles(file fs.Item) {
350	collected := analyze.CollectTopFiles(file, ui.top)
351	for _, file := range collected {
352		ui.printItemPath(file)
353	}
354}
355
356func (ui *UI) printTotalItem(file fs.Item) {
357	var lineFormat string
358	if ui.UseColors {
359		lineFormat = "%20s %s\n"
360	} else {
361		lineFormat = "%9s %s\n"
362	}
363
364	var size int64
365	if ui.ShowApparentSize {
366		size = file.GetSize()
367	} else {
368		size = file.GetUsage()
369	}
370
371	fmt.Fprintf(
372		ui.output,
373		lineFormat,
374		ui.formatSize(size),
375		file.GetName(),
376	)
377}
378
379// printItem prints one row of a directory listing. parent is the directory
380// being listed, which decides how the item is labelled; it may be nil.
381func (ui *UI) printItem(parent, file fs.Item) {
382	var lineFormat string
383	if ui.showItemCnt {
384		if ui.UseColors {
385			lineFormat = "%s %23s %25s %s\n"
386		} else {
387			lineFormat = "%s %9s %11s %s\n"
388		}
389	} else {
390		if ui.UseColors {
391			lineFormat = "%s %23s %s\n"
392		} else {
393			lineFormat = "%s %9s %s\n"
394		}
395	}
396
397	var size int64
398	if ui.ShowApparentSize {
399		size = file.GetSize()
400	} else {
401		size = file.GetUsage()
402	}
403
404	name := analyze.ItemDisplayName(parent, file)
405	if file.IsDir() {
406		// a scanned root is labelled with its absolute path, which already
407		// reads as a path and must not gain another leading separator
408		if !analyze.IsVirtualRootDir(parent) {
409			name = "/" + name
410		}
411		name = ui.blue.Sprint(name)
412	}
413
414	// Append symlink target with cyan name (like ls --color)
415	if ui.showSymlinkTarget {
416		if si, ok := file.(fs.SymlinkItem); ok {
417			if target := si.GetSymlinkTarget(); target != "" {
418				name = ui.cyan.Sprint(file.GetName()) + " -> " + target
419			}
420		}
421	}
422
423	if ui.showItemCnt {
424		fmt.Fprintf(
425			ui.output,
426			lineFormat,
427			string(file.GetFlag()),
428			ui.formatSize(size),
429			ui.formatCount(file.GetItemCount()),
430			name,
431		)
432		return
433	}
434
435	fmt.Fprintf(
436		ui.output,
437		lineFormat,
438		string(file.GetFlag()),
439		ui.formatSize(size),
440		name,
441	)
442}
443
444func (ui *UI) printItemPath(file fs.Item) {
445	var lineFormat string
446	if ui.UseColors {
447		lineFormat = "%20s %s\n"
448	} else {
449		lineFormat = "%9s %s\n"
450	}
451
452	var size int64
453	if ui.ShowApparentSize {
454		size = file.GetSize()
455	} else {
456		size = file.GetUsage()
457	}
458
459	if file.IsDir() {
460		fmt.Fprintf(ui.output,
461			lineFormat,
462			ui.formatSize(size),
463			ui.blue.Sprint(file.GetPath()))
464	} else {
465		fmt.Fprintf(ui.output,
466			lineFormat,
467			ui.formatSize(size),
468			file.GetPath())
469	}
470}
471
472func (ui *UI) printDirWithDepth(dir fs.Item, currentDepth int) {
473	// Print current directory
474	ui.printItemPath(dir)
475
476	// If we haven't reached the max depth, print contents
477	if currentDepth < ui.depth && dir.IsDir() {
478		sortOrder := fs.SortDesc
479		if ui.reverseSort {
480			sortOrder = fs.SortAsc
481		}
482
483		files := dir.GetFiles(fs.SortBySize, sortOrder)
484
485		// Print all files at this depth level
486		for file := range files {
487			if file.IsDir() {
488				// Recurse into subdirectories
489				ui.printDirWithDepth(file, currentDepth+1)
490			} else {
491				// Print regular files
492				ui.printItemPath(file)
493			}
494		}
495	}
496}
497
498// ReadAnalysis reads analysis report from JSON file
499func (ui *UI) ReadAnalysis(input io.Reader) error {
500	var (
501		dir      fs.Item
502		wait     sync.WaitGroup
503		err      error
504		doneChan chan struct{}
505	)
506
507	if ui.ShowProgress {
508		wait.Add(1)
509		doneChan = make(chan struct{})
510		go func() {
511			defer wait.Done()
512			ui.showReadingProgress(doneChan)
513		}()
514	}
515
516	wait.Add(1)
517	go func() {
518		defer wait.Done()
519		dir, err = report.ReadAnalysis(input)
520		if err != nil {
521			if ui.ShowProgress {
522				doneChan <- struct{}{}
523			}
524			return
525		}
526		runtime.GC()
527
528		if ui.IsFilteringFiles() {
529			dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems, 10))
530		} else {
531			dir.UpdateStats(make(fs.HardLinkedItems, 10))
532		}
533
534		if ui.ShowProgress {
535			doneChan <- struct{}{}
536		}
537	}()
538
539	wait.Wait()
540
541	if err != nil {
542		return err
543	}
544
545	if ui.summarize {
546		ui.printTotalItem(dir)
547	} else {
548		ui.showDir(dir)
549	}
550
551	return nil
552}
553
554func (ui *UI) showReadingProgress(doneChan chan struct{}) {
555	emptyRow := "\r"
556	for j := 0; j < 40; j++ {
557		emptyRow += " "
558	}
559
560	i := 0
561	for {
562		fmt.Fprint(ui.output, emptyRow)
563
564		select {
565		case <-doneChan:
566			fmt.Fprint(ui.output, "\r")
567			return
568		default:
569		}
570
571		fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i]))
572		fmt.Fprint(ui.output, "Reading analysis from file...")
573
574		time.Sleep(100 * time.Millisecond)
575		i++
576		i %= progressRunesCount
577	}
578}
579
580func (ui *UI) updateProgress(updateStatsDone <-chan struct{}) {
581	emptyRow := "\r"
582	for j := 0; j < 100; j++ {
583		emptyRow += " "
584	}
585
586	analysisDoneChan := ui.Analyzer.GetDone()
587	ticker := time.NewTicker(100 * time.Millisecond)
588	defer ticker.Stop()
589
590	i := 0
591	for {
592		select {
593		case <-ticker.C:
594			progress := ui.Analyzer.GetProgress()
595			fmt.Fprint(ui.output, emptyRow)
596			fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i]))
597			fmt.Fprint(ui.output, "Scanning... Total items: "+
598				ui.red.Sprint(common.FormatNumber(int64(progress.ItemCount)))+
599				" size: "+
600				ui.formatSize(progress.TotalUsage))
601			i++
602			i %= progressRunesCount
603		case <-analysisDoneChan:
604			ticker.Stop()
605			fmt.Fprint(ui.output, emptyRow)
606			for {
607				fmt.Fprint(ui.output, emptyRow)
608				fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i]))
609				fmt.Fprint(ui.output, "Calculating disk usage...")
610				time.Sleep(100 * time.Millisecond)
611				i++
612				i %= progressRunesCount
613
614				select {
615				case <-updateStatsDone:
616					fmt.Fprint(ui.output, emptyRow)
617					fmt.Fprint(ui.output, "\r")
618					return
619				default:
620				}
621			}
622		}
623	}
624}
625
626func (ui *UI) formatCount(count int64) string {
627	count64 := float64(count)
628
629	switch {
630	case count64 >= common.G:
631		return ui.red.Sprintf("%.1f", float64(count)/float64(common.G)) + "G"
632	case count64 >= common.M:
633		return ui.red.Sprintf("%.1f", float64(count)/float64(common.M)) + "M"
634	case count64 >= common.K:
635		return ui.red.Sprintf("%.1f", float64(count)/float64(common.K)) + "k"
636	default:
637		return ui.red.Sprintf("%d", count)
638	}
639}
640
641func (ui *UI) formatSize(size int64) string {
642	if ui.noPrefix {
643		return ui.orange.Sprintf("%d", size)
644	}
645	if formatted, ok := ui.FormatBlockSize(size); ok {
646		return ui.orange.Sprint(formatted)
647	}
648	if ui.fixedBase > 0 {
649		val := float64(size) / ui.fixedBase
650		return ui.orange.Sprintf("%.1f", val) + ui.fixedSuffix
651	}
652	if ui.UseSIPrefix {
653		return ui.formatWithDecPrefix(size)
654	}
655	return ui.formatWithBinPrefix(size)
656}
657
658func (ui *UI) formatWithBinPrefix(size int64) string {
659	fsize := float64(size)
660	asize := math.Abs(fsize)
661
662	switch {
663	case asize >= common.Ei:
664		return ui.orange.Sprintf("%.1f", fsize/common.Ei) + " EiB"
665	case asize >= common.Pi:
666		return ui.orange.Sprintf("%.1f", fsize/common.Pi) + " PiB"
667	case asize >= common.Ti:
668		return ui.orange.Sprintf("%.1f", fsize/common.Ti) + " TiB"
669	case asize >= common.Gi:
670		return ui.orange.Sprintf("%.1f", fsize/common.Gi) + " GiB"
671	case asize >= common.Mi:
672		return ui.orange.Sprintf("%.1f", fsize/common.Mi) + " MiB"
673	case asize >= common.Ki:
674		return ui.orange.Sprintf("%.1f", fsize/common.Ki) + " KiB"
675	default:
676		return ui.orange.Sprintf("%d", size) + " B"
677	}
678}
679
680func (ui *UI) formatWithDecPrefix(size int64) string {
681	fsize := float64(size)
682	asize := math.Abs(fsize)
683
684	switch {
685	case asize >= common.E:
686		return ui.orange.Sprintf("%.1f", fsize/common.E) + " EB"
687	case asize >= common.P:
688		return ui.orange.Sprintf("%.1f", fsize/common.P) + " PB"
689	case asize >= common.T:
690		return ui.orange.Sprintf("%.1f", fsize/common.T) + " TB"
691	case asize >= common.G:
692		return ui.orange.Sprintf("%.1f", fsize/common.G) + " GB"
693	case asize >= common.M:
694		return ui.orange.Sprintf("%.1f", fsize/common.M) + " MB"
695	case asize >= common.K:
696		return ui.orange.Sprintf("%.1f", fsize/common.K) + " kB"
697	default:
698		return ui.orange.Sprintf("%d", size) + " B"
699	}
700}
701
702func maxLength(list []*device.Device, keyGetter func(*device.Device) string) int {
703	maxLen := 0
704	var s string
705	for _, item := range list {
706		s = keyGetter(item)
707		if len(s) > maxLen {
708			maxLen = len(s)
709		}
710	}
711	return maxLen
712}
713
714func maxInt(x, y int) int {
715	if x > y {
716		return x
717	}
718	return y
719}