blob: 1675ec7f909bd2500d26279589da86835a954a33 (
plain)
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
|
package screens
import (
"os"
tea "github.com/charmbracelet/bubbletea"
"golang.org/x/term"
)
type ScreenSwitcher struct {
currentScreen tea.Model
}
type Globals struct {
width int
height int
}
var globals Globals
func (s ScreenSwitcher) Init() tea.Cmd {
return s.currentScreen.Init()
}
func (s ScreenSwitcher) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var model tea.Model
switch m := msg.(type) {
case tea.WindowSizeMsg:
globals.width, globals.height = m.Width, m.Height
}
model, cmd = s.currentScreen.Update(msg)
return ScreenSwitcher{currentScreen: model}, cmd
}
func (s ScreenSwitcher) View() string {
return s.currentScreen.View()
}
func (s ScreenSwitcher) Switch(screen tea.Model) (tea.Model, tea.Cmd) {
s.currentScreen = screen
return s.currentScreen, s.currentScreen.Init()
}
func screen() ScreenSwitcher {
screen := homeScreen()
return ScreenSwitcher{
currentScreen: screen,
}
}
func Initialize() tea.Model {
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
width = 80
height = 30
}
globals.width = width
globals.height = height
return screen()
}
|