summaryrefslogtreecommitdiffstats
path: root/tui/background.go
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2024-03-30 21:10:12 +0100
committerDaniel Milde <daniel@milde.cz>2024-03-30 23:17:37 +0100
commit755b20d5f0b830f5ea3823112dd91f429598f687 (patch)
tree3ede6ca8f63488f82e5827b58a08c72a4b779929 /tui/background.go
parent7bfeb985f4d687a05781d4f870a128990b0c6638 (diff)
feat: delete/empty items in background
Diffstat (limited to 'tui/background.go')
-rw-r--r--tui/background.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/tui/background.go b/tui/background.go
new file mode 100644
index 0000000..813c3b9
--- /dev/null
+++ b/tui/background.go
@@ -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--
+}