fork/gdu

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

Quet Zalfeat(analyze): add SQLite analyzer and some assorted changes (#484)da638da

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