-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (166 loc) · 4.69 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
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/crbroughton/go-backstop/config"
"github.com/crbroughton/go-backstop/utils"
depchecker "github.com/crbroughton/go-backstop/views"
mainmenu "github.com/crbroughton/go-backstop/views/mainMenu"
"github.com/crbroughton/go-backstop/views/mainMenu/createTests"
"github.com/crbroughton/go-backstop/views/mainMenu/createTests/resultsTable"
"github.com/crbroughton/go-backstop/views/mainMenu/settings"
"github.com/crbroughton/go-backstop/views/mainMenu/settings/cookies"
"github.com/crbroughton/go-backstop/views/mainMenu/settings/viewport"
)
type sessionState int
const (
depChecker sessionState = iota
mainMenu
resultsTableMenu
createTestMenu
settingsMenu
cookieMenu
viewportMenu
)
type MainModel struct {
state sessionState
depChecker tea.Model
mainMenu tea.Model
resultsTableMenu tea.Model
createTestMenu tea.Model
settingsMenu tea.Model
cookieMenu tea.Model
viewportMenu tea.Model
windowSize tea.WindowSizeMsg
}
func main() {
model := New()
program := tea.NewProgram(model, tea.WithAltScreen())
_, err := program.Run()
if utils.IsError(err) {
fmt.Println(err)
os.Exit(1)
}
}
func New() MainModel {
var startingState sessionState
if !config.GetDependencyCheck() {
startingState = depChecker
} else {
startingState = mainMenu
}
return MainModel{
state: startingState,
mainMenu: mainmenu.New(),
depChecker: depchecker.New(),
}
}
func (m MainModel) Init() tea.Cmd {
config.CreateJSON()
_, err := os.Stat("backstop_data")
if errors.Is(err, os.ErrNotExist) {
config.RunBackstopCommand("init", false)
}
_, err = os.Stat("backstop.json")
if err == nil {
err = os.Remove("backstop.json")
if utils.IsError(err) {
log.Fatal(err)
}
}
return tea.Batch(m.depChecker.Init())
}
func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.windowSize = msg // pass this along to the entry view so it uses the full window size when it's initialized
case depchecker.DependenciesInstalled:
m.state = mainMenu
m.mainMenu = mainmenu.New()
m.mainMenu, cmd = m.mainMenu.Update(m.windowSize)
case mainmenu.RunTestsSelected:
m.state = resultsTableMenu
m.resultsTableMenu = resultsTable.New()
case mainmenu.CreatedNewTestSelected:
m.state = createTestMenu
m.createTestMenu = createTests.New()
case createTests.GoBackToMainMenu:
m.state = mainMenu
case resultsTable.GoBackToSettingsMenu:
m.state = mainMenu
m.resultsTableMenu, cmd = m.resultsTableMenu.Update(m.windowSize)
case mainmenu.SettingsSelected:
m.state = settingsMenu
m.settingsMenu = settings.New()
m.settingsMenu, cmd = m.settingsMenu.Update(m.windowSize)
case settings.GoBackToMainMenu:
m.state = mainMenu
m.mainMenu, cmd = m.mainMenu.Update(m.windowSize)
case settings.GoToViewPort:
m.state = viewportMenu
m.viewportMenu = viewport.New()
m.viewportMenu, cmd = m.viewportMenu.Update(m.windowSize)
case settings.GoToCookie:
m.state = cookieMenu
m.cookieMenu = cookies.New()
m.cookieMenu, cmd = m.cookieMenu.Update(m.windowSize)
case viewport.GoBackToSettingsMenu:
m.state = settingsMenu
m.settingsMenu, cmd = m.settingsMenu.Update(m.windowSize)
case cookies.GoBackToSettingsMenu:
m.state = settingsMenu
m.settingsMenu, cmd = m.settingsMenu.Update(m.windowSize)
case spinner.TickMsg:
m.depChecker, cmd = m.depChecker.Update(msg)
cmds = append(cmds, cmd)
}
switch m.state {
case depChecker:
m.depChecker, cmd = m.depChecker.Update(msg)
cmds = append(cmds, cmd)
case mainMenu:
m.mainMenu, cmd = m.mainMenu.Update(msg)
cmds = append(cmds, cmd)
case resultsTableMenu:
m.resultsTableMenu, cmd = m.resultsTableMenu.Update(msg)
cmds = append(cmds, cmd)
case createTestMenu:
m.createTestMenu, cmd = m.createTestMenu.Update(msg)
cmds = append(cmds, cmd)
case settingsMenu:
m.settingsMenu, cmd = m.settingsMenu.Update(msg)
cmds = append(cmds, cmd)
case cookieMenu:
m.cookieMenu, cmd = m.cookieMenu.Update(msg)
cmds = append(cmds, cmd)
case viewportMenu:
m.viewportMenu, cmd = m.viewportMenu.Update(msg)
cmds = append(cmds, cmd)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m MainModel) View() string {
switch m.state {
case mainMenu:
return m.mainMenu.View()
case resultsTableMenu:
return m.resultsTableMenu.View()
case createTestMenu:
return m.createTestMenu.View()
case settingsMenu:
return m.settingsMenu.View()
case cookieMenu:
return m.cookieMenu.View()
case viewportMenu:
return m.viewportMenu.View()
default:
return m.depChecker.View()
}
}