fork/gdu
git clone https://git.julia.blue/fork/gdu
9ca5c60
subvolume
1package tui 2 3import ( 4"fmt" 5"strconv" 6"strings" 7 8"github.com/gdamore/tcell/v2" 9"github.com/rivo/tview" 10 log"github.com/sirupsen/logrus" 11 12"github.com/dundee/gdu/v5/build" 13"github.com/dundee/gdu/v5/pkg/fs" 14) 15 16var ( 17helpDisabledSuffix = " (disabled)" 18 19helpText = ` [::b]up/down, k/j [white:black:-]Move cursor up/down 20[::b]pgup/pgdn, g/G [white:black:-]Move cursor top/bottom 21[::b]enter, right, l [white:black:-]Go to directory/device 22[::b]left, h [white:black:-]Go to parent directory 23 24[::b]r [white:black:-]Rescan current directory 25[::b]E [white:black:-]Export analysis data to file as JSON 26[::b]/ [white:black:-]Search items by name 27[::b]T [white:black:-]Filter items by file type (extension) 28[::b]a [white:black:-]Toggle between showing disk usage and apparent size 29[::b]B [white:black:-]Toggle bar alignment to biggest file or directory 30[::b]c [white:black:-]Show/hide file count 31[::b]m [white:black:-]Show/hide latest mtime 32[::b]b [white:black:-]Spawn shell in current directory 33[::b]q [white:black:-]Quit gdu (asks to confirm after a long scan) 34[::b]Q [white:black:-]Quit gdu and print current directory path 35 36Item under cursor: 37[::b]d [white:black:-]Delete file or directory 38[::b]e [white:black:-]Empty file or directory 39[::b]D [white:black:-]Move file or directory to trash 40[::b]space [white:black:-]Mark file or directory for deletion 41[::b]p [white:black:-]Print marked items paths to stdout after quitting 42[::b]I [white:black:-]Ignore file or directory 43[::b]v [white:black:-]Show content of file 44[::b]o [white:black:-]Open file or directory in external program 45[::b]i [white:black:-]Show info about item 46 47Sort by (twice toggles asc/desc): 48[::b]n [white:black:-]Sort by name (asc/desc) 49[::b]s [white:black:-]Sort by size (asc/desc) 50[::b]C [white:black:-]Sort by file count (asc/desc) 51[::b]M [white:black:-]Sort by mtime (asc/desc)` 52) 53 54// currentDirLabelText builds the breadcrumb label shown above the table, 55// annotated when a mid-scan preview is being displayed. 56func (ui * UI )currentDirLabelText ()string { 57label := "[::b] --- " + 58tview .Escape ( 59strings .TrimPrefix (ui .currentDirPath ,build .RootPathPrefix ), 60 )+ 61" ---" 62if ui .previewing { 63label += " [::b][yellow]scanning… (preview, Tab to resume)[-]" 64 } 65return label 66} 67 68// nolint: funlen // Why: complex function 69func (ui * UI )showDir () { 70var ( 71totalUsage int64 72totalSize int64 73maxUsage int64 74maxSize int64 75itemCount int64 76 ) 77 78ui .currentDirPath = ui .currentDir .GetPath () 79 80if ui .changeCwdFn != nil { 81err := ui .changeCwdFn (ui .currentDirPath ) 82if err != nil { 83log .Printf ("error setting cwd: %s" ,err .Error ()) 84 } 85log .Printf ("changing cwd to %s" ,ui .currentDirPath ) 86 } 87 88ui .currentDirLabel .SetText (ui .currentDirLabelText ()).SetDynamicColors (true ) 89 90ui .table .Clear () 91 92rowIndex := 0 93if ui .currentDirPath != ui .topDirPath { 94prefix := " " 95if len (ui .markedRows )> 0 { 96prefix += " " 97 } 98 99cell := tview .NewTableCell (prefix + "[::b]/.." ) 100 101// Use the collapsed parent logic to handle navigation back through collapsed paths 102var collapsedParent fs.Item 103if ui .collapsePath { 104collapsedParent = findCollapsedParent (ui .currentDir ) 105 }else { 106collapsedParent = ui .currentDir .GetParent () 107 } 108cell .SetReference (collapsedParent ) 109cell .SetStyle (tcell.Style {}.Foreground (tcell .ColorDefault )) 110ui .table .SetCell (0 ,0 ,cell ) 111rowIndex ++ 112 } 113 114sortBy ,sortOrder := ui .getSortParams () 115 116unlock := ui .currentDir .RLock () 117defer unlock () 118 119i := rowIndex 120maxUsage = 0 121maxSize = 0 122for item := range ui .currentDir .GetFiles (sortBy ,sortOrder ) { 123if _ ,ignored := ui .ignoredRows [i ];ignored { 124i ++ 125continue 126 } 127 128if ui .ShowRelativeSize { 129if item .GetUsage ()> maxUsage { 130maxUsage = item .GetUsage () 131 } 132if item .GetSize ()> maxSize { 133maxSize = item .GetSize () 134 } 135 }else { 136maxSize += item .GetSize () 137maxUsage += item .GetUsage () 138 } 139i ++ 140 } 141 142for item := range ui .currentDir .GetFiles (sortBy ,sortOrder ) { 143if ui .filterValue != "" && ! strings .Contains ( 144strings .ToLower (item .GetName ()), 145strings .ToLower (ui .filterValue ), 146 ) { 147continue 148 } 149 150if ! ui .matchesTypeFilter (item .GetName (),item .IsDir ()) { 151continue 152 } 153 154_ ,ignored := ui .ignoredRows [rowIndex ] 155 156if ! ignored { 157totalUsage += item .GetUsage () 158totalSize += item .GetSize () 159itemCount += item .GetItemCount () 160 } 161 162_ ,marked := ui .markedRows [rowIndex ] 163 164var cell * tview.TableCell 165var reference fs.Item 166 167// Check if this directory can be collapsed 168if item .IsDir () { 169var collapsedPath * CollapsedPath 170if ui .collapsePath { 171collapsedPath = findCollapsiblePath (item ) 172 } 173 174if collapsedPath != nil { 175// Format as collapsed path 176cell = tview .NewTableCell (ui .formatCollapsedRow (collapsedPath ,maxUsage ,maxSize ,marked ,ignored )) 177// Reference should point to the deepest directory for navigation 178reference = collapsedPath .DeepestDir 179 }else { 180// Regular directory formatting 181cell = tview .NewTableCell (ui .formatFileRow (item ,maxUsage ,maxSize ,marked ,ignored )) 182reference = item 183 } 184 }else { 185// Regular file formatting 186cell = tview .NewTableCell (ui .formatFileRow (item ,maxUsage ,maxSize ,marked ,ignored )) 187reference = item 188 } 189 190cell .SetReference (reference ) 191 192switch { 193case ignored : 194cell .SetStyle (tcell.Style {}.Foreground (tview .Styles .SecondaryTextColor )) 195case marked : 196cell .SetStyle (tcell.Style {}.Foreground (ui .markedTextColor )) 197cell .SetBackgroundColor (ui .markedBackgroundColor ) 198default : 199cell .SetStyle (tcell.Style {}.Foreground (tcell .ColorDefault )) 200 } 201 202ui .table .SetCell (rowIndex ,0 ,cell ) 203rowIndex ++ 204 } 205 206var footerNumberColor ,footerTextColor string 207if ui .UseColors { 208footerNumberColor = fmt .Sprintf ( 209"[%s:%s:b]" , 210ui .footerNumberColor , 211ui .footerBackgroundColor , 212 ) 213footerTextColor = fmt .Sprintf ( 214"[%s:%s:-]" , 215ui .footerTextColor , 216ui .footerBackgroundColor , 217 ) 218 }else { 219footerNumberColor = "[black:white:b]" 220footerTextColor = blackOnWhite 221 } 222 223selected := "" 224if len (ui .markedRows )> 0 { 225selected = " Selected items: " + footerNumberColor + 226strconv .Itoa (len (ui .markedRows ))+ footerTextColor 227 } 228 229timeFilterText := ui .formatTimeFilterInfo () 230 231typeFilterText := ui .formatTypeFilterInfo (footerNumberColor ,footerTextColor ) 232 233ui .footerLabel .SetText ( 234selected + footerTextColor + 235" Total disk usage: " + 236footerNumberColor + 237ui .formatSize (totalUsage ,true ,false )+ 238" Apparent size: " + 239footerNumberColor + 240ui .formatSize (totalSize ,true ,false )+ 241" Items: " + footerNumberColor + fmt .Sprintf ("%d" ,itemCount )+ 242footerTextColor + 243" Sorting by: " + ui .sortBy + " " + ui .sortOrder + 244typeFilterText + 245timeFilterText ) 246 247ui .table .Select (0 ,0 ) 248ui .table .ScrollToBeginning () 249 250if ! ui .filtering && ! ui .typeFiltering { 251ui .app .SetFocus (ui .table ) 252 } 253} 254 255func (ui * UI )showDevices () { 256var totalUsage int64 257 258ui .table .Clear () 259ui .table .SetCell (0 ,0 ,tview .NewTableCell ("Device name" ).SetSelectable (false )) 260ui .table .SetCell (0 ,1 ,tview .NewTableCell ("Size" ).SetSelectable (false )) 261ui .table .SetCell (0 ,2 ,tview .NewTableCell ("Used" ).SetSelectable (false )) 262ui .table .SetCell (0 ,3 ,tview .NewTableCell ("Used part" ).SetSelectable (false )) 263ui .table .SetCell (0 ,4 ,tview .NewTableCell ("Free" ).SetSelectable (false )) 264ui .table .SetCell (0 ,5 ,tview .NewTableCell ("Mount point" ).SetSelectable (false )) 265 266var textColor ,sizeColor string 267if ui .UseColors { 268textColor = "[#3498db:-:b]" 269sizeColor = "[#edb20a:-:b]" 270 }else { 271textColor = "[white:-:b]" 272sizeColor = "[white:-:b]" 273 } 274 275ui .sortDevices () 276 277for i ,device := range ui .devices { 278totalUsage += device .GetUsage () 279ui .table .SetCell (i + 1 ,0 ,tview .NewTableCell (textColor + device .Name ).SetReference (ui .devices [i ])) 280ui .table .SetCell (i + 1 ,1 ,tview .NewTableCell (ui .formatSize (device .Size ,false ,true ))) 281ui .table .SetCell (i + 1 ,2 ,tview .NewTableCell (sizeColor + ui .formatSize (device .Size - device .Free ,false ,true ))) 282ui .table .SetCell (i + 1 ,3 ,tview .NewTableCell (getDeviceUsagePart (device ,ui .useOldSizeBar ))) 283ui .table .SetCell (i + 1 ,4 ,tview .NewTableCell (ui .formatSize (device .Free ,false ,true ))) 284ui .table .SetCell (i + 1 ,5 ,tview .NewTableCell (textColor + device .MountPoint ).SetReference (ui .devices [i ])) 285 } 286 287var footerNumberColor ,footerTextColor string 288if ui .UseColors { 289footerNumberColor = fmt .Sprintf ( 290"[%s:%s:b]" , 291ui .footerNumberColor , 292ui .footerBackgroundColor , 293 ) 294footerTextColor = fmt .Sprintf ( 295"[%s:%s:-]" , 296ui .footerTextColor , 297ui .footerBackgroundColor , 298 ) 299 }else { 300footerNumberColor = "[black:white:b]" 301footerTextColor = blackOnWhite 302 } 303 304ui .footerLabel .SetText ( 305" Total usage: " + 306footerNumberColor + 307ui .formatSize (totalUsage ,true ,false )+ 308footerTextColor + 309" Sorting by: " + ui .sortBy + " " + ui .sortOrder ) 310 311ui .table .Select (1 ,0 ) 312ui .table .SetSelectedFunc (ui .deviceItemSelected ) 313 314if ui .topDirPath != "" { 315for i ,device := range ui .devices { 316if device .MountPoint == ui .topDirPath { 317ui .table .Select (i + 1 ,0 ) 318break 319 } 320 } 321 } 322} 323 324func (ui * UI )showErr (msg string ,err error ) { 325text := msg 326if err != nil { 327text += ": " + err .Error () 328 } 329 330modal := tview .NewModal (). 331SetText (text ). 332AddButtons ([]string {"ok" }). 333SetDoneFunc (func (buttonIndex int ,buttonLabel string ) { 334ui .pages .RemovePage ("error" ) 335 }) 336 337if ! ui .UseColors { 338modal .SetBackgroundColor (tcell .ColorGray ) 339 } 340 341ui .pages .AddPage ("error" ,modal ,true ,true ) 342ui .app .SetFocus (modal ) 343} 344 345func (ui * UI )showErrFromGo (msg string ,err error ) { 346ui .app .QueueUpdateDraw (func () { 347ui .showErr (msg ,err ) 348 }) 349} 350 351func (ui * UI )showHelp () { 352text := tview .NewTextView ().SetDynamicColors (true ) 353text .SetBorder (true ).SetBorderPadding (2 ,2 ,2 ,2 ) 354text .SetBorderColor (tcell .ColorDefault ) 355text .SetTitle (" gdu help " ) 356text .SetScrollable (true ) 357 358formattedHelpText := ui .formatHelpTextFor () 359text .SetText (formattedHelpText ) 360 361maxHeight := strings .Count (formattedHelpText ,"\n" )+ 7 362_ ,height := ui .screen .Size () 363if height > maxHeight { 364height = maxHeight 365 } 366 367flex := tview .NewFlex (). 368AddItem (nil ,0 ,1 ,false ). 369AddItem (tview .NewFlex ().SetDirection (tview .FlexRow ). 370AddItem (nil ,0 ,1 ,false ). 371AddItem (text ,height ,1 ,false ). 372AddItem (nil ,0 ,1 ,false ),80 ,1 ,false ). 373AddItem (nil ,0 ,1 ,false ) 374 375ui .help = flex 376ui .pages .AddPage ("help" ,flex ,true ,true ) 377ui .app .SetFocus (text ) 378} 379 380func (ui * UI )formatHelpTextFor ()string { 381lines := strings .Split (helpText ,"\n" ) 382 383for i ,line := range lines { 384if ui .UseColors { 385lines [i ]= strings .ReplaceAll ( 386strings .ReplaceAll (line ,defaultColorBold ,"[red]" ), 387whiteOnBlack , 388"[white]" , 389 ) 390 } 391 392isFound := (strings .Contains (line ,"Empty file or directory" )|| 393strings .Contains (line ,"Delete file or directory" )|| 394strings .Contains (line ,"Move file or directory to trash" )) 395 396if ui .noDelete && isFound { 397lines [i ]+= helpDisabledSuffix 398 }else if ui .noDeleteWithFilter && isFound { 399lines [i ]+= " (disabled/filter)" 400 } 401 402if ui .noSpawnShell && (strings .Contains (line ,"Spawn shell in current directory" )|| 403strings .Contains (line ,"Open file or directory in external program" )) { 404lines [i ]+= helpDisabledSuffix 405 } 406 407if ui .noViewFile && strings .Contains (line ,"Show content of file" ) { 408lines [i ]+= helpDisabledSuffix 409 } 410 } 411 412return strings .Join (lines ,"\n" ) 413} 414 415func (ui * UI )formatTypeFilterInfo (numberColor ,textColor string )string { 416if ui .typeFilterValue == "" { 417return "" 418 } 419return " Type filter: " + numberColor + ui .typeFilterValue + textColor 420}