aboutsummaryrefslogtreecommitdiff
path: root/screens/screens.go
blob: 3456693b36663afeaf8d7acc85d7a537c8763f32 (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
package screens

import (
	"nectar/build"
	"nectar/types"
	"os"

	tea "github.com/charmbracelet/bubbletea"
	"golang.org/x/term"
)

type SwitchMsg struct {
	Screen tea.Model
}

type ScreenManager struct {
	currentScreen tea.Model
}

var globals types.Globals

func (sm *ScreenManager) Init() tea.Cmd {
	return sm.currentScreen.Init()
}

func (sm *ScreenManager) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch m := msg.(type) {
	case tea.WindowSizeMsg:
		globals.Width, globals.Height = m.Width, m.Height
		return sm, nil
	case SwitchMsg:
		sm.currentScreen = m.Screen
		return sm, sm.currentScreen.Init()
	}

	updatedScreen, cmd := sm.currentScreen.Update(msg)
	sm.currentScreen = updatedScreen
	return sm, cmd
}

func (sm *ScreenManager) View() string {
	return sm.currentScreen.View()
}

func switchScreen(screen tea.Model) tea.Cmd {
	return func() tea.Msg {
		return SwitchMsg{Screen: screen}
	}
}

func Start() tea.Model {
	width, height, err := term.GetSize(int(os.Stdout.Fd()))
	if err != nil {
		width = 80
		height = 24
	}

	globals = types.Globals{
		Width:     width,
		Height:    height,
		BuildDate: build.Date,
		Version:   build.Version,
	}

	return &ScreenManager{currentScreen: _root()}
}