fork/gdu
git clone https://git.julia.blue/fork/gdu
04c9958
ignore
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 ui .ShowRelativeSize { 124if item .GetUsage ()> maxUsage { 125maxUsage = item .GetUsage () 126 } 127if item .GetSize ()> maxSize { 128maxSize = item .GetSize () 129 } 130 }else { 131maxSize += item .GetSize () 132maxUsage += item .GetUsage () 133 } 134i ++ 135 } 136 137for item := range ui .currentDir .GetFiles (sortBy ,sortOrder ) { 138if ui .filterValue != "" && ! strings .Contains ( 139strings .ToLower (item .GetName ()), 140strings .ToLower (ui .filterValue ), 141 ) { 142continue 143 } 144 145if ! ui .matchesTypeFilter (item .GetName (),item .IsDir ()) { 146continue 147 } 148 149totalUsage += item .GetUsage () 150totalSize += item .GetSize () 151itemCount += item .GetItemCount () 152 153_ ,marked := ui .markedRows [rowIndex ] 154 155var cell * tview.TableCell 156var reference fs.Item 157 158// Check if this directory can be collapsed 159if item .IsDir () { 160var collapsedPath * CollapsedPath 161if ui .collapsePath { 162collapsedPath = findCollapsiblePath (item ) 163 } 164 165if collapsedPath != nil { 166// Format as collapsed path 167cell = tview .NewTableCell (ui .formatCollapsedRow (collapsedPath ,maxUsage ,maxSize ,marked )) 168// Reference should point to the deepest directory for navigation 169reference = collapsedPath .DeepestDir 170 }else { 171// Regular directory formatting 172cell = tview .NewTableCell (ui .formatFileRow (item ,maxUsage ,maxSize ,marked )) 173reference = item 174 } 175 }else { 176// Regular file formatting 177cell = tview .NewTableCell (ui .formatFileRow (item ,maxUsage ,maxSize ,marked )) 178reference = item 179 } 180 181cell .SetReference (reference ) 182 183switch { 184case marked : 185cell .SetStyle (tcell.Style {}.Foreground (ui .markedTextColor )) 186cell .SetBackgroundColor (ui .markedBackgroundColor ) 187default : 188cell .SetStyle (tcell.Style {}.Foreground (tcell .ColorDefault )) 189 } 190 191ui .table .SetCell (rowIndex ,0 ,cell ) 192rowIndex ++ 193 } 194 195var footerNumberColor ,footerTextColor string 196if ui .UseColors { 197footerNumberColor = fmt .Sprintf ( 198"[%s:%s:b]" , 199ui .footerNumberColor , 200ui .footerBackgroundColor , 201 ) 202footerTextColor = fmt .Sprintf ( 203"[%s:%s:-]" , 204ui .footerTextColor , 205ui .footerBackgroundColor , 206 ) 207 }else { 208footerNumberColor = "[black:white:b]" 209footerTextColor = blackOnWhite 210 } 211 212selected := "" 213if len (ui .markedRows )> 0 { 214selected = " Selected items: " + footerNumberColor + 215strconv .Itoa (len (ui .markedRows ))+ footerTextColor 216 } 217 218timeFilterText := ui .formatTimeFilterInfo () 219 220typeFilterText := ui .formatTypeFilterInfo (footerNumberColor ,footerTextColor ) 221 222ui .footerLabel .SetText ( 223selected + footerTextColor + 224" Total disk usage: " + 225footerNumberColor + 226ui .formatSize (totalUsage ,true ,false )+ 227" Apparent size: " + 228footerNumberColor + 229ui .formatSize (totalSize ,true ,false )+ 230" Items: " + footerNumberColor + fmt .Sprintf ("%d" ,itemCount )+ 231footerTextColor + 232" Sorting by: " + ui .sortBy + " " + ui .sortOrder + 233typeFilterText + 234timeFilterText ) 235 236ui .table .Select (0 ,0 ) 237ui .table .ScrollToBeginning () 238 239if ! ui .filtering && ! ui .typeFiltering { 240ui .app .SetFocus (ui .table ) 241 } 242} 243 244func (ui * UI )showDevices () { 245var totalUsage int64 246 247ui .table .Clear () 248ui .table .SetCell (0 ,0 ,tview .NewTableCell ("Device name" ).SetSelectable (false )) 249ui .table .SetCell (0 ,1 ,tview .NewTableCell ("Size" ).SetSelectable (false )) 250ui .table .SetCell (0 ,2 ,tview .NewTableCell ("Used" ).SetSelectable (false )) 251ui .table .SetCell (0 ,3 ,tview .NewTableCell ("Used part" ).SetSelectable (false )) 252ui .table .SetCell (0 ,4 ,tview .NewTableCell ("Free" ).SetSelectable (false )) 253ui .table .SetCell (0 ,5 ,tview .NewTableCell ("Mount point" ).SetSelectable (false )) 254 255var textColor ,sizeColor string 256if ui .UseColors { 257textColor = "[#3498db:-:b]" 258sizeColor = "[#edb20a:-:b]" 259 }else { 260textColor = "[white:-:b]" 261sizeColor = "[white:-:b]" 262 } 263 264ui .sortDevices () 265 266for i ,device := range ui .devices { 267totalUsage += device .GetUsage () 268ui .table .SetCell (i + 1 ,0 ,tview .NewTableCell (textColor + device .Name ).SetReference (ui .devices [i ])) 269ui .table .SetCell (i + 1 ,1 ,tview .NewTableCell (ui .formatSize (device .Size ,false ,true ))) 270ui .table .SetCell (i + 1 ,2 ,tview .NewTableCell (sizeColor + ui .formatSize (device .Size - device .Free ,false ,true ))) 271ui .table .SetCell (i + 1 ,3 ,tview .NewTableCell (getDeviceUsagePart (device ,ui .useOldSizeBar ))) 272ui .table .SetCell (i + 1 ,4 ,tview .NewTableCell (ui .formatSize (device .Free ,false ,true ))) 273ui .table .SetCell (i + 1 ,5 ,tview .NewTableCell (textColor + device .MountPoint ).SetReference (ui .devices [i ])) 274 } 275 276var footerNumberColor ,footerTextColor string 277if ui .UseColors { 278footerNumberColor = fmt .Sprintf ( 279"[%s:%s:b]" , 280ui .footerNumberColor , 281ui .footerBackgroundColor , 282 ) 283footerTextColor = fmt .Sprintf ( 284"[%s:%s:-]" , 285ui .footerTextColor , 286ui .footerBackgroundColor , 287 ) 288 }else { 289footerNumberColor = "[black:white:b]" 290footerTextColor = blackOnWhite 291 } 292 293ui .footerLabel .SetText ( 294" Total usage: " + 295footerNumberColor + 296ui .formatSize (totalUsage ,true ,false )+ 297footerTextColor + 298" Sorting by: " + ui .sortBy + " " + ui .sortOrder ) 299 300ui .table .Select (1 ,0 ) 301ui .table .SetSelectedFunc (ui .deviceItemSelected ) 302 303if ui .topDirPath != "" { 304for i ,device := range ui .devices { 305if device .MountPoint == ui .topDirPath { 306ui .table .Select (i + 1 ,0 ) 307break 308 } 309 } 310 } 311} 312 313func (ui * UI )showErr (msg string ,err error ) { 314text := msg 315if err != nil { 316text += ": " + err .Error () 317 } 318 319modal := tview .NewModal (). 320SetText (text ). 321AddButtons ([]string {"ok" }). 322SetDoneFunc (func (buttonIndex int ,buttonLabel string ) { 323ui .pages .RemovePage ("error" ) 324 }) 325 326if ! ui .UseColors { 327modal .SetBackgroundColor (tcell .ColorGray ) 328 } 329 330ui .pages .AddPage ("error" ,modal ,true ,true ) 331ui .app .SetFocus (modal ) 332} 333 334func (ui * UI )showErrFromGo (msg string ,err error ) { 335ui .app .QueueUpdateDraw (func () { 336ui .showErr (msg ,err ) 337 }) 338} 339 340func (ui * UI )showHelp () { 341text := tview .NewTextView ().SetDynamicColors (true ) 342text .SetBorder (true ).SetBorderPadding (2 ,2 ,2 ,2 ) 343text .SetBorderColor (tcell .ColorDefault ) 344text .SetTitle (" gdu help " ) 345text .SetScrollable (true ) 346 347formattedHelpText := ui .formatHelpTextFor () 348text .SetText (formattedHelpText ) 349 350maxHeight := strings .Count (formattedHelpText ,"\n" )+ 7 351_ ,height := ui .screen .Size () 352if height > maxHeight { 353height = maxHeight 354 } 355 356flex := tview .NewFlex (). 357AddItem (nil ,0 ,1 ,false ). 358AddItem (tview .NewFlex ().SetDirection (tview .FlexRow ). 359AddItem (nil ,0 ,1 ,false ). 360AddItem (text ,height ,1 ,false ). 361AddItem (nil ,0 ,1 ,false ),80 ,1 ,false ). 362AddItem (nil ,0 ,1 ,false ) 363 364ui .help = flex 365ui .pages .AddPage ("help" ,flex ,true ,true ) 366ui .app .SetFocus (text ) 367} 368 369func (ui * UI )formatHelpTextFor ()string { 370lines := strings .Split (helpText ,"\n" ) 371 372for i ,line := range lines { 373if ui .UseColors { 374lines [i ]= strings .ReplaceAll ( 375strings .ReplaceAll (line ,defaultColorBold ,"[red]" ), 376whiteOnBlack , 377"[white]" , 378 ) 379 } 380 381isFound := (strings .Contains (line ,"Empty file or directory" )|| 382strings .Contains (line ,"Delete file or directory" )|| 383strings .Contains (line ,"Move file or directory to trash" )) 384 385if ui .noDelete && isFound { 386lines [i ]+= helpDisabledSuffix 387 }else if ui .noDeleteWithFilter && isFound { 388lines [i ]+= " (disabled/filter)" 389 } 390 391if ui .noSpawnShell && (strings .Contains (line ,"Spawn shell in current directory" )|| 392strings .Contains (line ,"Open file or directory in external program" )) { 393lines [i ]+= helpDisabledSuffix 394 } 395 396if ui .noViewFile && strings .Contains (line ,"Show content of file" ) { 397lines [i ]+= helpDisabledSuffix 398 } 399 } 400 401return strings .Join (lines ,"\n" ) 402} 403 404func (ui * UI )formatTypeFilterInfo (numberColor ,textColor string )string { 405if ui .typeFilterValue == "" { 406return "" 407 } 408return " Type filter: " + numberColor + ui .typeFilterValue + textColor 409}