fork/gdu
git clone https://git.julia.blue/fork/gdu
04c9958
ignore
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 .pages .RemovePage ("progress" ) 286ui .showDir () 287ui .table .Select (0 ,0 ) 288ui .app .SetFocus (ui .table ) 289} 290 291// exitPreview leaves the mid-scan preview and restores the scanning progress modal. 292func (ui * UI )exitPreview () { 293ui .previewing = false 294ui .currentDir = ui .previewSavedDir 295ui .previewSavedDir = nil 296if ui .progressFlex != nil { 297ui .pages .AddPage ("progress" ,ui .progressFlex ,true ,true ) 298 } 299ui .app .SetFocus (ui .table ) 300} 301 302// handlePreviewKeys handles input while a mid-scan preview is shown. Navigation 303// and sorting are allowed; destructive or external actions are intentionally 304// ignored because the tree is still being built. 305func (ui * UI )handlePreviewKeys (key * tcell.EventKey )* tcell.EventKey { 306if ui .pages .HasPage ("help" ) { 307return key 308 } 309 310if key .Key ()== tcell .KeyTab || key .Key ()== tcell .KeyEsc { 311ui .exitPreview () 312return nil 313 } 314if key .Key ()== tcell .KeyLeft { 315ui .previewLeft () 316return nil 317 } 318if key .Key ()== tcell .KeyRight { 319ui .handleRight () 320return nil 321 } 322 323switch key .Rune () { 324case '?' : 325ui .showHelp () 326return nil 327case 'h' : 328ui .previewLeft () 329return nil 330case 'l' : 331ui .handleRight () 332return nil 333case 's' ,'C' ,'n' ,'M' : 334ui .handleSorting (key ) 335return nil 336case 'a' ,'B' ,'c' ,'m' : 337ui .handleToggles (key ) 338return nil 339 } 340 341// up/down/pgup/pgdn and Enter are handled by the table itself 342return key 343} 344 345// previewLeft navigates to the parent dir within the preview, or leaves the 346// preview when already at its root. 347func (ui * UI )previewLeft () { 348if ui .currentDir == nil || ui .currentDir .GetParent ()== nil { 349ui .exitPreview () 350return 351 } 352ui .fileItemSelected (0 ,0 ) 353} 354 355func (ui * UI )handleHelp (key * tcell.EventKey )* tcell.EventKey { 356if key .Rune ()== '?' { 357if ui .pages .HasPage ("help" ) { 358ui .pages .RemovePage ("help" ) 359ui .app .SetFocus (ui .table ) 360return nil 361 } 362ui .showHelp () 363return nil 364 } 365return key 366} 367 368func (ui * UI )handleShell (key * tcell.EventKey )* tcell.EventKey { 369if key .Rune ()== 'b' { 370if ui .isInArchive () { 371ui .showErr ("Spawning shell is not supported in archives" ,nil ) 372return nil 373 } 374if ui .noSpawnShell { 375previousHeaderText := ui .header .GetText (false ) 376 377// show feedback to user 378ui .header .SetText (" Shell spawning is disabled!" ) 379 380go func () { 381time .Sleep (2 * time .Second ) 382ui .app .QueueUpdateDraw (func () { 383ui .header .Clear () 384ui .header .SetText (previousHeaderText ) 385 }) 386 }() 387 388return nil 389 } 390ui .spawnShell () 391return nil 392 } 393return key 394} 395 396func (ui * UI )handleLeftRight (key * tcell.EventKey )* tcell.EventKey { 397if key .Rune ()== 'h' || key .Key ()== tcell .KeyLeft { 398ui .handleLeft () 399return nil 400 } 401 402if key .Rune ()== 'l' || key .Key ()== tcell .KeyRight { 403ui .handleRight () 404return nil 405 } 406return key 407} 408 409func (ui * UI )handleFiltering (key * tcell.EventKey )* tcell.EventKey { 410if key .Key ()!= tcell .KeyTab { 411return key 412 } 413if ui .filteringInput != nil { 414ui .filtering = true 415ui .app .SetFocus (ui .filteringInput ) 416return nil 417 } 418if ui .typeFilteringInput != nil { 419ui .typeFiltering = true 420ui .app .SetFocus (ui .typeFilteringInput ) 421return nil 422 } 423return key 424} 425 426// nolint: funlen // Why: there's a lot of options to handle 427func (ui * UI )handleMainActions (key * tcell.EventKey )* tcell.EventKey { 428switch key .Rune () { 429case 'd' : 430if ui .isInArchive () { 431ui .showErr ("Deletion is not supported in archives" ,nil ) 432return nil 433 } 434ui .handleDelete (ActionDelete ) 435case 'e' : 436if ui .isInArchive () { 437ui .showErr ("Deletion is not supported in archives" ,nil ) 438return nil 439 } 440ui .handleDelete (ActionEmpty ) 441case 'D' : 442if ui .isInArchive () { 443ui .showErr ("Deletion is not supported in archives" ,nil ) 444return nil 445 } 446ui .handleDelete (ActionMoveToTrash ) 447case 'v' : 448if ui .isInArchive () { 449ui .showErr ("Viewing content is not supported in archives" ,nil ) 450return nil 451 } 452if ui .noViewFile { 453previousHeaderText := ui .header .GetText (false ) 454 455ui .header .SetText (" Viewing files is disabled!" ) 456 457go func () { 458time .Sleep (2 * time .Second ) 459ui .app .QueueUpdateDraw (func () { 460ui .header .Clear () 461ui .header .SetText (previousHeaderText ) 462 }) 463 }() 464 465return nil 466 } 467ui .showFile () 468case 'o' : 469if ui .noSpawnShell { 470previousHeaderText := ui .header .GetText (false ) 471 472// show feedback to user 473ui .header .SetText (" Opening items is disabled!" ) 474 475go func () { 476time .Sleep (2 * time .Second ) 477ui .app .QueueUpdateDraw (func () { 478ui .header .Clear () 479ui .header .SetText (previousHeaderText ) 480 }) 481 }() 482return nil 483 } 484ui .openItem () 485case 'i' : 486ui .showInfo () 487case 'a' ,'B' ,'c' ,'m' : 488ui .handleToggles (key ) 489case 'r' : 490if ui .currentDir != nil { 491ui .rescanDir () 492 } 493case 'E' : 494ui .confirmExport () 495return nil 496case 's' ,'C' ,'n' ,'M' : 497ui .handleSorting (key ) 498case '/' : 499ui .showFilterInput () 500return nil 501case 'T' : 502ui .showTypeFilterInput () 503return nil 504case ' ' : 505ui .handleMark () 506case 'p' : 507ui .printMarked () 508return nil 509case 'I' : 510ui .ignoreItem () 511 } 512return key 513} 514 515func (ui * UI )handleToggles (key * tcell.EventKey ) { 516switch key .Rune () { 517case 'a' : 518ui .ShowApparentSize = ! ui .ShowApparentSize 519case 'B' : 520ui .ShowRelativeSize = ! ui .ShowRelativeSize 521case 'c' : 522ui .showItemCount = ! ui .showItemCount 523case 'm' : 524ui .showMtime = ! ui .showMtime 525 } 526if ui .currentDir != nil { 527row ,column := ui .table .GetSelection () 528ui .showDir () 529ui .table .Select (row ,column ) 530 } 531} 532 533func (ui * UI )handleSorting (key * tcell.EventKey ) { 534switch key .Rune () { 535case 's' : 536ui .setSorting ("size" ) 537case 'C' : 538ui .setSorting ("itemCount" ) 539case 'n' : 540ui .setSorting ("name" ) 541case 'M' : 542ui .setSorting ("mtime" ) 543 } 544} 545 546func (ui * UI )handleLeft () { 547if ui .currentDirPath == ui .topDirPath { 548if ui .devices != nil { 549ui .currentDir = nil 550err := ui .ListDevices (ui .getter ) 551if err != nil { 552ui .showErr ("Error listing devices" ,err ) 553 } 554 }else if ui .browseParentDirs { 555ui .analyzeParentOfTopDir () 556 } 557return 558 } 559if ui .currentDir != nil { 560ui .fileItemSelected (0 ,0 ) 561 } 562} 563 564func (ui * UI )analyzeParentOfTopDir () { 565if ui .currentDir == nil || ui .isInArchive () { 566return 567 } 568 569currentPath := ui .currentDir .GetPath () 570parentPath := filepath .Dir (currentPath ) 571if parentPath == currentPath { 572return 573 } 574 575ui .Analyzer .ResetProgress () 576ui .linkedItems = make (fs.HardLinkedItems ) 577 578if err := analyzeParentPath (ui ,parentPath ,nil );err != nil { 579ui .showErr ("Error analyzing parent directory" ,err ) 580 } 581} 582 583func (ui * UI )handleRight () { 584row ,column := ui .table .GetSelection () 585if ui .currentDirPath != ui .topDirPath && row == 0 {// do not select /.. 586return 587 } 588 589if ui .currentDir != nil { 590ui .fileItemSelected (row ,column ) 591 }else { 592ui .deviceItemSelected (row ,column ) 593 } 594} 595 596func (ui * UI )handleDelete (action DeleteAction ) { 597if ui .currentDir == nil { 598return 599 } 600// do not allow deleting parent dir 601row ,column := ui .table .GetSelection () 602selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 603if ! ok || selectedFile == ui .currentDir .GetParent () { 604return 605 } 606 607if ui .askBeforeDelete { 608ui .confirmDeletion (action ) 609 }else { 610ui .delete (action ) 611 } 612} 613 614func (ui * UI )handleMark () { 615if ui .currentDir == nil { 616return 617 } 618// do not allow deleting parent dir 619row ,column := ui .table .GetSelection () 620selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 621if ! ok || selectedFile == ui .currentDir .GetParent () { 622return 623 } 624 625ui .fileItemMarked (row ) 626} 627 628func (ui * UI )ignoreItem () { 629if ui .currentDir == nil { 630return 631 } 632// do not allow ignoring parent dir 633row ,column := ui .table .GetSelection () 634selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 635if ! ok || selectedFile == ui .currentDir .GetParent () { 636return 637 } 638 639ui .AddIgnoreDirPath (selectedFile .GetPath ()) 640ui .rescanDir () 641}