-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.go
312 lines (254 loc) · 6.75 KB
/
operations.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"io/fs"
"log"
"os"
"sort"
"path/filepath"
"golang.org/x/term"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/viewport"
"github.com/charmbracelet/lipgloss"
)
type selectFileMsg string
type tabMsg int
func (m model) Init() tea.Cmd {
return tea.EnterAltScreen
}
func tab(tabNumber int) tea.Cmd {
return func () tea.Msg {
return tabMsg(tabNumber)
}
}
func selectFile(name string) tea.Cmd {
return func () tea.Msg {
return selectFileMsg(name)
}
}
func (m *model) CloseTab() tea.Cmd {
ct := m.CurrentTab
ct.active = false
ct.filter = ""
ct.files = []fs.DirEntry{}
ct.filteredFiles = []fs.DirEntry{}
tabIndex := m.popTabHistory()
for tabIndex != -1 {
if m.tabs[tabIndex].active {
break
}
tabIndex = m.popTabHistory()
}
if tabIndex == -1 {
m.writeLastd()
return tea.Quit
} else {
// We Select tab *number* here
return tab(tabIndex+1)
}
}
// This will activate the tab if not already. Returns true if was previously active
func (m *model) SelectTab(tabIndex int) bool {
wasActive := m.tabs[tabIndex].active
m.tabs[tabIndex].active = true
m.CurrentTabIndex = tabIndex
m.CurrentTab = &m.tabs[tabIndex]
m.tabHistory = append(m.tabHistory, tabIndex)
return wasActive
}
// Returns the index of the last tab open or -1 if there is no more tab history
func (m *model) popTabHistory() int {
if len(m.tabHistory) == 0 {
return -1
}
lastTab := m.tabHistory[len(m.tabHistory) - 1]
//Remove Last Entry
m.tabHistory = m.tabHistory[:len(m.tabHistory) - 1]
return lastTab
}
func (td *tabData) SetSort(sort int) {
td.sort = sort
td.SetFilter("")
}
func (m *model) MoveCursor(linesDown int) {
ct := m.CurrentTab
// Move cursor by reqested amount
ct.cursor += linesDown
// Ensure we are in bounds
ct.cursor = Max(0, ct.cursor)
ct.cursor = Min(len(ct.filteredFiles)-1, ct.cursor)
// Scroll viewport if necessary
if (linesDown > 0) {
m.checkScrollDown()
} else {
m.checkScrollUp()
}
}
func (m *model) MoveCursorTop() {
ct := m.CurrentTab
m.MoveCursor(-ct.cursor)
}
func (m *model) MoveCursorBottom() {
ct := m.CurrentTab
m.MoveCursor(len(ct.filteredFiles)-1-ct.cursor)
}
func (m *model) handleResize(msg tea.WindowSizeMsg) {
m.termWidth, m.termHeight, _ = term.GetSize(int(os.Stdout.Fd()))
headerHeight := lipgloss.Height(m.headerView())
footerHeight := lipgloss.Height(m.footerView())
verticalMarginHeight := headerHeight + footerHeight
m.viewportHeight = msg.Height-verticalMarginHeight
if !m.firstResize {
// Since this program is using the full size of the viewport we
// need to wait until we've received the window dimensions before
// we can initialize the viewport. The initial dimensions come in
// quickly, though asynchronously, which is why we wait for them
// here.
m.viewport = viewport.New(msg.Width, m.viewportHeight)
m.viewport.YPosition = headerHeight
m.viewport.SetContent("")
m.firstResize = true
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - verticalMarginHeight
}
m.viewport.SetContent(m.generateContent())
// viewport must be resetting YOffset on window resize somehow
// We'll manually match and rescroll
m.checkScrollDown()
}
func (td *tabData) AddHistory(path string) {
td.dirHistory = td.dirHistory[0:td.dirHistoryIndex]
td.dirHistory = append(td.dirHistory, path)
td.dirHistoryIndex++
//log.Printf("Added %s to History. di: %d", path, td.dirHistoryIndex)
}
func (m *model) GoHistoryBack() tea.Cmd {
ct := m.CurrentTab
if (ct.dirHistoryIndex == 1) {
return nil
}
ct.dirHistoryIndex--
dir := ct.dirHistory[ct.dirHistoryIndex-1]
//log.Printf("Changing to %s di: %d dhl: %d", dir, ct.dirHistoryIndex, len(ct.dirHistory))
err := ct.ChangeDirectory(dir)
if err != nil {
parent := filepath.Dir(dir)
m.appendError("Error getting contents of "+dir+". Folder may have been removed. Changing directory to "+parent+".")
return cd(parent)
}
return refresh()
}
func (m *model) GoHistoryForward() tea.Cmd {
log.Printf("GoHistoryForward")
ct := m.CurrentTab
if (ct.dirHistoryIndex > len(ct.dirHistory)-1) {
return nil
}
ct.dirHistoryIndex++
dir := ct.dirHistory[ct.dirHistoryIndex-1]
//log.Printf("Changing to %s di: %d dhl: %d", dir, ct.dirHistoryIndex, len(ct.dirHistory))
err := ct.ChangeDirectory(dir)
if err != nil {
parent := filepath.Dir(dir)
m.appendError("Error getting contents of "+dir+". Folder may have been removed. Changing directory to "+parent+".")
return cd(parent)
}
return refresh()
}
func (td *tabData) JumpToFile(name string) {
log.Printf("Looking for %s", name)
for i, ff := range(td.filteredFiles) {
if ff.Name() == name {
log.Printf("Found %s at %d", name, i)
td.cursor = i
return
}
}
}
func (m *model) MoveNextSelected() {
indicies := m.SelectedIndicies()
for _, si := range(indicies) {
log.Printf("m.CurrentTab.cursor: %d", m.CurrentTab.cursor)
if (si > m.CurrentTab.cursor) {
m.CurrentTab.cursor = si
break
}
}
m.checkScrollDown()
}
func (m *model) MovePrevSelected() {
indicies := m.SelectedIndicies()
for i:=len(indicies)-1; i>=0; i-- {
si := indicies[i]
if (si < m.CurrentTab.cursor) {
m.CurrentTab.cursor = si
break
}
}
m.checkScrollUp()
}
func (td *tabData) filterFiles() {
if td.filter == "" {
td.filteredFiles = td.files
} else {
td.filteredFiles = []fs.DirEntry{}
for _, f := range td.files {
if (!td.filtered(f)) {
td.filteredFiles = append(td.filteredFiles, f)
}
}
}
if (td.sort == nameSort) {
sort.Sort(ByName(td.filteredFiles))
}
if (td.sort == modifiedSort) {
sort.Sort(ByMod(td.filteredFiles))
}
if (td.sort == sizeSort) {
sort.Sort(BySize(td.filteredFiles))
}
}
func (td *tabData) SetFilter(filter string) {
td.filter = filter
td.filterFiles()
td.cursor = 0
}
func (td *tabData) ReRunFilter() {
td.filterFiles()
}
func (m *model) Select(absdir string, file fs.DirEntry) {
m.selectedFiles = append(m.selectedFiles, selectedFile{directory: absdir, file: file})
}
func (m *model) ClearSelections() {
m.selectedFiles = []selectedFile{}
}
func (m *model) DeselectAll() tea.Cmd {
m.ClearSelections()
return refresh()
}
func (m *model) SelectAll() tea.Cmd {
ct := m.CurrentTab
for _, f := range(ct.filteredFiles) {
i := m.Selected(ct.absdir, f)
if i == -1 {
m.Select(ct.absdir, f)
}
}
return refresh()
}
func (m *model) Unselect(i int) {
// This doesn't maintain ordering, but is much faster
// https://stackoverflow.com/a/37335777/319894
m.selectedFiles[i] = m.selectedFiles[len(m.selectedFiles)-1]
m.selectedFiles = m.selectedFiles[:len(m.selectedFiles)-1]
}
func (m *model) ToggleSelected() {
ct := m.CurrentTab
hoveredDirEntry := ct.filteredFiles[ct.cursor]
i := m.Selected(ct.absdir, hoveredDirEntry)
if i == -1 {
m.Select(ct.absdir, hoveredDirEntry)
} else {
m.Unselect(i)
}
}