fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
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// rowMaxima holds the denominators the bars of one directory listing are 21// scaled against. Which field is used depends on the displayed size metric and 22// on which bar is being drawn. Each field is either the sum over the sibling 23// items or, with ShowRelativeSize, the largest sibling. 24type rowMaxima struct { 25usage int64 26size int64 27count int64 28} 29 30// getUsagePart returns the percentage (0-100) that the given item's size or 31// usage represents relative to the provided maximum. 32func (ui * UI )getUsagePart (item fs.Item ,maxima rowMaxima ,ignored bool )float64 { 33if ignored { 34return 0 35 } 36if ui .ShowApparentSize { 37if size := item .GetSize ();size > 0 { 38return float64 (size )/ float64 (maxima .size )* 100.0 39 } 40 }else { 41if usage := item .GetUsage ();usage > 0 { 42return float64 (usage )/ float64 (maxima .usage )* 100.0 43 } 44 } 45return 0 46} 47 48// getCountPart returns the percentage (0-100) that the given item's displayed 49// item count represents relative to the provided maximum. 50func (ui * UI )getCountPart (item fs.Item ,maxima rowMaxima ,ignored bool )float64 { 51if ignored || maxima .count == 0 { 52return 0 53 } 54if count := fs .DisplayedItemCount (item );count > 0 { 55return float64 (count )/ float64 (maxima .count )* 100.0 56 } 57return 0 58} 59 60// formatUsagePercentage formats the numeric usage percentage shown next to the size bar. 61func formatUsagePercentage (part float64 )string { 62return fmt .Sprintf (" %5.1f%%" ,part ) 63} 64 65// formatBar renders a single bar filled to the given percentage, in whichever 66// bar style is configured. 67func (ui * UI )formatBar (part int )string { 68if ui .useOldSizeBar { 69return " " + getUsageGraphOld (part )+ " " 70 } 71return getUsageGraph (part ) 72} 73 74// Visible widths of the columns rendered by formatColumns. The numeric columns 75// are padded to a wider format verb than these because formatSize and 76// formatCount embed a color tag, which is counted by the verb but occupies no 77// screen columns. 78const ( 79flagColumnWidth = 1 80sizeColumnWidth = 10 81percentageColumnWidth = 7 82itemCountColumnWidth = 7 83mtimeColumnWidth = 20 84markedColumnWidth = 2 85barColumnWidth = 12 86oldBarColumnWidth = 14 87) 88 89// columnsWidth returns the number of screen columns formatColumns occupies, so 90// that rows which have no columns of their own (the "/.." entry) can be padded 91// to line up their name with the rest of the listing. 92func (ui * UI )columnsWidth ()int { 93barWidth := barColumnWidth 94if ui .useOldSizeBar { 95barWidth = oldBarColumnWidth 96 } 97 98width := flagColumnWidth + sizeColumnWidth + barWidth 99if ui .showBarPercentage { 100width += percentageColumnWidth 101 } 102if ui .showItemCount { 103width += itemCountColumnWidth 104if ui .showItemCountBar { 105width += barWidth 106 } 107 } 108if ui .showMtime { 109width += mtimeColumnWidth 110 } 111if len (ui .markedRows )> 0 { 112width += markedColumnWidth 113 } 114return width 115} 116 117// formatColumns renders every column of a row except the trailing name: the 118// type flag, the size, the size bar, and the optional percentage, item count, 119// item count bar, mtime and marked columns. 120// 121// statsItem is the item the numbers are taken from, which is not always the 122// item whose name ends up on the row: a collapsed path displays the name of a 123// whole chain of directories but the stats of its deepest one. 124func (ui * UI )formatColumns (statsItem fs.Item ,maxima rowMaxima ,marked ,ignored bool )string { 125// numberPrefix is the color tag introducing a numeric column. 126numberPrefix := func ()string { 127if ui .UseColors && ! marked && ! ignored { 128return fmt .Sprintf ("[%s::b]" ,ui .resultRow .NumberColor ) 129 } 130return defaultColorBold 131 } 132 133row := string (statsItem .GetFlag ())+ numberPrefix () 134 135if ui .ShowApparentSize { 136row += fmt .Sprintf ("%15s" ,ui .formatSize (statsItem .GetSize (),false ,true )) 137 }else { 138row += fmt .Sprintf ("%15s" ,ui .formatSize (statsItem .GetUsage (),false ,true )) 139 } 140 141usagePart := ui .getUsagePart (statsItem ,maxima ,ignored ) 142if ui .showBarPercentage { 143row += formatUsagePercentage (usagePart ) 144 } 145row += ui .formatBar (int (usagePart )) 146 147if ui .showItemCount { 148row += numberPrefix () 149row += fmt .Sprintf ("%11s " ,ui .formatCount (fs .DisplayedItemCount (statsItem ))) 150 151if ui .showItemCountBar { 152row += ui .formatBar (int (ui .getCountPart (statsItem ,maxima ,ignored ))) 153 } 154 } 155 156if ui .showMtime { 157row += numberPrefix () 158row += fmt .Sprintf ( 159"%s " + defaultColor , 160statsItem .GetMtime ().Format ("2006-01-02 15:04:05" ), 161 ) 162 } 163 164if len (ui .markedRows )> 0 { 165if marked { 166row += string ('✓' ) 167 }else { 168row += " " 169 } 170row += " " 171 } 172 173return row 174} 175 176// dirRowStyle returns the styling that introduces a directory name in a row. 177func (ui * UI )dirRowStyle (marked ,ignored bool )string { 178if ui .UseColors && ! marked && ! ignored { 179return fmt .Sprintf ("[%s::b]" ,ui .resultRow .DirectoryColor ) 180 } 181return defaultColorBold 182} 183 184func (ui * UI )formatFileRow (item fs.Item ,maxima rowMaxima ,marked ,ignored bool )string { 185row := ui .formatColumns (item ,maxima ,marked ,ignored ) 186 187// Display symlink name in cyan/aqua (like ls --color) and target 188if name := ui .formatItemName (item ,marked ,ignored );name != "" { 189return row + name 190 } 191 192// Scanned roots are labelled with their absolute path, which already reads 193// as a path and must not be given another leading separator. 194if ui .atVirtualRoot () { 195return row + ui .dirRowStyle (marked ,ignored )+ tview .Escape (item .GetPath ()) 196 } 197 198if item .IsDir () { 199row += ui .dirRowStyle (marked ,ignored )+ "/" 200 } 201row += tview .Escape (item .GetName ()) 202 203return row 204} 205 206// formatItemName returns formatted name for special item types (e.g. symlinks). 207// Returns empty string if the item has no special formatting. 208func (ui * UI )formatItemName (item fs.Item ,marked ,ignored bool )string { 209if ! ui .showSymlinkTarget { 210return "" 211 } 212si ,ok := item .(fs.SymlinkItem ) 213if ! ok { 214return "" 215 } 216target := si .GetSymlinkTarget () 217if target == "" { 218return "" 219 } 220 221var name string 222if ui .UseColors && ! marked && ! ignored { 223name = "[aqua::b]" + tview .Escape (item .GetName ()) 224 }else { 225name = tview .Escape (item .GetName ()) 226 } 227return name + defaultColor + " -> " + tview .Escape (target ) 228} 229 230// formatCollapsedRow formats a collapsed directory path for display 231func (ui * UI )formatCollapsedRow ( 232collapsedPath * CollapsedPath ,maxima rowMaxima ,marked ,ignored bool , 233)string { 234// Use the deepest directory's stats for display 235row := ui .formatColumns (collapsedPath .DeepestDir ,maxima ,marked ,ignored ) 236 237// Always display as directory with special formatting for collapsed path 238row += ui .dirRowStyle (marked ,ignored ) 239// a collapsed scanned root already starts from an absolute path 240if ! ui .atVirtualRoot () { 241row += "/" 242 } 243 244// Display the collapsed path (e.g., "a/b/c") 245row += tview .Escape (collapsedPath .DisplayName ) 246return row 247} 248 249func (ui * UI )formatSize (size int64 ,reverseColor ,transparentBg bool )string { 250var color string 251if reverseColor { 252if ui .UseColors { 253color = fmt .Sprintf ( 254"[%s:%s:-]" , 255ui .footerTextColor , 256ui .footerBackgroundColor , 257 ) 258 }else { 259color = blackOnWhite 260 } 261 }else { 262if transparentBg { 263color = defaultColor 264 }else { 265color = whiteOnBlack 266 } 267 } 268 269if formatted ,ok := ui .FormatBlockSize (size );ok { 270return formatted + color 271 } 272if ui .UseSIPrefix { 273return formatWithDecPrefix (size ,color ) 274 } 275return formatWithBinPrefix (float64 (size ),color ) 276} 277 278func (ui * UI )formatCount (count int64 )string { 279row := "" 280color := defaultColor 281count64 := float64 (count ) 282 283switch { 284case count64 >= common .G : 285row += fmt .Sprintf ("%.1f%sG" ,float64 (count )/ float64 (common .G ),color ) 286case count64 >= common .M : 287row += fmt .Sprintf ("%.1f%sM" ,float64 (count )/ float64 (common .M ),color ) 288case count64 >= common .K : 289row += fmt .Sprintf ("%.1f%sk" ,float64 (count )/ float64 (common .K ),color ) 290default : 291row += fmt .Sprintf ("%d%s" ,count ,color ) 292 } 293return row 294} 295 296func formatWithBinPrefix (fsize float64 ,color string )string { 297asize := math .Abs (fsize ) 298 299switch { 300case asize >= common .Ei : 301return fmt .Sprintf ("%.1f%s EiB" ,fsize / common .Ei ,color ) 302case asize >= common .Pi : 303return fmt .Sprintf ("%.1f%s PiB" ,fsize / common .Pi ,color ) 304case asize >= common .Ti : 305return fmt .Sprintf ("%.1f%s TiB" ,fsize / common .Ti ,color ) 306case asize >= common .Gi : 307return fmt .Sprintf ("%.1f%s GiB" ,fsize / common .Gi ,color ) 308case asize >= common .Mi : 309return fmt .Sprintf ("%.1f%s MiB" ,fsize / common .Mi ,color ) 310case asize >= common .Ki : 311return fmt .Sprintf ("%.1f%s KiB" ,fsize / common .Ki ,color ) 312default : 313return fmt .Sprintf ("%d%s B" ,int64 (fsize ),color ) 314 } 315} 316 317func formatWithDecPrefix (size int64 ,color string )string { 318fsize := float64 (size ) 319asize := math .Abs (fsize ) 320switch { 321case asize >= common .E : 322return fmt .Sprintf ("%.1f%s EB" ,fsize / common .E ,color ) 323case asize >= common .P : 324return fmt .Sprintf ("%.1f%s PB" ,fsize / common .P ,color ) 325case asize >= common .T : 326return fmt .Sprintf ("%.1f%s TB" ,fsize / common .T ,color ) 327case asize >= common .G : 328return fmt .Sprintf ("%.1f%s GB" ,fsize / common .G ,color ) 329case asize >= common .M : 330return fmt .Sprintf ("%.1f%s MB" ,fsize / common .M ,color ) 331case asize >= common .K : 332return fmt .Sprintf ("%.1f%s kB" ,fsize / common .K ,color ) 333default : 334return fmt .Sprintf ("%d%s B" ,size ,color ) 335 } 336}