-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (46 loc) · 1.15 KB
/
main.go
File metadata and controls
57 lines (46 loc) · 1.15 KB
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
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/chris-bateman/OpsTerm/ui"
)
type appModel struct {
current tea.Model
windowSize *tea.WindowSizeMsg // cache the last known size
switchingTo tea.Model // temporary target for model switch
}
func (m appModel) Init() tea.Cmd {
return m.current.Init()
}
func (m appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.windowSize = &msg // Cache it
updated, cmd := m.current.Update(msg)
m.current = updated
return m, cmd
case ui.SwitchToMainMenuMsg:
newModel := ui.NewMainMenu()
if m.windowSize != nil {
// Send cached size to new model
newModel, _ = newModel.Update(*m.windowSize)
}
m.current = newModel
return m, nil
}
updated, cmd := m.current.Update(msg)
m.current = updated
return m, cmd
}
func (m appModel) View() string {
return m.current.View()
}
func main() {
root := appModel{current: ui.NewAuthSelector()}
p := tea.NewProgram(root, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error running OpsTerm: %v\n", err)
os.Exit(1)
}
}