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