- Notifications
You must be signed in to change notification settings - Fork0
/
Copy pathmotions.go
203 lines (155 loc) · 3.93 KB
/
motions.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
package main
import (
"os"
"path/filepath"
)
func (ts*terminalSession)moveUpSelection(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
iflen(ts.cwdFiles)<1 {
return
}
ts.selectionPos-=n
ts.previewOffsetV=0
ts.previewOffsetH=0
// Reset if we are before the beginning of the files
ifts.selectionPos<0 {
ts.selectionPos=0
}
// If the selection is outside of the range, adjust the offset
ifts.selectionPos<ts.mainOffset {
ts.mainOffset=ts.selectionPos
}
ts.refreshQueue()
}
func (ts*terminalSession)moveDownSelection(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
iflen(ts.cwdFiles)<1 {
return
}
ts.selectionPos+=n
ts.previewOffsetV=0
ts.previewOffsetH=0
// Reset if we are beyond the end of the files
ifts.selectionPos>len(ts.cwdFiles)-1 {
ts.selectionPos=len(ts.cwdFiles)-1
}
// If the selection is outside of the range, adjust the offset
ifts.selectionPos>ts.height+ts.mainOffset-1-BottomRows {
ts.mainOffset=ts.selectionPos-ts.height+1+BottomRows
}
ts.refreshQueue()
}
func (ts*terminalSession)moveUpDir() {
ts.mu.Lock()
deferts.mu.Unlock()
ts.mainOffset=0
ts.previewOffsetV=0
ts.previewOffsetH=0
_,fileName:=filepath.Split(ts.cwd)
ts.cwd=filepath.Dir(ts.cwd)
cwdFiles,err:=os.ReadDir(ts.cwd)
iferr!=nil {
return
}
ts.cwdFiles=ts.sortFunc(cwdFiles)
// Get the index of the directory we just moved out of to select it
fori,file:=rangets.cwdFiles {
iffile.Name()==fileName {
ts.selectionPos=i
}
}
ifts.selectionPos>ts.height+ts.mainOffset-1-BottomRows {
ts.mainOffset+=ts.selectionPos- (ts.height-1-BottomRows)
}
ts.refreshQueue()
}
func (ts*terminalSession)moveDownDir() {
ts.mu.Lock()
deferts.mu.Unlock()
// If the current directory has no files do nothing
iflen(ts.cwdFiles)==0 {
return
}
ts.mainOffset=0
ts.previewOffsetV=0
ts.previewOffsetH=0
// If the selection isn't a directory do nothing
// Should I add symlink directories as well?
if!ts.cwdFiles[ts.selectionPos].IsDir() {
return
}
// Get the full path
fileName:=ts.cwdFiles[ts.selectionPos].Name()
newDir:=filepath.Join(ts.cwd,fileName)
newFiles,err:=os.ReadDir(newDir)
iferr!=nil {
// Better error handling?
return
}
ts.cwd=newDir
ts.cwdFiles=newFiles
ts.selectionPos=0
ts.refreshQueue()
}
func (ts*terminalSession)moveUpPreview(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
ts.previewOffsetV-=n
// Reset if we are before the beginning of the files
ifts.previewOffsetV<0 {
ts.previewOffsetV=0
}
ts.refreshQueue()
}
func (ts*terminalSession)moveDownPreview(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
ifts.previewLen<=ts.height-BottomRows {
return
}
ts.previewOffsetV+=n
// Reset if we are beyond the end of the files
// TODO: it seems there is always an extra empty line at the end, check it out
// Probably to do with splitting on \n and an empty last string
ifts.previewOffsetV>ts.previewLen-(ts.height-BottomRows) {
ts.previewOffsetV=ts.previewLen- (ts.height-BottomRows)
}
ts.refreshQueue()
}
func (ts*terminalSession)moveLeftPreview(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
ts.previewOffsetH-=n
// Reset if we are before the first char
ifts.previewOffsetH<0 {
ts.previewOffsetH=0
}
ts.refreshQueue()
}
func (ts*terminalSession)moveRightPreview(nint) {
ts.mu.Lock()
deferts.mu.Unlock()
ts.previewOffsetH+=n
// TODO: Reset if we are after the last char of the file
// This might be a bit convoluted with the way the code is built atm
ts.refreshQueue()
}
func (ts*terminalSession)goHome() {
ts.mu.Lock()
deferts.mu.Unlock()
home,err:=os.UserHomeDir()
// If homedir is not found, just return
iferr!=nil {
return
}
ts.selectionPos=0
ts.mainOffset=0
ts.previewOffsetV=0
ts.previewOffsetH=0
ts.cwd=home
// No error handling, needs to change?
ts.cwdFiles,_=os.ReadDir(home)
ts.refreshQueue()
}