fork/gdu
git clone https://git.julia.blue/fork/gdu
da638da
ignore
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 ( 13barFullRune = "\u2588" 14barPartRunes = map [int ]string { 150 :" " , 161 :"\u258F" , 172 :"\u258E" , 183 :"\u258D" , 194 :"\u258C" , 205 :"\u258B" , 216 :"\u258A" , 227 :"\u2589" , 23 } 24) 25 26func getDeviceUsagePart (item * device.Device ,useOld bool )string { 27part := int (float64 (item .Size - item .Free )/ float64 (item .Size )* 100.0 ) 28if useOld { 29return getUsageGraphOld (part ) 30 } 31return getUsageGraph (part ) 32} 33 34func getUsageGraph (part int )string { 35graph := " " 36whole := part / 10 37for i := 0 ;i < whole ;i ++ { 38graph += barFullRune 39 } 40partWidth := (part % 10 )* 8 / 10 41if part < 100 { 42graph += barPartRunes [partWidth ] 43 } 44 45for i := 0 ;i < 10 - whole - 1 ;i ++ { 46graph += " " 47 } 48 49graph += "\u258F" 50return graph 51} 52 53func getUsageGraphOld (part int )string { 54part /= 10 55graph := "[" 56for i := 0 ;i < 10 ;i ++ { 57if part > i { 58graph += "#" 59 }else { 60graph += " " 61 } 62 } 63graph += "]" 64return graph 65} 66 67func modal (p tview.Primitive ,width ,height int ) tview.Primitive { 68return tview .NewFlex (). 69AddItem (nil ,0 ,1 ,false ). 70AddItem (tview .NewFlex ().SetDirection (tview .FlexRow ). 71AddItem (nil ,0 ,1 ,false ). 72AddItem (p ,height ,1 ,true ). 73AddItem (nil ,0 ,1 ,false ),width ,1 ,true ). 74AddItem (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 { 81DisplayName string // The display name shown in the UI (e.g., "a/b/c") 82DeepestDir fs.Item // The actual deepest directory item 83Segments []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 { 89if item == nil || ! item .IsDir () { 90return nil 91 } 92 93var segments []string 94current := item 95 96for { 97// Collect files to check count and types 98var files []fs.Item 99for file := range current .GetFiles (fs .SortByName ,fs .SortAsc ) { 100files = append (files ,file ) 101 } 102 103if len (files )> 1 { 104break 105 } 106 107// Count directories and files separately 108var subdirs []fs.Item 109var fileCount int 110for _ ,file := range files { 111if file .IsDir () { 112subdirs = append (subdirs ,file ) 113 }else { 114fileCount ++ 115 } 116 } 117 118// Only collapse if there's exactly one subdirectory AND no files 119if len (subdirs )!= 1 || fileCount > 0 { 120break 121 } 122 123// Add this segment to the path 124// nolint:staticcheck // the result is used 125segments = append (segments ,subdirs [0 ].GetName ()) 126current = subdirs [0 ] 127 } 128 129// Only create collapsed path if we have at least one collapsible segment 130if len (segments )== 0 { 131return nil 132 } 133 134return & CollapsedPath { 135DisplayName :filepath .Join (slices .Concat ([]string {item .GetName ()},segments )... ), 136DeepestDir :current , 137Segments :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 { 144if currentDir == nil { 145return nil 146 } 147if currentDir .GetParent ()== nil { 148return nil 149 } 150 151// Check if current directory is part of a single-child chain going up 152current := currentDir 153var chainParent fs.Item 154 155// Walk up the parent chain 156for current .GetParent ()!= nil { 157parent := current .GetParent () 158 159// Count files in parent 160fileCount := 0 161for range parent .GetFiles (fs .SortByName ,fs .SortAsc ) { 162fileCount ++ 163if fileCount > 1 { 164break 165 } 166 } 167 168// If parent has more than one item, this is where the collapsed chain starts 169if fileCount > 1 { 170chainParent = parent 171break 172 } 173 174// Move up the chain 175current = 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 180if chainParent != nil { 181return chainParent 182 } 183 184return currentDir .GetParent () 185}