-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
418 lines (363 loc) · 10.1 KB
/
Copy pathmain.go
File metadata and controls
418 lines (363 loc) · 10.1 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"golang.org/x/term"
)
type FileManager struct {
app *tview.Application
list *tview.List
fileDetails *tview.TextView
gitDetails *tview.TextView
commands *tview.TextView
commandInput *tview.InputField // Input field for command mode
path string
items []string
oldState *term.State // To store original terminal state
flex *tview.Flex
vimRunning bool
commandMode bool // Flag to track if command mode is active
}
func NewFileManager(path string) *FileManager {
fm := &FileManager{
app: tview.NewApplication(),
list: tview.NewList(),
fileDetails: tview.NewTextView().SetDynamicColors(true).SetTextAlign(tview.AlignLeft),
gitDetails: tview.NewTextView().SetDynamicColors(true).SetTextAlign(tview.AlignLeft),
commands: tview.NewTextView().SetTextAlign(tview.AlignCenter).SetDynamicColors(true),
commandInput: tview.NewInputField().SetLabel("Command: ").SetFieldWidth(0).SetFieldBackgroundColor(tcell.ColorBlack).SetLabelColor(tcell.ColorYellow),
path: path,
flex: tview.NewFlex(),
}
fm.loadItems()
return fm
}
func (fm *FileManager) loadItems() {
fm.list.Clear()
entries, err := os.ReadDir(fm.path)
if err != nil {
panic(err)
}
var dirs, files []string
for _, entry := range entries {
if entry.IsDir() {
dirs = append(dirs, entry.Name())
} else {
files = append(files, entry.Name())
}
}
sort.Strings(dirs)
sort.Strings(files)
fm.items = append(dirs, files...)
for _, item := range fm.items {
info, err := os.Stat(filepath.Join(fm.path, item))
if err != nil {
continue
}
var color tcell.Color
var symbol string
switch mode := info.Mode(); {
case mode.IsDir():
color = tcell.ColorBlue
symbol = "📁"
case (mode & os.ModeSymlink) != 0:
color = tcell.ColorFuchsia
symbol = "🔗"
case (mode & os.ModeNamedPipe) != 0:
color = tcell.ColorYellow
symbol = "📜"
case (mode & os.ModeSocket) != 0:
color = tcell.ColorGreen
symbol = "🔌"
case (mode & os.ModeDevice) != 0:
color = tcell.ColorRed
symbol = "🔧"
default:
color = tcell.ColorWhite
symbol = "📄"
}
fm.list.AddItem(fmt.Sprintf("%s %s", symbol, item), "", 0, func() {
fm.navigate(item)
}).SetMainTextColor(color)
}
// Update both file and git details simultaneously
fm.updateDetails()
fm.updateGitDetails()
}
func isGitRepo(path string) bool {
gitPath := filepath.Join(path, ".git")
stat, err := os.Stat(gitPath)
if err != nil {
return false
}
return stat.IsDir() || (stat.Mode()&os.ModeSymlink != 0)
}
func (fm *FileManager) navigate(item string) {
newPath := filepath.Join(fm.path, item)
info, err := os.Stat(newPath)
if err != nil {
return
}
if info.IsDir() {
fm.path = newPath
} else if item == ".." {
fm.path = filepath.Dir(fm.path)
} else {
fm.openInVim(newPath)
return
}
fm.loadItems()
}
func (fm *FileManager) openInVim(filepath string) {
if fm.vimRunning {
return
}
fm.vimRunning = true
oldState, err := term.GetState(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
fm.oldState = oldState
fm.app.Suspend(func() {
cmd := exec.Command("nvim", filepath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
if fm.oldState != nil {
_ = term.Restore(int(os.Stdin.Fd()), fm.oldState)
}
fm.vimRunning = false
})
fm.loadItems()
}
func (fm *FileManager) updateDetails() {
selectedIndex := fm.list.GetCurrentItem()
if selectedIndex < 0 || selectedIndex >= len(fm.items) {
fm.fileDetails.SetText("")
return
}
selectedItem := fm.items[selectedIndex]
fullPath := filepath.Join(fm.path, selectedItem)
info, err := os.Stat(fullPath)
if err != nil {
fm.fileDetails.SetText(fmt.Sprintf("Error retrieving details: %v", err))
return
}
fileType := "File"
if info.IsDir() {
fileType = "Directory"
} else if (info.Mode() & os.ModeSymlink) != 0 {
fileType = "Symlink"
} else if (info.Mode() & os.ModeNamedPipe) != 0 {
fileType = "Named Pipe"
} else if (info.Mode() & os.ModeSocket) != 0 {
fileType = "Socket"
} else if (info.Mode() & os.ModeDevice) != 0 {
fileType = "Device"
}
permissions := info.Mode().String()
owner := info.Sys().(*syscall.Stat_t).Uid
group := info.Sys().(*syscall.Stat_t).Gid
modTime := info.ModTime().Format(time.RFC1123)
size := info.Size()
details := fmt.Sprintf(
"[yellow]Name:[white] %s\n[yellow]Type:[white] %s\n[yellow]Size:[white] %d bytes\n[yellow]Permissions:[white] %s\n[yellow]Owner:[white] %d\n[yellow]Group:[white] %d\n[yellow]Modified:[white] %s\n",
info.Name(),
fileType,
size,
permissions,
owner,
group,
modTime,
)
fm.fileDetails.SetText(details)
}
func (fm *FileManager) deleteSelectedItem() {
selectedIndex := fm.list.GetCurrentItem()
if selectedIndex < 0 || selectedIndex >= len(fm.items) {
return
}
selectedItem := fm.items[selectedIndex]
fullPath := filepath.Join(fm.path, selectedItem)
err := os.RemoveAll(fullPath) // Remove files or directories
if err != nil {
fm.fileDetails.SetText(fmt.Sprintf("Error deleting file: %v", err))
return
}
fm.loadItems()
}
func (fm *FileManager) updateGitDetails() {
if !isGitRepo(fm.path) {
fm.gitDetails.SetText("")
return
}
gitCommits := fm.gitLog()
// Set Git details with commit hashes and messages
fm.gitDetails.SetText(gitCommits)
}
func (fm *FileManager) gitLog() string {
cmd := exec.Command("git", "-C", fm.path, "log", "--oneline", "-10")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("Error getting git log: %v", err)
}
// Process git log output to include commit hashes and messages
lines := strings.Split(string(output), "\n")
var result strings.Builder
for _, line := range lines {
if line != "" {
parts := strings.SplitN(line, " ", 2)
if len(parts) >= 2 {
result.WriteString(fmt.Sprintf("[yellow]%s [white]%s\n", parts[0], parts[1]))
}
}
}
return result.String()
}
func (fm *FileManager) run() {
fm.list.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
fm.navigate(mainText)
})
fm.list.SetChangedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
fm.updateDetails()
fm.updateGitDetails()
})
fm.commandInput.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEscape {
fm.commandMode = false
fm.app.SetFocus(fm.list) // Return focus to list after exiting command mode
} else if key == tcell.KeyEnter {
// Process the command input
command := fm.commandInput.GetText()
fm.processCommand(command)
fm.commandInput.SetText("") // Clear input field after processing command
}
})
fm.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if fm.commandMode {
return event
}
switch event.Key() {
case tcell.KeyBackspace, tcell.KeyLeft:
if fm.path != filepath.VolumeName(fm.path) && fm.path != "/" {
fm.path = filepath.Dir(fm.path)
fm.loadItems()
}
return nil
case tcell.KeyEnter, tcell.KeyRight:
index := fm.list.GetCurrentItem()
if index >= 0 && index < len(fm.items) {
fm.navigate(fm.items[index])
}
return nil
case tcell.KeyCtrlD:
fm.deleteSelectedItem()
return nil
case tcell.KeyRune:
switch event.Rune() {
case ':':
fm.commandMode = true
fm.app.SetFocus(fm.commandInput) // Switch focus to command input
return nil
case 'q':
fm.app.Stop()
return nil
}
}
return event
})
fm.commands.SetText("[red]Navigate: [yellow]← Back, → Enter, [green]Ctrl+D Delete, [blue]: Command, [blue]q Quit")
// Adjust layout to have the commands at the bottom center
fm.flex.SetDirection(tview.FlexRow).
AddItem(
tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(fm.list, 0, 1, true).
AddItem(tview.NewTextView().SetText("").SetTextAlign(tview.AlignLeft), 1, 1, false).
AddItem(fm.fileDetails, 0, 2, false).
AddItem(tview.NewTextView().SetText("").SetTextAlign(tview.AlignLeft), 1, 1, false).
AddItem(fm.gitDetails, 0, 2, false), 0, 1, true).
AddItem(fm.commands, 3, 1, false). // Use a height of 3 for the commands
AddItem(fm.commandInput, 1, 1, false) // Add command input field at the bottom
fm.app.SetRoot(fm.flex, true)
// Set a dark black background color
fm.app.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
screen.Clear()
bgStyle := tcell.StyleDefault.Background(tcell.ColorBlack)
screen.SetStyle(bgStyle)
return false
})
if err := fm.app.Run(); err != nil {
panic(err)
}
}
func (fm *FileManager) processCommand(command string) {
command = strings.TrimSpace(command)
if command == "" {
return
}
parts := strings.Fields(command)
cmd := parts[0]
args := parts[1:]
switch cmd {
case "mkdir":
if len(args) < 1 {
fm.printCommandOutput("Usage: mkdir <directory_name>")
return
}
dirName := args[0]
err := os.Mkdir(filepath.Join(fm.path, dirName), 0755)
if err != nil {
fm.printCommandOutput(fmt.Sprintf("Error creating directory: %v", err))
} else {
fm.printCommandOutput(fmt.Sprintf("Created directory '%s'", dirName))
fm.loadItems()
}
case "touch":
if len(args) < 1 {
fm.printCommandOutput("Usage: touch <file_name>")
return
}
fileName := args[0]
filePath := filepath.Join(fm.path, fileName)
file, err := os.Create(filePath)
if err != nil {
fm.printCommandOutput(fmt.Sprintf("Error creating file: %v", err))
} else {
file.Close()
fm.printCommandOutput(fmt.Sprintf("Created file '%s'", fileName))
fm.loadItems()
}
case "ls":
cmd := exec.Command("ls", args...)
output, err := cmd.CombinedOutput()
if err != nil {
fm.printCommandOutput(fmt.Sprintf("Error running command: %v", err))
return
}
fm.printCommandOutput(string(output))
default:
fm.printCommandOutput(fmt.Sprintf("Unknown command: %s", cmd))
}
}
func (fm *FileManager) printCommandOutput(output string) {
fm.fileDetails.SetText(output)
}
func main() {
// Check if running as root (sudo)
if os.Geteuid() != 0 {
fmt.Println("This tool requires sudo privileges. Run with 'sudo'.")
os.Exit(1)
}
fm := NewFileManager(".")
fm.run()
}