-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
357 lines (310 loc) · 8.77 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/creack/pty"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// Constants for key bindings and colors
const (
KeySave = tcell.KeyCtrlS
KeyQuit = tcell.KeyCtrlQ
KeyFocusTerminal = tcell.KeyCtrlT
KeyFocusEditor = tcell.KeyCtrlE
KeyFocusFileExplorer = tcell.KeyCtrlF
KeyCustomizeTerminal = tcell.KeyCtrlA
ColorGreen = tcell.ColorGreen
)
// UI represents the main UI components
type UI struct {
app *tview.Application
root *tview.Flex
fileExplorer *tview.TreeView
editor *tview.TextArea
output *tview.TextView
terminal *tview.TextView
}
// TerminalState represents the state of the terminal
type TerminalState struct {
pty *os.File
cmd *exec.Cmd
done chan struct{}
}
var (
ui UI
termState TerminalState
currentFile string
)
func main() {
var err error
ui.app = tview.NewApplication()
if err = createUI(); err != nil {
log.Fatalf("Failed to create UI: %v", err)
}
if err = setupKeyBindings(); err != nil {
log.Fatalf("Failed to set up key bindings: %v", err)
}
if err = ui.app.SetRoot(ui.root, true).EnableMouse(true).Run(); err != nil {
log.Fatalf("Error running application: %v", err)
}
}
// createUI initializes and sets up the user interface components
func createUI() error {
ui.root = tview.NewFlex().SetDirection(tview.FlexRow)
menuBar := createMenuBar()
ui.root.AddItem(menuBar, 1, 0, false)
content := tview.NewFlex().SetDirection(tview.FlexColumn)
var err error
ui.fileExplorer, err = createFileExplorer()
if err != nil {
return fmt.Errorf("failed to create file explorer: %w", err)
}
content.AddItem(ui.fileExplorer, 30, 0, true)
rightPanel := tview.NewFlex().SetDirection(tview.FlexRow)
ui.editor = createEditor()
ui.output = createOutput()
ui.terminal, err = createTerminal()
if err != nil {
return fmt.Errorf("failed to create terminal: %w", err)
}
rightPanel.AddItem(ui.editor, 0, 2, false)
rightPanel.AddItem(ui.output, 0, 1, false)
rightPanel.AddItem(ui.terminal, 0, 1, false)
content.AddItem(rightPanel, 0, 1, false)
ui.root.AddItem(content, 0, 1, true)
return nil
}
// setupKeyBindings configures the global key bindings for the application
func setupKeyBindings() error {
ui.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case KeySave:
if err := saveFile(); err != nil {
ui.output.SetText(fmt.Sprintf("Error saving file: %s", err))
}
return nil
case KeyQuit:
ui.app.Stop()
return nil
case KeyFocusTerminal:
ui.app.SetFocus(ui.terminal)
return nil
case KeyFocusEditor:
ui.app.SetFocus(ui.editor)
return nil
case KeyFocusFileExplorer:
ui.app.SetFocus(ui.fileExplorer)
return nil
case KeyCustomizeTerminal:
if ui.app.GetFocus() == ui.terminal {
customizeTerminal()
return nil
}
}
return event
})
return nil
}
// createMenuBar creates and returns the menu bar component
func createMenuBar() *tview.TextView {
menuBar := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWrap(false)
menuText := `[yellow]Ctrl+S[-] Save [yellow]Ctrl+Q[-] Quit [yellow]Ctrl+T[-] Terminal [yellow]Ctrl+E[-] Editor [yellow]Ctrl+F[-] Files [yellow]Ctrl+C[-] Customize Terminal`
menuBar.SetText(menuText)
return menuBar
}
// createFileExplorer creates and returns the file explorer component
func createFileExplorer() (*tview.TreeView, error) {
root := tview.NewTreeNode(".").
SetColor(ColorGreen)
if err := populateTree(root, "."); err != nil {
return nil, fmt.Errorf("failed to populate tree: %w", err)
}
tree := tview.NewTreeView().
SetRoot(root).
SetCurrentNode(root)
tree.SetSelectedFunc(func(node *tview.TreeNode) {
reference := node.GetReference()
if reference == nil {
return
}
path := reference.(string)
if err := loadFile(path); err != nil {
ui.output.SetText(fmt.Sprintf("Error loading file: %s", err))
}
})
return tree, nil
}
// populateTre recursively populates the file explorer tree
func populateTree(node *tview.TreeNode, path string) error {
files, err := os.ReadDir(path)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
for _, file := range files {
child := tview.NewTreeNode(file.Name()).
SetSelectable(true)
if file.IsDir() {
child.SetColor(ColorGreen)
if err := populateTree(child, filepath.Join(path, file.Name())); err != nil {
return err
}
} else {
child.SetReference(filepath.Join(path, file.Name()))
}
node.AddChild(child)
}
return nil
}
// createEditor creates and returns the text editor component
func createEditor() *tview.TextArea {
return tview.NewTextArea().
SetPlaceholder("No file loaded.")
}
// createOutput creates and returns the output view component
func createOutput() *tview.TextView {
output := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true)
output.SetBorder(true).SetTitle("Output")
return output
}
// createTerminal creates and returns the terminal component
func createTerminal() (*tview.TextView, error) {
terminal := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true)
terminal.SetBorder(true).SetTitle("Terminal")
termState.cmd = exec.Command("bash")
var err error
termState.pty, err = pty.Start(termState.cmd)
if err != nil {
return nil, fmt.Errorf("failed to start pty: %w", err)
}
termState.done = make(chan struct{})
go func() {
defer close(termState.done)
for {
buf := make([]byte, 1024)
n, err := termState.pty.Read(buf)
if err != nil {
if err == io.EOF {
return
}
log.Printf("Error reading from pty: %v", err)
return
}
processedOutput := processANSI(buf[:n])
ui.app.QueueUpdateDraw(func() {
terminal.Write(processedOutput)
})
}
}()
terminal.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
handleTerminalInput(event)
return nil
})
return terminal, nil
}
// handleTerminalInput handles input to the terminal
func handleTerminalInput(event *tcell.EventKey) {
switch event.Key() {
case tcell.KeyRune:
_, _ = termState.pty.Write([]byte(string(event.Rune())))
case tcell.KeyEnter:
_, _ = termState.pty.Write([]byte("\n"))
case tcell.KeyBackspace, tcell.KeyBackspace2:
_, _ = termState.pty.Write([]byte{0x7f})
case tcell.KeyTab:
_, _ = termState.pty.Write([]byte{0x09})
case tcell.KeyEscape:
_, _ = termState.pty.Write([]byte{0x1b})
default:
if event.Key() >= tcell.KeyCtrlA && event.Key() <= tcell.KeyCtrlZ {
_, _ = termState.pty.Write([]byte{byte(event.Key() - tcell.KeyCtrlA + 1)})
}
}
}
// processANSI processes ANSI escape sequences and returns cleaned output
func processANSI(input []byte) []byte {
var output []byte
inEscapeSeq := false
for _, b := range input {
if b == 0x1b { // ESC character
inEscapeSeq = true
continue
}
if inEscapeSeq {
if (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') {
inEscapeSeq = false
}
continue
}
if b >= 32 && b != 127 { // Printable ASCII characters
output = append(output, b)
}
}
return output
}
// customizeTerminal creates and displays a form for customizing the terminal colors
func customizeTerminal() {
bgInput := tview.NewInputField().SetLabel("Background Color")
textInput := tview.NewInputField().SetLabel("Text Color")
form := tview.NewForm().
AddFormItem(bgInput).
AddFormItem(textInput).
AddButton("Save", func() {
bgColor := bgInput.GetText()
textColor := textInput.GetText()
ui.terminal.SetBackgroundColor(tcell.GetColor(bgColor))
ui.terminal.SetTextColor(tcell.GetColor(textColor))
ui.app.SetRoot(ui.root, true)
ui.app.SetFocus(ui.terminal)
}).
AddButton("Cancel", func() {
ui.app.SetRoot(ui.root, true)
ui.app.SetFocus(ui.terminal)
})
form.SetBorder(true).SetTitle("Customize Terminal")
formFlex := tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(form, 10, 1, true).
AddItem(nil, 0, 1, false), 40, 1, true).
AddItem(nil, 0, 1, false)
ui.app.SetRoot(formFlex, true)
}
// loadFile loads the content of a file into the editor
func loadFile(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
ui.editor.SetText(string(content), true)
currentFile = path
ui.output.SetText(fmt.Sprintf("Loaded file: %s", path))
return nil
}
// saveFile saves the content of the editor to the current file
func saveFile() error {
if currentFile == "" {
return fmt.Errorf("no file loaded")
}
content := ui.editor.GetText()
err := os.WriteFile(currentFile, []byte(content), 0644)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
ui.output.SetText(fmt.Sprintf("File saved: %s", currentFile))
return nil
}