-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
271 lines (215 loc) · 6.94 KB
/
main.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
// Bug: going to a folder and pressing buttons on the left side of the keyboard togther caused a panic
// Bug: not displaying the contents on the dir on first load
// TODO - add some colour
// TODO - add ctrl + backspace so i can delete the whole word and normal backspace to delete letters at a time
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
var globalDirectory string
type KeyValues struct {
value string
index int
}
func main() {
p := tea.NewProgram(initialModel(), tea.WithOutput(os.Stderr))
// if _, err := p.Run(); err != nil {
// log.Fatal(err)
// }
m, err := p.Run()
if err != nil {
}
if m, ok := m.(model); ok && m.textInput.Value() != "" {
fmt.Printf("%s\n", m.textInput.Value())
}
}
type (
errMsg error
)
type model struct {
textInput textinput.Model
directory string
path string
err error
keyMaps map[tea.KeyType]KeyValues
}
func initialPath() string {
c := exec.Command("pwd")
output, err := c.Output()
if err != nil {
// not quite sure what to add here yet
}
return string(output)
}
func makeKeyMaps() map[tea.KeyType]KeyValues {
keyMapper := make(map[tea.KeyType]KeyValues)
keyMapper[tea.KeyCtrlA] = KeyValues{value: "a", index: 0}
keyMapper[tea.KeyCtrlS] = KeyValues{value: "s", index: 1}
keyMapper[tea.KeyCtrlD] = KeyValues{value: "d", index: 2}
keyMapper[tea.KeyCtrlQ] = KeyValues{value: "q", index: 3}
keyMapper[tea.KeyCtrlW] = KeyValues{value: "w", index: 4}
keyMapper[tea.KeyCtrlE] = KeyValues{value: "e", index: 5}
keyMapper[tea.KeyCtrlZ] = KeyValues{value: "z", index: 6}
keyMapper[tea.KeyCtrlX] = KeyValues{value: "x", index: 7}
return keyMapper
}
func lastCharacter(str string) string {
return string(str[len(str)-1])
}
func listDirectory(path string) string {
strippedPath := strings.TrimSuffix(path, "\n")
keyMaps := makeKeyMaps()
shortcutAppender := func(list []string, key tea.KeyType, i int, value string) {
remove_newline := strings.TrimPrefix(value, "\n")
if i == keyMaps[key].index && remove_newline != "" {
key := keyMaps[key]
list[i] = key.value + " " + value
}
}
if strippedPath != "" {
c := exec.Command("ls", strippedPath)
output, err := c.Output()
// Putting a partial path into the ls command here causes an error
// e.g. ~/play/code/b
// the b represents the beginning of the bubble-tea directory
// the command cant handle that and errors out here
// this is where we then list the last path and then filter results
// TODO - check for this before executing the first ls
if err != nil {
previous_output := strings.Split(strippedPath, "/")
previous_output_minus_after_slash := strings.Join(previous_output[:len(previous_output)-1], "/")
co := exec.Command("ls", previous_output_minus_after_slash)
innerOutput, innerErr := co.Output()
if innerErr != nil {
}
var filtered = []string{}
for _, value := range strings.Split(string(innerOutput), "\n") {
test := strings.ToLower(value)
if strings.HasPrefix(test, strings.ToLower(previous_output[len(previous_output)-1])) {
// if strings.HasPrefix(string(value), previous_output[len(previous_output)-1]) {
filtered = append(filtered, string(value))
}
}
for i, value := range filtered {
shortcutAppender(filtered, tea.KeyCtrlA, i, value)
shortcutAppender(filtered, tea.KeyCtrlS, i, value)
shortcutAppender(filtered, tea.KeyCtrlD, i, value)
shortcutAppender(filtered, tea.KeyCtrlQ, i, value)
shortcutAppender(filtered, tea.KeyCtrlW, i, value)
shortcutAppender(filtered, tea.KeyCtrlE, i, value)
shortcutAppender(filtered, tea.KeyCtrlZ, i, value)
shortcutAppender(filtered, tea.KeyCtrlX, i, value)
}
return strings.Join(filtered, "\n")
}
list := strings.Split(string(output), "\n")
for i, value := range list {
shortcutAppender(list, tea.KeyCtrlA, i, value)
shortcutAppender(list, tea.KeyCtrlS, i, value)
shortcutAppender(list, tea.KeyCtrlD, i, value)
shortcutAppender(list, tea.KeyCtrlQ, i, value)
shortcutAppender(list, tea.KeyCtrlW, i, value)
shortcutAppender(list, tea.KeyCtrlE, i, value)
shortcutAppender(list, tea.KeyCtrlZ, i, value)
shortcutAppender(list, tea.KeyCtrlX, i, value)
}
return strings.Join(list, "\n")
} else {
return " path isnt empty "
}
}
func initialModel() model {
ti := textinput.New()
ti.Focus()
ti.CharLimit = 156
ti.Width = 80
ti.SetValue(initialPath())
return model{
textInput: ti,
path: initialPath(),
directory: listDirectory(initialPath()),
keyMaps: makeKeyMaps(),
err: nil,
}
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func updateInput(m model, inputValue string) {
m.textInput.SetValue(inputValue)
m.textInput.CursorEnd()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.directory = listDirectory(m.textInput.Value())
m.path = m.textInput.Value()
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlA,
tea.KeyCtrlS,
tea.KeyCtrlD,
tea.KeyCtrlQ,
tea.KeyCtrlW,
tea.KeyCtrlE,
tea.KeyCtrlZ,
tea.KeyCtrlX:
currentKey := m.keyMaps[msg.Type]
list := listDirectory(m.textInput.Value())
input := strings.Split(list, "\n")
var updateValue = m.textInput.Value()
remove_tea_key := strings.Split(input[currentKey.index], " ")[1]
// if last character is not /
split_path := strings.Split(updateValue, "/")
path_after_last_slash := split_path[len(split_path)-1]
if strings.HasPrefix(strings.ToLower(remove_tea_key), strings.ToLower(path_after_last_slash)) {
if string(updateValue[len(updateValue)-1]) != "/" {
// apparently i dont need this just yet
// updateValue = updateValue + "/"
}
// I am here and first letter prefixes work but multi letter ones dont
m.textInput.SetValue(strings.TrimSuffix(updateValue, path_after_last_slash) + remove_tea_key + "/")
} else {
if string(updateValue[len(updateValue)-1]) != "/" {
updateValue = updateValue + "/"
}
m.textInput.SetValue(updateValue + remove_tea_key + "/")
}
m.textInput.CursorEnd()
return m, cmd
case tea.KeyBackspace:
line := strings.Split(m.textInput.Value(), "/")
if lastCharacter(m.textInput.Value()) != "/" {
m.textInput.SetValue(strings.Join(line[:len(line)-1], "/") + "/")
} else {
m.textInput.SetValue(strings.Join(line[:len(line)-2], "/") + "/")
}
m.textInput.CursorEnd()
return m, cmd
case tea.KeyEnter:
// This is not where the program exits. On exit the loops stops running by NewProgram and we return the model.
return m, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
// putting this here is slow
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m model) View() string {
return fmt.Sprintf(
"%s\n%s",
m.textInput.View(),
listDirectory(m.textInput.Value()),
) + "\n"
}