summaryrefslogtreecommitdiffstats
path: root/tui/background.go
blob: 8974defb32d30b3b6cd4356b21e1d7fe3cb87528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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() {
	defer func() {
		if r := recover(); r != nil {
			ui.app.Stop()
			panic(r)
		}
	}()

	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 = actionEmpty
	} else {
		action = actionDelete
	}

	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.GetFilesLocked() {
			deleteItems = append(deleteItems, file)
		}
	} else {
		parentDir = ui.currentDir
		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().GetPath() == ui.currentDir.GetPath() {
		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--
}