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