ficsit-cli-flake/tea/root.go

90 lines
2.3 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package tea
import (
2021-12-02 04:00:33 +00:00
"github.com/Khan/genqlient/graphql"
2021-11-05 21:42:49 +00:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2021-12-02 04:00:33 +00:00
"github.com/pkg/errors"
2021-11-05 21:42:49 +00:00
"github.com/satisfactorymodding/ficsit-cli/cli"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/ficsit"
"github.com/satisfactorymodding/ficsit-cli/tea/components"
2021-11-05 21:42:49 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/scenes"
)
type rootModel struct {
currentProfile *cli.Profile
currentInstallation *cli.Installation
2021-12-02 04:00:33 +00:00
global *cli.GlobalContext
apiClient graphql.Client
currentSize tea.WindowSizeMsg
headerComponent tea.Model
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func newModel(global *cli.GlobalContext) *rootModel {
m := &rootModel{
global: global,
currentProfile: global.Profiles.GetProfile(global.Profiles.SelectedProfile),
currentInstallation: global.Installations.GetInstallation(global.Installations.SelectedInstallation),
apiClient: ficsit.InitAPI(),
currentSize: tea.WindowSizeMsg{
Width: 20,
Height: 14,
},
}
m.headerComponent = components.NewHeaderComponent(m)
return m
2021-11-05 21:42:49 +00:00
}
func (m *rootModel) GetCurrentProfile() *cli.Profile {
return m.currentProfile
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) SetCurrentProfile(profile *cli.Profile) error {
2021-11-05 21:42:49 +00:00
m.currentProfile = profile
2021-12-02 04:00:33 +00:00
m.global.Profiles.SelectedProfile = profile.Name
return m.global.Save()
2021-11-05 21:42:49 +00:00
}
func (m *rootModel) GetCurrentInstallation() *cli.Installation {
return m.currentInstallation
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) SetCurrentInstallation(installation *cli.Installation) error {
2021-11-05 21:42:49 +00:00
m.currentInstallation = installation
2021-12-02 04:00:33 +00:00
m.global.Installations.SelectedInstallation = installation.Path
return m.global.Save()
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) GetAPIClient() graphql.Client {
return m.apiClient
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) Size() tea.WindowSizeMsg {
return m.currentSize
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) SetSize(size tea.WindowSizeMsg) {
m.currentSize = size
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) View() string {
return m.headerComponent.View()
}
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
func (m *rootModel) Height() int {
return lipgloss.Height(m.View()) + 1
}
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
func (m *rootModel) GetGlobal() *cli.GlobalContext {
return m.global
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func RunTea(global *cli.GlobalContext) error {
if err := tea.NewProgram(scenes.NewMainMenu(newModel(global))).Start(); err != nil {
return errors.Wrap(err, "internal tea error")
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
return nil
2021-11-05 21:42:49 +00:00
}