-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (56 loc) · 1.57 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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"golang.org/x/term"
)
func main() {
// TODO: Use CLI args for root and fullscreen.
if _, e := runProgram(".", false); e != nil {
fmt.Println("ERROR RUNNING PROGRAM ", e)
os.Exit(1)
}
}
func runProgram(root string, fullscreen bool) (tea.Model, error) {
state := model{root: root, fullscreen: fullscreen}
if absolutePath, e := filepath.Abs(state.root); e != nil {
return nil, e
} else {
state.list = addFileItems(absolutePath, state)
var options []tea.ProgramOption
if state.fullscreen {
options = append(options, tea.WithAltScreen())
}
return tea.NewProgram(state, options...).Run()
}
}
func addFileItems(absolutePath string, state model) list.Model {
delegate := itemDelegate{}
width, height := style.GetMaxWidth(), 10
if newWidth, newHeight, e := term.GetSize(0); e != nil || state.fullscreen {
width, height = newWidth, newHeight
}
files := list.New([]list.Item{}, delegate, width, height)
files.Title = absolutePath
if directory, e := os.Open(absolutePath); e != nil {
fmt.Println("ERROR RUNNING PROGRAM ", e)
os.Exit(1)
} else {
if names, e := directory.Readdirnames(0); e != nil {
fmt.Println("ERROR RUNNING PROGRAM ", e)
os.Exit(1)
} else {
for index, name := range names {
filetype := false
if info, e := os.Stat(fmt.Sprintf("%s/%s", absolutePath, name)); e == nil {
filetype = info.IsDir()
}
files.InsertItem(index, item{title: name, filetype: filetype})
}
}
}
return files
}