fork/gdu
git clone https://git.julia.blue/fork/gdu
9ca5c60
subvolume
1package tui 2 3import ( 4"fmt" 5"path/filepath" 6"time" 7 8"github.com/dundee/gdu/v5/pkg/fs" 9"github.com/gdamore/tcell/v2" 10"github.com/rivo/tview" 11) 12 13var analyzeParentPath = func (ui * UI ,path string ,parentDir fs.Item )error { 14return ui .AnalyzePath (path ,parentDir ) 15} 16 17func (ui * UI )keyPressed (key * tcell.EventKey )* tcell.EventKey { 18if key .Key ()== tcell .KeyExit { 19ui .handleSignalEvent (key ) 20return nil 21 } 22 23if ui .handleCtrlZ (key )== nil { 24return nil 25 } 26 27if ui .pages .HasPage ("file" )|| ui .pages .HasPage ("export" ) { 28return key // send event to primitive 29 } 30if ui .filtering || ui .typeFiltering { 31return key 32 } 33 34key = ui .handleClosingModals (key ) 35if key == nil { 36return nil 37 } 38key = ui .handleInfoPageEvents (key ) 39if key == nil { 40return nil 41 } 42key = ui .handleQuit (key ) 43if key == nil { 44return nil 45 } 46 47if ui .pages .HasPage ("confirm" ) { 48return ui .handleConfirmation (key ) 49 } 50 51if key .Key ()== tcell .KeyCtrlC && ui .cancelScan () { 52return nil 53 } 54 55if ui .previewing { 56return ui .handlePreviewKeys (key ) 57 } 58 59if ui .pages .HasPage ("progress" )|| 60ui .pages .HasPage ("deleting" )|| 61ui .pages .HasPage ("emptying" )|| 62ui .pages .HasPage ("moving to trash" ) { 63// allow peeking at the results found so far during a scan 64if key .Key ()== tcell .KeyTab && ui .pages .HasPage ("progress" ) { 65ui .enterPreview () 66return nil 67 } 68return key 69 } 70 71key = ui .handleHelp (key ) 72if key == nil { 73return nil 74 } 75 76if ui .pages .HasPage ("help" ) { 77return key 78 } 79 80key = ui .handleShell (key ) 81if key == nil { 82return nil 83 } 84 85key = ui .handleLeftRight (key ) 86if key == nil { 87return nil 88 } 89 90key = ui .handleFiltering (key ) 91if key == nil { 92return nil 93 } 94 95return ui .handleMainActions (key ) 96} 97 98func (ui * UI )handleClosingModals (key * tcell.EventKey )* tcell.EventKey { 99if key .Key ()== tcell .KeyEsc || key .Rune ()== 'q' { 100if ui .pages .HasPage ("help" ) { 101ui .pages .RemovePage ("help" ) 102ui .app .SetFocus (ui .table ) 103return nil 104 } 105if ui .pages .HasPage ("info" ) { 106ui .pages .RemovePage ("info" ) 107ui .app .SetFocus (ui .table ) 108return nil 109 } 110 } 111return key 112} 113 114func (ui * UI )handleConfirmation (key * tcell.EventKey )* tcell.EventKey { 115if key .Rune ()== 'h' { 116return tcell .NewEventKey (tcell .KeyLeft ,0 ,0 ) 117 } 118if key .Rune ()== 'l' { 119return tcell .NewEventKey (tcell .KeyRight ,0 ,0 ) 120 } 121return key 122} 123 124func (ui * UI )handleInfoPageEvents (key * tcell.EventKey )* tcell.EventKey { 125if ui .pages .HasPage ("info" ) { 126switch key .Rune () { 127case 'i' : 128ui .pages .RemovePage ("info" ) 129ui .app .SetFocus (ui .table ) 130return nil 131case '?' : 132return nil 133 } 134 135if key .Key ()== tcell .KeyUp || 136key .Key ()== tcell .KeyDown || 137key .Rune ()== 'j' || 138key .Rune ()== 'k' { 139row ,column := ui .table .GetSelection () 140if (key .Key ()== tcell .KeyUp || key .Rune ()== 'k' )&& row > 0 { 141row -- 142 }else if (key .Key ()== tcell .KeyDown || key .Rune ()== 'j' )&& 143row + 1 < ui .table .GetRowCount () { 144row ++ 145 } 146ui .table .Select (row ,column ) 147 } 148ui .showInfo ()// refresh file info after any change 149 } 150return key 151} 152 153// handle ctrl+z job control 154func (ui * UI )handleCtrlZ (key * tcell.EventKey )* tcell.EventKey { 155if key .Key ()== tcell .KeyCtrlZ { 156ui .app .Suspend (func () { 157termApp := ui .app .(* tview.Application ) 158termApp .Lock () 159defer termApp .Unlock () 160 161err := stopProcess () 162if err != nil { 163ui .showErr ("Error sending STOP signal" ,err ) 164 } 165 }) 166return nil 167 } 168 169return key 170} 171 172// confirmQuitMinScanDuration is the scan time above which quitting asks for 173// confirmation, so a long scan is not lost by an accidental key press. 174const confirmQuitMinScanDuration = 3 * time .Second 175 176func (ui * UI )handleQuit (key * tcell.EventKey )* tcell.EventKey { 177clearTerminalProgress () 178 179// do not re-trigger quitting while a confirmation dialog is open 180if ui .pages .HasPage ("confirm" ) { 181return key 182 } 183 184switch key .Rune () { 185case 'Q' : 186ui .quit (true ) 187return nil 188case 'q' : 189ui .quit (false ) 190return nil 191 } 192return key 193} 194 195// quit asks for confirmation when there are scan results worth protecting, 196// otherwise it quits immediately. 197func (ui * UI )quit (printCurrentDirPath bool ) { 198if ui .shouldConfirmQuit () { 199ui .confirmQuitDialog (printCurrentDirPath ) 200return 201 } 202ui .doQuit (printCurrentDirPath ) 203} 204 205// shouldConfirmQuit returns true when quitting would discard the work of a scan 206// that took a noticeable amount of time, whether it is still running or already 207// finished. 208func (ui * UI )shouldConfirmQuit ()bool { 209if ! ui .confirmQuit { 210return false 211 } 212if ui .scanning { 213return time .Since (ui .scanStart )>= confirmQuitMinScanDuration 214 } 215return ui .currentDir != nil && ui .scanDuration >= confirmQuitMinScanDuration 216} 217 218func (ui * UI )doQuit (printCurrentDirPath bool ) { 219ui .app .Stop () 220ui .printMarkedPaths () 221if printCurrentDirPath { 222fmt .Fprintf (ui .output ,"%s\n" ,sanitizePathForDisplay (ui .currentDirPath )) 223 } 224} 225 226func (ui * UI )confirmQuitDialog (printCurrentDirPath bool ) { 227var text string 228if ui .scanning { 229text = "A scan has been running for " + 230time .Since (ui .scanStart ).Round (time .Second ).String ()+ ".\n\n" + 231"Do you really want to quit gdu and abandon it?" 232 }else { 233text = "Do you really want to quit gdu?\n\n" + 234"This scan took " + ui .scanDuration .Round (time .Second ).String ()+ 235" and the results are not saved.\n" + 236"Choose \"no\" and press E to export them first." 237 } 238modal := tview .NewModal (). 239SetText (text ). 240AddButtons ([]string {"no" ,"yes" ,"don't ask me again" }). 241SetDoneFunc (func (buttonIndex int ,buttonLabel string ) { 242ui .pages .RemovePage ("confirm" ) 243ui .app .SetFocus (ui .table ) 244switch buttonIndex { 245case 2 : 246ui .confirmQuit = false 247fallthrough 248case 1 : 249ui .doQuit (printCurrentDirPath ) 250 } 251 }) 252 253if ! ui .UseColors { 254modal .SetBackgroundColor (tcell .ColorGray ) 255 }else { 256modal .SetBackgroundColor (tcell .ColorBlack ) 257 } 258modal .SetBorderColor (tcell .ColorDefault ) 259 260ui .pages .AddPage ("confirm" ,modal ,true ,true ) 261} 262 263// enterPreview switches from the scanning progress modal to a read-only, 264// point-in-time view of the directory tree discovered so far. The view does not 265// auto-refresh: pressing Tab again returns to the progress modal, and entering 266// the preview once more takes a fresh snapshot. 267func (ui * UI )enterPreview () { 268analyzer ,ok := ui .Analyzer .(interface {GetCurrentDir () fs.Item }) 269if ! ok { 270return 271 } 272root := analyzer .GetCurrentDir () 273if root == nil { 274return // nothing scanned yet 275 } 276 277// compute aggregated sizes on the partial tree using a throwaway hard-link 278// map so the running scan's accounting is left untouched 279root .UpdateStats (make (fs.HardLinkedItems )) 280 281ui .previewing = true 282ui .previewSavedDir = ui .currentDir 283ui .currentDir = root 284ui .markedRows = make (map [int ]struct {}) 285ui .ignoredRows = make (map [int ]struct {}) 286ui .pages .RemovePage ("progress" ) 287ui .showDir () 288ui .table .Select (0 ,0 ) 289ui .app .SetFocus (ui .table ) 290} 291 292// exitPreview leaves the mid-scan preview and restores the scanning progress modal. 293func (ui * UI )exitPreview () { 294ui .previewing = false 295ui .currentDir = ui .previewSavedDir 296ui .previewSavedDir = nil 297if ui .progressFlex != nil { 298ui .pages .AddPage ("progress" ,ui .progressFlex ,true ,true ) 299 } 300ui .app .SetFocus (ui .table ) 301} 302 303// handlePreviewKeys handles input while a mid-scan preview is shown. Navigation 304// and sorting are allowed; destructive or external actions are intentionally 305// ignored because the tree is still being built. 306func (ui * UI )handlePreviewKeys (key * tcell.EventKey )* tcell.EventKey { 307if ui .pages .HasPage ("help" ) { 308return key 309 } 310 311if key .Key ()== tcell .KeyTab || key .Key ()== tcell .KeyEsc { 312ui .exitPreview () 313return nil 314 } 315if key .Key ()== tcell .KeyLeft { 316ui .previewLeft () 317return nil 318 } 319if key .Key ()== tcell .KeyRight { 320ui .handleRight () 321return nil 322 } 323 324switch key .Rune () { 325case '?' : 326ui .showHelp () 327return nil 328case 'h' : 329ui .previewLeft () 330return nil 331case 'l' : 332ui .handleRight () 333return nil 334case 's' ,'C' ,'n' ,'M' : 335ui .handleSorting (key ) 336return nil 337case 'a' ,'B' ,'c' ,'m' : 338ui .handleToggles (key ) 339return nil 340 } 341 342// up/down/pgup/pgdn and Enter are handled by the table itself 343return key 344} 345 346// previewLeft navigates to the parent dir within the preview, or leaves the 347// preview when already at its root. 348func (ui * UI )previewLeft () { 349if ui .currentDir == nil || ui .currentDir .GetParent ()== nil { 350ui .exitPreview () 351return 352 } 353ui .fileItemSelected (0 ,0 ) 354} 355 356func (ui * UI )handleHelp (key * tcell.EventKey )* tcell.EventKey { 357if key .Rune ()== '?' { 358if ui .pages .HasPage ("help" ) { 359ui .pages .RemovePage ("help" ) 360ui .app .SetFocus (ui .table ) 361return nil 362 } 363ui .showHelp () 364return nil 365 } 366return key 367} 368 369func (ui * UI )handleShell (key * tcell.EventKey )* tcell.EventKey { 370if key .Rune ()== 'b' { 371if ui .isInArchive () { 372ui .showErr ("Spawning shell is not supported in archives" ,nil ) 373return nil 374 } 375if ui .noSpawnShell { 376previousHeaderText := ui .header .GetText (false ) 377 378// show feedback to user 379ui .header .SetText (" Shell spawning is disabled!" ) 380 381go func () { 382time .Sleep (2 * time .Second ) 383ui .app .QueueUpdateDraw (func () { 384ui .header .Clear () 385ui .header .SetText (previousHeaderText ) 386 }) 387 }() 388 389return nil 390 } 391ui .spawnShell () 392return nil 393 } 394return key 395} 396 397func (ui * UI )handleLeftRight (key * tcell.EventKey )* tcell.EventKey { 398if key .Rune ()== 'h' || key .Key ()== tcell .KeyLeft { 399ui .handleLeft () 400return nil 401 } 402 403if key .Rune ()== 'l' || key .Key ()== tcell .KeyRight { 404ui .handleRight () 405return nil 406 } 407return key 408} 409 410func (ui * UI )handleFiltering (key * tcell.EventKey )* tcell.EventKey { 411if key .Key ()!= tcell .KeyTab { 412return key 413 } 414if ui .filteringInput != nil { 415ui .filtering = true 416ui .app .SetFocus (ui .filteringInput ) 417return nil 418 } 419if ui .typeFilteringInput != nil { 420ui .typeFiltering = true 421ui .app .SetFocus (ui .typeFilteringInput ) 422return nil 423 } 424return key 425} 426 427// nolint: funlen // Why: there's a lot of options to handle 428func (ui * UI )handleMainActions (key * tcell.EventKey )* tcell.EventKey { 429switch key .Rune () { 430case 'd' : 431if ui .isInArchive () { 432ui .showErr ("Deletion is not supported in archives" ,nil ) 433return nil 434 } 435ui .handleDelete (ActionDelete ) 436case 'e' : 437if ui .isInArchive () { 438ui .showErr ("Deletion is not supported in archives" ,nil ) 439return nil 440 } 441ui .handleDelete (ActionEmpty ) 442case 'D' : 443if ui .isInArchive () { 444ui .showErr ("Deletion is not supported in archives" ,nil ) 445return nil 446 } 447ui .handleDelete (ActionMoveToTrash ) 448case 'v' : 449if ui .isInArchive () { 450ui .showErr ("Viewing content is not supported in archives" ,nil ) 451return nil 452 } 453if ui .noViewFile { 454previousHeaderText := ui .header .GetText (false ) 455 456ui .header .SetText (" Viewing files is disabled!" ) 457 458go func () { 459time .Sleep (2 * time .Second ) 460ui .app .QueueUpdateDraw (func () { 461ui .header .Clear () 462ui .header .SetText (previousHeaderText ) 463 }) 464 }() 465 466return nil 467 } 468ui .showFile () 469case 'o' : 470if ui .noSpawnShell { 471previousHeaderText := ui .header .GetText (false ) 472 473// show feedback to user 474ui .header .SetText (" Opening items is disabled!" ) 475 476go func () { 477time .Sleep (2 * time .Second ) 478ui .app .QueueUpdateDraw (func () { 479ui .header .Clear () 480ui .header .SetText (previousHeaderText ) 481 }) 482 }() 483return nil 484 } 485ui .openItem () 486case 'i' : 487ui .showInfo () 488case 'a' ,'B' ,'c' ,'m' : 489ui .handleToggles (key ) 490case 'r' : 491if ui .currentDir != nil { 492ui .rescanDir () 493 } 494case 'E' : 495ui .confirmExport () 496return nil 497case 's' ,'C' ,'n' ,'M' : 498ui .handleSorting (key ) 499case '/' : 500ui .showFilterInput () 501return nil 502case 'T' : 503ui .showTypeFilterInput () 504return nil 505case ' ' : 506ui .handleMark () 507case 'p' : 508ui .printMarked () 509return nil 510case 'I' : 511ui .ignoreItem () 512 } 513return key 514} 515 516func (ui * UI )handleToggles (key * tcell.EventKey ) { 517switch key .Rune () { 518case 'a' : 519ui .ShowApparentSize = ! ui .ShowApparentSize 520case 'B' : 521ui .ShowRelativeSize = ! ui .ShowRelativeSize 522case 'c' : 523ui .showItemCount = ! ui .showItemCount 524case 'm' : 525ui .showMtime = ! ui .showMtime 526 } 527if ui .currentDir != nil { 528row ,column := ui .table .GetSelection () 529ui .showDir () 530ui .table .Select (row ,column ) 531 } 532} 533 534func (ui * UI )handleSorting (key * tcell.EventKey ) { 535switch key .Rune () { 536case 's' : 537ui .setSorting ("size" ) 538case 'C' : 539ui .setSorting ("itemCount" ) 540case 'n' : 541ui .setSorting ("name" ) 542case 'M' : 543ui .setSorting ("mtime" ) 544 } 545} 546 547func (ui * UI )handleLeft () { 548if ui .currentDirPath == ui .topDirPath { 549if ui .devices != nil { 550ui .currentDir = nil 551err := ui .ListDevices (ui .getter ) 552if err != nil { 553ui .showErr ("Error listing devices" ,err ) 554 } 555 }else if ui .browseParentDirs { 556ui .analyzeParentOfTopDir () 557 } 558return 559 } 560if ui .currentDir != nil { 561ui .fileItemSelected (0 ,0 ) 562 } 563} 564 565func (ui * UI )analyzeParentOfTopDir () { 566if ui .currentDir == nil || ui .isInArchive () { 567return 568 } 569 570currentPath := ui .currentDir .GetPath () 571parentPath := filepath .Dir (currentPath ) 572if parentPath == currentPath { 573return 574 } 575 576ui .Analyzer .ResetProgress () 577ui .linkedItems = make (fs.HardLinkedItems ) 578 579if err := analyzeParentPath (ui ,parentPath ,nil );err != nil { 580ui .showErr ("Error analyzing parent directory" ,err ) 581 } 582} 583 584func (ui * UI )handleRight () { 585row ,column := ui .table .GetSelection () 586if ui .currentDirPath != ui .topDirPath && row == 0 {// do not select /.. 587return 588 } 589 590if ui .currentDir != nil { 591ui .fileItemSelected (row ,column ) 592 }else { 593ui .deviceItemSelected (row ,column ) 594 } 595} 596 597func (ui * UI )handleDelete (action DeleteAction ) { 598if ui .currentDir == nil { 599return 600 } 601// do not allow deleting parent dir 602row ,column := ui .table .GetSelection () 603selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 604if ! ok || selectedFile == ui .currentDir .GetParent () { 605return 606 } 607 608if ui .askBeforeDelete { 609ui .confirmDeletion (action ) 610 }else { 611ui .delete (action ) 612 } 613} 614 615func (ui * UI )handleMark () { 616if ui .currentDir == nil { 617return 618 } 619// do not allow deleting parent dir 620row ,column := ui .table .GetSelection () 621selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 622if ! ok || selectedFile == ui .currentDir .GetParent () { 623return 624 } 625 626ui .fileItemMarked (row ) 627} 628 629func (ui * UI )ignoreItem () { 630if ui .currentDir == nil { 631return 632 } 633// do not allow ignoring parent dir 634row ,column := ui .table .GetSelection () 635selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 636if ! ok || selectedFile == ui .currentDir .GetParent () { 637return 638 } 639 640if _ ,ok := ui .ignoredRows [row ];ok { 641delete (ui .ignoredRows ,row ) 642 }else { 643ui .ignoredRows [row ]= struct {}{} 644 } 645ui .showDir () 646// select next row if possible 647ui .table .Select (min (row + 1 ,ui .table .GetRowCount ()- 1 ),0 ) 648}