-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
357 lines (294 loc) · 7.59 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 (
"context"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/Azanul/peer-pressure/pkg/peer"
"github.com/Azanul/peer-pressure/tui/style"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
TabToI = map[string]int{
"Main Menu": 0,
"Create new node": 1,
}
TabChoices = [][]string{
{},
{"Send", "Receive"},
}
nodeCreate = createFormModel{
inputs: []textinput.Model{
textinput.New(),
textinput.New(),
},
}
crrNode = oldNodeMenuModel{
name: "test",
choices: []string{"Send", "Receive"},
filepicker: filepicker.New(),
transfer: peer.Transfer{
Progress: progress.New(progress.WithDefaultGradient()),
EventCh: make(chan peer.Event),
CommandCh: make(chan peer.Command),
TempPerc: 0,
},
}
)
type sessionState uint
const (
mainMenu sessionState = iota
newNodeForm
oldNodeMenu
sendFileExplorer
sendLoader
receiveLoader
)
type model struct {
state sessionState // state to track which model is focussed
Tabs []string
choices []string // options [create new node, list of nodes...]
cursor int // which option our cursor is pointing at
}
func (m model) Init() tea.Cmd {
return crrNode.filepicker.Init()
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
crrNode.filepicker, cmd = crrNode.filepicker.Update(msg)
cmds = append(cmds, cmd)
case progress.FrameMsg:
progressModel, cmd := crrNode.transfer.Progress.Update(msg)
crrNode.transfer.Progress = progressModel.(progress.Model)
cmds = append(cmds, cmd)
}
switch m.state {
case newNodeForm:
return nodeCreate.Update(m, msg)
case oldNodeMenu:
return crrNode.Update(m, msg)
case sendFileExplorer:
crrNode.filepicker, cmd = crrNode.filepicker.Update(msg)
// Did the user select a file?
if didSelect, path := crrNode.filepicker.DidSelectFile(msg); didSelect {
go func() {
go func() {
for c := range crrNode.transfer.EventCh {
data := c.Data.(float64)
if data < 0 {
return
} else {
crrNode.transfer.TempPerc = min(data, 1)
}
}
}()
err := sendFile(context.TODO(), crrNode.name, path, crrNode.transfer.EventCh, crrNode.transfer.CommandCh)
if err != nil {
fmt.Println(style.ErrorTextStyle(err.Error()))
cmd = tea.Quit
}
}()
m.state++
}
return m, cmd
case sendLoader:
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q", "esc":
crrNode.transfer.Stop()
return m, tea.Quit
// These keys should pause/continue the transfer.
case "space":
crrNode.transfer.Toggle()
}
}
if crrNode.transfer.Progress.Percent() == 1 {
m.state = 0
crrNode.transfer.TempPerc = 0
crrNode.transfer.Progress.SetPercent(0)
m.Tabs = m.Tabs[:1]
} else {
cmds = append(cmds, crrNode.transfer.Progress.SetPercent(crrNode.transfer.TempPerc))
}
case receiveLoader:
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q", "esc":
crrNode.transfer.Stop()
return m, tea.Quit
// These keys should pause/continue the transfer.
case "space":
crrNode.transfer.Toggle()
}
}
if crrNode.transfer.Progress.Percent() == 1 {
m.state = 0
crrNode.transfer.TempPerc = 0
crrNode.transfer.Progress.SetPercent(0)
m.Tabs = m.Tabs[:1]
} else {
cmds = append(cmds, crrNode.transfer.Progress.SetPercent(crrNode.transfer.TempPerc))
}
default:
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q", "esc":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "left", "backspace":
if len(m.Tabs) > 1 {
m.choices = TabChoices[TabToI[m.Tabs[len(m.Tabs)-1]]]
m.Tabs = m.Tabs[:len(m.Tabs)-1]
}
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
choice := m.choices[m.cursor]
m.Tabs = append(m.Tabs, style.TabStyles[len(m.Tabs)](choice))
if choice == "Create new node" {
m.state++
nodeCreate.inputs[0].Focus()
} else {
m.state += 2
crrNode.name = choice
}
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, tea.Batch(cmds...)
}
func (m model) View() string {
// Tabs
s := lipgloss.JoinHorizontal(lipgloss.Top, m.Tabs...)
switch m.state {
case mainMenu:
// The header
header := style.HeaderStyle("Create new node or select an already existing one\n")
mainChoices := ""
// Iterate over our choices
for i, choice := range m.choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Render the row
mainChoices += fmt.Sprintf("%s %s\n", cursor, choice)
}
// Tab header
s = lipgloss.JoinVertical(lipgloss.Left, s, header, mainChoices)
// The footer
footer := ""
if len(m.Tabs) > 1 {
footer = "\nPress ◀ / Backspace to go back"
}
footer += "\nPress q to quit.\n"
s += style.FooterStyle(footer)
case newNodeForm:
s += nodeCreate.View()
case oldNodeMenu:
s += crrNode.View()
case sendFileExplorer:
s += "\n\n" + crrNode.filepicker.View()
case sendLoader:
s += "\n\n" + crrNode.transfer.Progress.View()
footer := ""
if crrNode.transfer.Paused() {
footer = "\n\t\tPAUSED\n\n"
footer += "Press space to continue"
} else {
footer += "Press space to pause"
}
s += style.FooterStyle(footer)
case receiveLoader:
s += "\n\n" + crrNode.transfer.Progress.View()
footer := ""
if crrNode.transfer.Paused() {
footer = "\n\t\tPAUSED\n\n"
footer += "Press space to continue"
} else {
footer += "Press space to pause"
}
s += style.FooterStyle(footer)
}
// Send the UI for rendering
return s
}
func initialModel() model {
choices := []string{"Create new node"}
crrNode.filepicker.CurrentDirectory, _ = os.UserHomeDir()
err := os.MkdirAll("./nodes", os.ModePerm)
if err != nil {
panic(err)
}
f, err := os.Open("./nodes")
if err != nil {
log.Panicln(err)
}
files, err := f.Readdir(0)
if err != nil {
log.Panicln(err)
}
for _, v := range files {
if v.IsDir() {
choices = append(choices, v.Name())
}
}
TabChoices[0] = choices
mainModel := model{
choices: choices,
Tabs: []string{style.TabStyles[0]("Main Menu")},
}
return mainModel
}
func main() {
f, err := os.OpenFile("log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.SetLevel(log.DebugLevel)
// starting our program
m := initialModel()
if _, err := tea.NewProgram(&m).Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}
func min[T float64 | float32 | int | int64 | int32 | int16 | int8](a, b T) T {
if a < b {
return a
}
return b
}