fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
subvolume
1package tui 2 3import ( 4"fmt" 5"math" 6 7"github.com/dundee/gdu/v5/internal/common" 8"github.com/dundee/gdu/v5/pkg/fs" 9"github.com/rivo/tview" 10) 11 12const ( 13blackOnWhite = "[black:white:-]" 14whiteOnBlack = "[white:black:-]" 15 16defaultColor = "[-::]" 17defaultColorBold = "[::b]" 18) 19 20// getUsagePart returns the percentage (0-100) that the given item's size or 21// usage represents relative to the provided maximum. 22func (ui * UI )getUsagePart (item fs.Item ,maxUsage ,maxSize int64 ,ignored bool )float64 { 23if ignored { 24return 0 25 } 26if ui .ShowApparentSize { 27if size := item .GetSize ();size > 0 { 28return float64 (size )/ float64 (maxSize )* 100.0 29 } 30 }else { 31if usage := item .GetUsage ();usage > 0 { 32return float64 (usage )/ float64 (maxUsage )* 100.0 33 } 34 } 35return 0 36} 37 38// formatUsagePercentage formats the numeric usage percentage shown next to the size bar. 39func formatUsagePercentage (part float64 )string { 40return fmt .Sprintf (" %5.1f%%" ,part ) 41} 42 43func (ui * UI )formatFileRow (item fs.Item ,maxUsage ,maxSize int64 ,marked ,ignored bool )string { 44partFloat := ui .getUsagePart (item ,maxUsage ,maxSize ,ignored ) 45part := int (partFloat ) 46 47row := string (item .GetFlag ()) 48 49numberColor := fmt .Sprintf ( 50"[%s::b]" , 51ui .resultRow .NumberColor , 52 ) 53 54if ui .UseColors && ! marked && ! ignored { 55row += numberColor 56 }else { 57row += defaultColorBold 58 } 59 60if ui .ShowApparentSize { 61row += fmt .Sprintf ("%15s" ,ui .formatSize (item .GetSize (),false ,true )) 62 }else { 63row += fmt .Sprintf ("%15s" ,ui .formatSize (item .GetUsage (),false ,true )) 64 } 65 66if ui .showBarPercentage { 67row += formatUsagePercentage (partFloat ) 68 } 69if ui .useOldSizeBar { 70row += " " + getUsageGraphOld (part )+ " " 71 }else { 72row += getUsageGraph (part ) 73 } 74 75if ui .showItemCount { 76if ui .UseColors && ! marked && ! ignored { 77row += numberColor 78 }else { 79row += defaultColorBold 80 } 81 82countToDisplay := item .GetItemCount () 83if item .IsDir () { 84countToDisplay -- 85 } 86row += fmt .Sprintf ("%11s " ,ui .formatCount (countToDisplay )) 87 } 88 89if ui .showMtime { 90if ui .UseColors && ! marked && ! ignored { 91row += numberColor 92 }else { 93row += defaultColorBold 94 } 95row += fmt .Sprintf ( 96"%s " + defaultColor , 97item .GetMtime ().Format ("2006-01-02 15:04:05" ), 98 ) 99 } 100 101if len (ui .markedRows )> 0 { 102if marked { 103row += string ('✓' ) 104 }else { 105row += " " 106 } 107row += " " 108 } 109 110// Display symlink name in cyan/aqua (like ls --color) and target 111if name := ui .formatItemName (item ,marked ,ignored );name != "" { 112return row + name 113 } 114 115if item .IsDir () { 116if ui .UseColors && ! marked && ! ignored { 117row += fmt .Sprintf ("[%s::b]/" ,ui .resultRow .DirectoryColor ) 118 }else { 119row += defaultColorBold + "/" 120 } 121 } 122row += tview .Escape (item .GetName ()) 123 124return row 125} 126 127// formatItemName returns formatted name for special item types (e.g. symlinks). 128// Returns empty string if the item has no special formatting. 129func (ui * UI )formatItemName (item fs.Item ,marked ,ignored bool )string { 130if ! ui .showSymlinkTarget { 131return "" 132 } 133si ,ok := item .(fs.SymlinkItem ) 134if ! ok { 135return "" 136 } 137target := si .GetSymlinkTarget () 138if target == "" { 139return "" 140 } 141 142var name string 143if ui .UseColors && ! marked && ! ignored { 144name = "[aqua::b]" + tview .Escape (item .GetName ()) 145 }else { 146name = tview .Escape (item .GetName ()) 147 } 148return name + defaultColor + " -> " + tview .Escape (target ) 149} 150 151// formatCollapsedRow formats a collapsed directory path for display 152func (ui * UI )formatCollapsedRow (collapsedPath * CollapsedPath ,maxUsage ,maxSize int64 ,marked ,ignored bool )string { 153// Use the deepest directory's stats for display 154item := collapsedPath .DeepestDir 155 156partFloat := ui .getUsagePart (item ,maxUsage ,maxSize ,ignored ) 157part := int (partFloat ) 158 159row := string (item .GetFlag ()) 160 161numberColor := fmt .Sprintf ( 162"[%s::b]" , 163ui .resultRow .NumberColor , 164 ) 165 166if ui .UseColors && ! marked && ! ignored { 167row += numberColor 168 }else { 169row += defaultColorBold 170 } 171 172if ui .ShowApparentSize { 173row += fmt .Sprintf ("%15s" ,ui .formatSize (item .GetSize (),false ,true )) 174 }else { 175row += fmt .Sprintf ("%15s" ,ui .formatSize (item .GetUsage (),false ,true )) 176 } 177 178if ui .showBarPercentage { 179row += formatUsagePercentage (partFloat ) 180 } 181if ui .useOldSizeBar { 182row += " " + getUsageGraphOld (part )+ " " 183 }else { 184row += getUsageGraph (part ) 185 } 186 187if ui .showItemCount { 188if ui .UseColors && ! marked && ! ignored { 189row += numberColor 190 }else { 191row += defaultColorBold 192 } 193 194countToDisplay := item .GetItemCount () 195if item .IsDir () { 196countToDisplay -- 197 } 198row += fmt .Sprintf ("%11s " ,ui .formatCount (countToDisplay )) 199 } 200 201if ui .showMtime { 202if ui .UseColors && ! marked && ! ignored { 203row += numberColor 204 }else { 205row += defaultColorBold 206 } 207row += fmt .Sprintf ( 208"%s " + defaultColor , 209item .GetMtime ().Format ("2006-01-02 15:04:05" ), 210 ) 211 } 212 213if len (ui .markedRows )> 0 { 214if marked { 215row += string ('✓' ) 216 }else { 217row += " " 218 } 219row += " " 220 } 221 222// Always display as directory with special formatting for collapsed path 223if ui .UseColors && ! marked && ! ignored { 224row += fmt .Sprintf ("[%s::b]/" ,ui .resultRow .DirectoryColor ) 225 }else { 226row += defaultColorBold + "/" 227 } 228 229// Display the collapsed path (e.g., "a/b/c") 230row += tview .Escape (collapsedPath .DisplayName ) 231return row 232} 233 234func (ui * UI )formatSize (size int64 ,reverseColor ,transparentBg bool )string { 235var color string 236if reverseColor { 237if ui .UseColors { 238color = fmt .Sprintf ( 239"[%s:%s:-]" , 240ui .footerTextColor , 241ui .footerBackgroundColor , 242 ) 243 }else { 244color = blackOnWhite 245 } 246 }else { 247if transparentBg { 248color = defaultColor 249 }else { 250color = whiteOnBlack 251 } 252 } 253 254if formatted ,ok := ui .FormatBlockSize (size );ok { 255return formatted + color 256 } 257if ui .UseSIPrefix { 258return formatWithDecPrefix (size ,color ) 259 } 260return formatWithBinPrefix (float64 (size ),color ) 261} 262 263func (ui * UI )formatCount (count int64 )string { 264row := "" 265color := defaultColor 266count64 := float64 (count ) 267 268switch { 269case count64 >= common .G : 270row += fmt .Sprintf ("%.1f%sG" ,float64 (count )/ float64 (common .G ),color ) 271case count64 >= common .M : 272row += fmt .Sprintf ("%.1f%sM" ,float64 (count )/ float64 (common .M ),color ) 273case count64 >= common .K : 274row += fmt .Sprintf ("%.1f%sk" ,float64 (count )/ float64 (common .K ),color ) 275default : 276row += fmt .Sprintf ("%d%s" ,count ,color ) 277 } 278return row 279} 280 281func formatWithBinPrefix (fsize float64 ,color string )string { 282asize := math .Abs (fsize ) 283 284switch { 285case asize >= common .Ei : 286return fmt .Sprintf ("%.1f%s EiB" ,fsize / common .Ei ,color ) 287case asize >= common .Pi : 288return fmt .Sprintf ("%.1f%s PiB" ,fsize / common .Pi ,color ) 289case asize >= common .Ti : 290return fmt .Sprintf ("%.1f%s TiB" ,fsize / common .Ti ,color ) 291case asize >= common .Gi : 292return fmt .Sprintf ("%.1f%s GiB" ,fsize / common .Gi ,color ) 293case asize >= common .Mi : 294return fmt .Sprintf ("%.1f%s MiB" ,fsize / common .Mi ,color ) 295case asize >= common .Ki : 296return fmt .Sprintf ("%.1f%s KiB" ,fsize / common .Ki ,color ) 297default : 298return fmt .Sprintf ("%d%s B" ,int64 (fsize ),color ) 299 } 300} 301 302func formatWithDecPrefix (size int64 ,color string )string { 303fsize := float64 (size ) 304asize := math .Abs (fsize ) 305switch { 306case asize >= common .E : 307return fmt .Sprintf ("%.1f%s EB" ,fsize / common .E ,color ) 308case asize >= common .P : 309return fmt .Sprintf ("%.1f%s PB" ,fsize / common .P ,color ) 310case asize >= common .T : 311return fmt .Sprintf ("%.1f%s TB" ,fsize / common .T ,color ) 312case asize >= common .G : 313return fmt .Sprintf ("%.1f%s GB" ,fsize / common .G ,color ) 314case asize >= common .M : 315return fmt .Sprintf ("%.1f%s MB" ,fsize / common .M ,color ) 316case asize >= common .K : 317return fmt .Sprintf ("%.1f%s kB" ,fsize / common .K ,color ) 318default : 319return fmt .Sprintf ("%d%s B" ,size ,color ) 320 } 321}