-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete/empty items in background
- Loading branch information
Showing
10 changed files
with
415 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
golang 1.21.5 | ||
golang 1.22.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package tui | ||
|
||
import ( | ||
"github.com/dundee/gdu/v5/pkg/analyze" | ||
"github.com/dundee/gdu/v5/pkg/fs" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
func (ui *UI) queueForDeletion(items []fs.Item, shouldEmpty bool) { | ||
go func() { | ||
for _, item := range items { | ||
ui.deleteQueue <- deleteQueueItem{item: item, shouldEmpty: shouldEmpty} | ||
} | ||
}() | ||
|
||
ui.markedRows = make(map[int]struct{}) | ||
} | ||
|
||
func (ui *UI) deleteWorker() { | ||
for item := range ui.deleteQueue { | ||
ui.deleteItem(item.item, item.shouldEmpty) | ||
} | ||
} | ||
|
||
func (ui *UI) deleteItem(item fs.Item, shouldEmpty bool) { | ||
ui.increaseActiveWorkers() | ||
defer ui.decreaseActiveWorkers() | ||
|
||
var action, acting string | ||
if shouldEmpty { | ||
action = "empty " | ||
} else { | ||
action = "delete " | ||
} | ||
|
||
var deleteFun func(fs.Item, fs.Item) error | ||
if shouldEmpty && !item.IsDir() { | ||
deleteFun = ui.emptier | ||
} else { | ||
deleteFun = ui.remover | ||
} | ||
|
||
var parentDir fs.Item | ||
var deleteItems []fs.Item | ||
if shouldEmpty && item.IsDir() { | ||
parentDir = item.(*analyze.Dir) | ||
for _, file := range item.GetFiles() { | ||
deleteItems = append(deleteItems, file) | ||
} | ||
} else { | ||
parentDir = item.GetParent() | ||
deleteItems = append(deleteItems, item) | ||
} | ||
|
||
for _, toDelete := range deleteItems { | ||
if err := deleteFun(parentDir, toDelete); err != nil { | ||
msg := "Can't " + action + tview.Escape(toDelete.GetName()) | ||
ui.app.QueueUpdateDraw(func() { | ||
ui.pages.RemovePage(acting) | ||
ui.showErr(msg, err) | ||
}) | ||
if ui.done != nil { | ||
ui.done <- struct{}{} | ||
} | ||
return | ||
} | ||
} | ||
|
||
if item.GetParent() == ui.currentDir { | ||
ui.app.QueueUpdateDraw(func() { | ||
row, _ := ui.table.GetSelection() | ||
x, y := ui.table.GetOffset() | ||
ui.showDir() | ||
ui.table.Select(min(row, ui.table.GetRowCount()-1), 0) | ||
ui.table.SetOffset(min(x, ui.table.GetRowCount()-1), y) | ||
}) | ||
} | ||
if ui.done != nil { | ||
ui.done <- struct{}{} | ||
} | ||
} | ||
|
||
func (ui *UI) increaseActiveWorkers() { | ||
ui.workersMut.Lock() | ||
defer ui.workersMut.Unlock() | ||
ui.activeWorkers++ | ||
} | ||
|
||
func (ui *UI) decreaseActiveWorkers() { | ||
ui.workersMut.Lock() | ||
defer ui.workersMut.Unlock() | ||
ui.activeWorkers-- | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package tui | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
func (ui *UI) toggleStatusBar(show bool) { | ||
var textColor, textBgColor tcell.Color | ||
if ui.UseColors { | ||
textColor = tcell.NewRGBColor(0, 0, 0) | ||
textBgColor = tcell.NewRGBColor(36, 121, 208) | ||
} else { | ||
textColor = tcell.NewRGBColor(0, 0, 0) | ||
textBgColor = tcell.NewRGBColor(255, 255, 255) | ||
} | ||
|
||
ui.grid.Clear() | ||
|
||
ui.statusMut.Lock() | ||
defer ui.statusMut.Unlock() | ||
|
||
if show { | ||
ui.status = tview.NewTextView().SetDynamicColors(true) | ||
ui.status.SetTextColor(textColor) | ||
ui.status.SetBackgroundColor(textBgColor) | ||
|
||
ui.grid.SetRows(1, 1, 0, 1, 1) | ||
ui.grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false). | ||
AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false). | ||
AddItem(ui.table, 2, 0, 1, 1, 0, 0, true). | ||
AddItem(ui.status, 3, 0, 1, 1, 0, 0, false). | ||
AddItem(ui.footer, 4, 0, 1, 1, 0, 0, false) | ||
return | ||
} | ||
ui.status = nil | ||
ui.grid.SetRows(1, 1, 0, 1) | ||
ui.grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false). | ||
AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false). | ||
AddItem(ui.table, 2, 0, 1, 1, 0, 0, true). | ||
AddItem(ui.footer, 3, 0, 1, 1, 0, 0, false) | ||
} | ||
|
||
func (ui *UI) updateStatusWorker() { | ||
for { | ||
ui.updateStatus() | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
} | ||
|
||
func (ui *UI) updateStatus() { | ||
ui.workersMut.Lock() | ||
cnt := ui.activeWorkers | ||
ui.workersMut.Unlock() | ||
|
||
ui.statusMut.RLock() | ||
status := ui.status | ||
ui.statusMut.RUnlock() | ||
|
||
if cnt == 0 && status == nil { | ||
return | ||
} | ||
|
||
if cnt > 0 && status == nil { | ||
ui.app.QueueUpdateDraw(func() { | ||
ui.toggleStatusBar(true) | ||
}) | ||
} else if cnt == 0 { | ||
ui.app.QueueUpdateDraw(func() { | ||
ui.toggleStatusBar(false) | ||
}) | ||
return | ||
} | ||
|
||
ui.app.QueueUpdateDraw(func() { | ||
msg := fmt.Sprintf(" Active background deletions: %d", cnt) | ||
ui.statusMut.RLock() | ||
ui.status.SetText(msg) | ||
ui.statusMut.RUnlock() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.