fork/gdu

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

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

master
4.4 KiB190 linesraw
1package tui
2
3import (
4	"path/filepath"
5	"slices"
6
7	"github.com/dundee/gdu/v5/pkg/analyze"
8	"github.com/dundee/gdu/v5/pkg/device"
9	"github.com/dundee/gdu/v5/pkg/fs"
10	"github.com/rivo/tview"
11)
12
13var (
14	barFullRune  = "\u2588"
15	barPartRunes = map[int]string{
16		0: " ",
17		1: "\u258F",
18		2: "\u258E",
19		3: "\u258D",
20		4: "\u258C",
21		5: "\u258B",
22		6: "\u258A",
23		7: "\u2589",
24	}
25)
26
27func getDeviceUsagePart(item *device.Device, useOld bool) string {
28	part := int(float64(item.Size-item.Free) / float64(item.Size) * 100.0)
29	if useOld {
30		return getUsageGraphOld(part)
31	}
32	return getUsageGraph(part)
33}
34
35func getUsageGraph(part int) string {
36	graph := " "
37	whole := part / 10
38	for i := 0; i < whole; i++ {
39		graph += barFullRune
40	}
41	partWidth := (part % 10) * 8 / 10
42	if part < 100 {
43		graph += barPartRunes[partWidth]
44	}
45
46	for i := 0; i < 10-whole-1; i++ {
47		graph += " "
48	}
49
50	graph += "\u258F"
51	return graph
52}
53
54func getUsageGraphOld(part int) string {
55	part /= 10
56	graph := "["
57	for i := 0; i < 10; i++ {
58		if part > i {
59			graph += "#"
60		} else {
61			graph += " "
62		}
63	}
64	graph += "]"
65	return graph
66}
67
68func modal(p tview.Primitive, width, height int) tview.Primitive {
69	return tview.NewFlex().
70		AddItem(nil, 0, 1, false).
71		AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
72			AddItem(nil, 0, 1, false).
73			AddItem(p, height, 1, true).
74			AddItem(nil, 0, 1, false), width, 1, true).
75		AddItem(nil, 0, 1, false)
76}
77
78// CollapsedPath represents a directory chain that can be collapsed into a single display entry.
79// For example, if directory "a" contains only directory "b", and "b" contains only "c",
80// this represents the collapsed path "a/b/c" that allows direct navigation to the deepest directory.
81type CollapsedPath struct {
82	DisplayName string   // The display name shown in the UI (e.g., "a/b/c")
83	DeepestDir  fs.Item  // The actual deepest directory item
84	Segments    []string // Individual path segments of the collapsed chain
85}
86
87// findCollapsiblePath checks if the given directory item has a single subdirectory chain
88// and returns a CollapsedPath if it can be collapsed
89func findCollapsiblePath(item fs.Item) *CollapsedPath {
90	if item == nil || !item.IsDir() {
91		return nil
92	}
93
94	var segments []string
95	current := item
96
97	for {
98		// Collect files to check count and types
99		var files []fs.Item
100		for file := range current.GetFiles(fs.SortByName, fs.SortAsc) {
101			files = append(files, file)
102		}
103
104		if len(files) > 1 {
105			break
106		}
107
108		// Count directories and files separately
109		var subdirs []fs.Item
110		var fileCount int
111		for _, file := range files {
112			if file.IsDir() {
113				subdirs = append(subdirs, file)
114			} else {
115				fileCount++
116			}
117		}
118
119		// Only collapse if there's exactly one subdirectory AND no files
120		if len(subdirs) != 1 || fileCount > 0 {
121			break
122		}
123
124		// Add this segment to the path
125		// nolint:staticcheck // the result is used
126		segments = append(segments, subdirs[0].GetName())
127		current = subdirs[0]
128	}
129
130	// Only create collapsed path if we have at least one collapsible segment
131	if len(segments) == 0 {
132		return nil
133	}
134
135	// a collapsed chain starting at a scanned root is labelled from its
136	// absolute path, as an uncollapsed root would be
137	base := analyze.ItemDisplayName(item.GetParent(), item)
138
139	return &CollapsedPath{
140		DisplayName: filepath.Join(slices.Concat([]string{base}, segments)...),
141		DeepestDir:  current,
142		Segments:    segments,
143	}
144}
145
146// findCollapsedParent checks if the current directory is the deepest directory
147// in a collapsed path, and returns the appropriate parent to navigate to
148func findCollapsedParent(currentDir fs.Item) fs.Item {
149	if currentDir == nil {
150		return nil
151	}
152	if currentDir.GetParent() == nil {
153		return nil
154	}
155
156	// Check if current directory is part of a single-child chain going up
157	current := currentDir
158	var chainParent fs.Item
159
160	// Walk up the parent chain
161	for current.GetParent() != nil {
162		parent := current.GetParent()
163
164		// Count files in parent
165		fileCount := 0
166		for range parent.GetFiles(fs.SortByName, fs.SortAsc) {
167			fileCount++
168			if fileCount > 1 {
169				break
170			}
171		}
172
173		// If parent has more than one item, this is where the collapsed chain starts
174		if fileCount > 1 {
175			chainParent = parent
176			break
177		}
178
179		// Move up the chain
180		current = parent
181	}
182
183	// If we found a chain parent (meaning current dir is part of a collapsed path),
184	// return it, otherwise return the normal parent
185	if chainParent != nil {
186		return chainParent
187	}
188
189	return currentDir.GetParent()
190}