-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (178 loc) · 4.48 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/frogfreg/godu/fileinfo"
)
type model struct {
currentDir string
fileMap map[string]fileinfo.FileInfo
loading bool
deleting bool
deleteDir string
selectedIndex int
err error
table table.Model
}
type fileInfoResponse struct {
data []fileinfo.FileInfo
fileMap map[string]fileinfo.FileInfo
err error
}
type deleteResponse struct {
err error
}
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
func (m model) Init() tea.Cmd {
return getFileInfoCmd(nil, m.currentDir)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case fileInfoResponse:
m.loading = false
fir := fileInfoResponse(msg)
if fir.err != nil {
m.err = fir.err
log.Fatalf("something went wrong: %v", m.err)
}
m.fileMap = fir.fileMap
m.table.SetRows(fileinfo.FileInfosToRow(fir.data))
case deleteResponse:
m.deleting = false
m.err = deleteResponse(msg).err
if m.err != nil {
log.Fatalf("something went wrong: %v", m.err)
}
m.loading = true
return m, getFileInfoCmd(m.fileMap, m.currentDir)
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
return m, tea.Quit
case "enter", "right", "l":
sr := m.table.SelectedRow()
if !m.loading && !m.deleting && len(sr) != 0 && sr[1] == "dir" {
m = m.updateCurrentDir(m.table.SelectedRow()[0], false)
return m, getFileInfoCmd(m.fileMap, m.currentDir)
}
case "left", "h", "backspace":
if !m.loading && !m.deleting {
m = m.updateCurrentDir(filepath.Dir(m.currentDir), true)
return m, getFileInfoCmd(m.fileMap, m.currentDir)
}
case "d":
sr := m.table.SelectedRow()
if !m.loading && !m.deleting && len(sr) != 0 {
m.deleting = true
m.deleteDir = filepath.Join(m.currentDir, sr[0])
return m, deleteCmd(m.fileMap, m.deleteDir)
}
}
}
m.table, _ = m.table.Update(msg)
return m, nil
}
func (m model) View() string {
if m.err != nil {
return m.err.Error()
}
if m.deleting {
return fmt.Sprintf("deleting %v...", m.deleteDir)
}
if m.loading {
return "Reading files..."
}
viewString := "Controls:\n"
viewString += "d: delete dir/file\nh,left arrow: go up a directory\nl, right arrow, enter: go down selected dir\n"
viewString += "j,down arrow: move down\nk,up arrow: move up\n"
viewString += fmt.Sprintf("\nCurrent directory: %q\n", m.currentDir)
viewString += baseStyle.Render(m.table.View()) + "\n"
return viewString
}
func (m model) updateCurrentDir(dir string, replace bool) model {
m.currentDir = filepath.Join(m.currentDir, dir)
if replace {
m.currentDir = dir
}
m.loading = true
return m
}
func deleteCmd(m map[string]fileinfo.FileInfo, path string) tea.Cmd {
f := func() tea.Msg {
var res deleteResponse
err := os.RemoveAll(path)
if err != nil {
res.err = err
}
fileinfo.CleanChildren(m, path)
return res
}
return f
}
func getFileInfoCmd(m map[string]fileinfo.FileInfo, dir string) tea.Cmd {
return func() tea.Msg {
var res fileInfoResponse
fm, err := fileinfo.GenerateFileMap(m, dir)
if err != nil {
res.err = err
return res
}
data := fileinfo.GetSortedDirs(fm, dir)
res.fileMap = fm
res.data = data
return res
}
}
func getInitialTable() table.Model {
t := table.New(table.WithColumns(
[]table.Column{
{Title: "Name", Width: 30},
{Title: "Type", Width: 7},
{Title: "Size", Width: 15},
}),
table.WithRows(
[]table.Row{}),
table.WithFocused(true),
table.WithHeight(10))
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
return t
}
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("something went wrong: %v", err)
}
m := model{
currentDir: cwd,
loading: true,
selectedIndex: 0,
table: getInitialTable(),
}
// f, err := tea.LogToFile("/home/nyan/Desktop/experiments/godu/debug.log", "debug")
// if err != nil {
// fmt.Println("fatal:", err)
// os.Exit(1)
// }
// defer f.Close()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
log.Fatalf("something went wrong %v\n", err)
}
}