fork/gdu
git clone https://git.julia.blue/fork/gdu
34f3996
master
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 ,DontAskAgain }). 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 { 376ui .showMessage (" Shell spawning is disabled!" ) 377return nil 378 } 379ui .spawnShell () 380return nil 381 } 382return key 383} 384 385func (ui * UI )handleLeftRight (key * tcell.EventKey )* tcell.EventKey { 386if key .Rune ()== 'h' || key .Key ()== tcell .KeyLeft { 387ui .handleLeft () 388return nil 389 } 390 391if key .Rune ()== 'l' || key .Key ()== tcell .KeyRight { 392ui .handleRight () 393return nil 394 } 395return key 396} 397 398func (ui * UI )handleFiltering (key * tcell.EventKey )* tcell.EventKey { 399if key .Key ()!= tcell .KeyTab { 400return key 401 } 402if ui .filteringInput != nil { 403ui .filtering = true 404ui .app .SetFocus (ui .filteringInput ) 405return nil 406 } 407if ui .typeFilteringInput != nil { 408ui .typeFiltering = true 409ui .app .SetFocus (ui .typeFilteringInput ) 410return nil 411 } 412return key 413} 414 415// nolint: funlen // Why: there's a lot of options to handle 416func (ui * UI )handleMainActions (key * tcell.EventKey )* tcell.EventKey { 417switch key .Rune () { 418case 'd' : 419if ui .isInArchive () { 420ui .showErr ("Deletion is not supported in archives" ,nil ) 421return nil 422 } 423ui .handleDelete (ActionDelete ) 424case 'e' : 425if ui .isInArchive () { 426ui .showErr ("Deletion is not supported in archives" ,nil ) 427return nil 428 } 429ui .handleDelete (ActionEmpty ) 430case 'D' : 431if ui .isInArchive () { 432ui .showErr ("Deletion is not supported in archives" ,nil ) 433return nil 434 } 435ui .handleDelete (ActionMoveToTrash ) 436case 'v' : 437if ui .isInArchive () { 438ui .showErr ("Viewing content is not supported in archives" ,nil ) 439return nil 440 } 441if ui .noViewFile { 442ui .showMessage (" Viewing files is disabled!" ) 443return nil 444 } 445ui .showFile () 446case 'o' : 447if ui .noSpawnShell { 448ui .showMessage (" Opening items is disabled!" ) 449return nil 450 } 451ui .openItem () 452case 'i' : 453ui .showInfo () 454case 'a' ,'B' ,'c' ,'m' : 455ui .handleToggles (key ) 456case 'r' : 457if ui .currentDir != nil { 458ui .rescanDir () 459 } 460case 'E' : 461ui .confirmExport () 462return nil 463case 's' ,'C' ,'n' ,'M' : 464ui .handleSorting (key ) 465case '/' : 466ui .showFilterInput () 467return nil 468case 'T' : 469ui .showTypeFilterInput () 470return nil 471case ' ' : 472ui .handleMark () 473case 'p' : 474ui .printMarked () 475return nil 476case 'y' : 477ui .copySelectedPath () 478return nil 479case 'I' : 480ui .ignoreItem () 481 } 482return key 483} 484 485func (ui * UI )handleToggles (key * tcell.EventKey ) { 486switch key .Rune () { 487case 'a' : 488ui .ShowApparentSize = ! ui .ShowApparentSize 489case 'B' : 490ui .ShowRelativeSize = ! ui .ShowRelativeSize 491case 'c' : 492ui .showItemCount = ! ui .showItemCount 493case 'm' : 494ui .showMtime = ! ui .showMtime 495 } 496if ui .currentDir != nil { 497row ,column := ui .table .GetSelection () 498ui .showDir () 499ui .table .Select (row ,column ) 500 } 501} 502 503func (ui * UI )handleSorting (key * tcell.EventKey ) { 504switch key .Rune () { 505case 's' : 506ui .setSorting ("size" ) 507case 'C' : 508ui .setSorting ("itemCount" ) 509case 'n' : 510ui .setSorting ("name" ) 511case 'M' : 512ui .setSorting ("mtime" ) 513 } 514} 515 516func (ui * UI )handleLeft () { 517if ui .currentDirPath == ui .topDirPath { 518if ui .devices != nil { 519ui .currentDir = nil 520err := ui .ListDevices (ui .getter ) 521if err != nil { 522ui .showErr ("Error listing devices" ,err ) 523 } 524 }else if ui .browseParentDirs { 525ui .analyzeParentOfTopDir () 526 } 527return 528 } 529if ui .currentDir != nil { 530ui .fileItemSelected (0 ,0 ) 531 } 532} 533 534func (ui * UI )analyzeParentOfTopDir () { 535// the virtual root has no parent on disk to browse up into 536if ui .currentDir == nil || ui .isInArchive ()|| ui .atVirtualRoot () { 537return 538 } 539 540currentPath := ui .currentDir .GetPath () 541parentPath := filepath .Dir (currentPath ) 542if parentPath == currentPath { 543return 544 } 545 546ui .Analyzer .ResetProgress () 547ui .linkedItems = make (fs.HardLinkedItems ) 548 549if err := analyzeParentPath (ui ,parentPath ,nil );err != nil { 550ui .showErr ("Error analyzing parent directory" ,err ) 551 } 552} 553 554func (ui * UI )handleRight () { 555row ,column := ui .table .GetSelection () 556if ui .currentDirPath != ui .topDirPath && row == 0 {// do not select /.. 557return 558 } 559 560if ui .currentDir != nil { 561ui .fileItemSelected (row ,column ) 562 }else { 563ui .deviceItemSelected (row ,column ) 564 } 565} 566 567func (ui * UI )handleDelete (action DeleteAction ) { 568if ui .currentDir == nil { 569return 570 } 571// do not allow deleting parent dir 572row ,column := ui .table .GetSelection () 573selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 574if ! ok || selectedFile == ui .currentDir .GetParent () { 575return 576 } 577 578if ui .askBeforeDelete { 579ui .confirmDeletion (action ) 580 }else { 581ui .delete (action ) 582 } 583} 584 585func (ui * UI )handleMark () { 586if ui .currentDir == nil { 587return 588 } 589// do not allow deleting parent dir 590row ,column := ui .table .GetSelection () 591selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 592if ! ok || selectedFile == ui .currentDir .GetParent () { 593return 594 } 595 596ui .fileItemMarked (row ) 597} 598 599func (ui * UI )ignoreItem () { 600if ui .currentDir == nil { 601return 602 } 603// do not allow ignoring parent dir 604row ,column := ui .table .GetSelection () 605selectedFile ,ok := ui .table .GetCell (row ,column ).GetReference ().(fs.Item ) 606if ! ok || selectedFile == ui .currentDir .GetParent () { 607return 608 } 609 610if _ ,ok := ui .ignoredRows [row ];ok { 611delete (ui .ignoredRows ,row ) 612 }else { 613ui .ignoredRows [row ]= struct {}{} 614 } 615ui .showDir () 616// select next row if possible 617ui .table .Select (min (row + 1 ,ui .table .GetRowCount ()- 1 ),0 ) 618}