-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (66 loc) · 1.46 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
package main
import (
"github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/taz03/monkeytui/config"
"github.com/taz03/monkeytui/test"
)
type model struct {
Test *test.Model
Config *config.Model
}
var width, height int
func main() {
userConfig := config.New("config.json")
app := tea.NewProgram(model{
Test: test.New(userConfig),
Config: userConfig,
}, tea.WithAltScreen())
app.Run()
}
func (m model) calculateTestWidth() int {
if m.Config.MaxLineWidth == 0 {
return width - 10
}
if m.Config.MaxLineWidth > width {
return width
}
return m.Config.MaxLineWidth
}
func (m model) Init() tea.Cmd {
return m.Test.Init()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
width, height = msg.Width, msg.Height
case tea.KeyMsg:
if msg.String() == m.Config.RestartKey() {
m.Test = test.New(m.Config)
m.Test.Width = m.calculateTestWidth()
return m, m.Test.Init()
}
switch msg.String() {
case tea.KeyCtrlC.String():
return m, tea.Quit
}
}
_, cmd := m.Test.Update(msg)
return m, cmd
}
func (m model) View() string {
m.Test.Width = m.calculateTestWidth()
m.Test.ProgressBar.Width = width
return lipgloss.JoinVertical(
lipgloss.Left,
m.Test.ProgressBar.View(),
lipgloss.Place(
width,
height-1,
lipgloss.Center,
lipgloss.Center,
m.Test.View(),
lipgloss.WithWhitespaceBackground(m.Config.BackgroundColor()),
),
)
}