summaryrefslogtreecommitdiffstats
path: root/tui/marked.go
blob: 129b53e17289819b0da60ce4e66a5844373f843e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package tui

import (
	"strconv"
	"strings"

	"github.com/dundee/gdu/v5/pkg/analyze"
	"github.com/dundee/gdu/v5/pkg/fs"
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func (ui *UI) fileItemMarked(row int) {
	if _, ok := ui.markedRows[row]; ok {
		delete(ui.markedRows, row)
	} else {
		ui.markedRows[row] = struct{}{}
	}
	ui.showDir()
	// select next row if possible
	ui.table.Select(min(row+1, ui.table.GetRowCount()-1), 0)
}

func (ui *UI) deleteMarked(shouldEmpty bool) {
	var action, acting string
	if shouldEmpty {
		action = actionEmpty
		acting = actingEmpty
	} else {
		action = actionDelete
		acting = actingDelete
	}

	var currentDir fs.Item
	var markedItems []fs.Item
	for row := range ui.markedRows {
		item := ui.table.GetCell(row, 0).GetReference().(fs.Item)
		markedItems = append(markedItems, item)
	}

	if ui.deleteInBackground {
		ui.queueForDeletion(markedItems, shouldEmpty)
		return
	}

	modal := tview.NewModal()
	ui.pages.AddPage(acting, modal, true, true)

	currentRow, _ := ui.table.GetSelection()

	var deleteFun func(fs.Item, fs.Item) error

	go func() {
		for _, one := range markedItems {
			ui.app.QueueUpdateDraw(func() {
				modal.SetText(
					// nolint: staticcheck // Why: fixed string
					strings.Title(acting) +
						" " +
						tview.Escape(one.GetName()) +
						"...",
				)
			})

			if shouldEmpty && !one.IsDir() {
				deleteFun = ui.emptier
			} else {
				deleteFun = ui.remover
			}

			var deleteItems []fs.Item
			if shouldEmpty && one.IsDir() {
				currentDir = one.(*analyze.Dir)
				for _, file := range currentDir.GetFiles() {
					deleteItems = append(deleteItems, file)
				}
			} else {
				currentDir = ui.currentDir
				deleteItems = append(deleteItems, one)
			}

			for _, item := range deleteItems {
				if err := deleteFun(currentDir, item); err != nil {
					msg := "Can't " + action + " " + tview.Escape(one.GetName())
					ui.app.QueueUpdateDraw(func() {
						ui.pages.RemovePage(acting)
						ui.showErr(msg, err)
					})
					if ui.done != nil {
						ui.done <- struct{}{}
					}
					return
				}
			}
		}

		ui.app.QueueUpdateDraw(func() {
			ui.pages.RemovePage(acting)
			ui.markedRows = make(map[int]struct{})
			x, y := ui.table.GetOffset()
			ui.showDir()
			ui.table.Select(min(currentRow, 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) confirmDeletionMarked(shouldEmpty bool) {
	var action string
	if shouldEmpty {
		action = actionEmpty
	} else {
		action = actionDelete
	}

	modal := tview.NewModal().
		SetText(
			"Are you sure you want to " +
				action + " [::b]" +
				strconv.Itoa(len(ui.markedRows)) +
				"[::-] items?",
		).
		AddButtons([]string{"yes", "no", "don't ask me again"}).
		SetDoneFunc(func(buttonIndex int, buttonLabel string) {
			switch buttonIndex {
			case 2:
				ui.askBeforeDelete = false
				fallthrough
			case 0:
				ui.deleteMarked(shouldEmpty)
			}
			ui.pages.RemovePage("confirm")
		})

	if !ui.UseColors {
		modal.SetBackgroundColor(tcell.ColorGray)
	} else {
		modal.SetBackgroundColor(tcell.ColorBlack)
	}
	modal.SetBorderColor(tcell.ColorDefault)

	ui.pages.AddPage("confirm", modal, true, true)
}