ficsit-cli-flake/tea/root.go

103 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-11-05 21:42:49 +00:00
package tea
import (
"fmt"
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"
resolver "github.com/satisfactorymodding/ficsit-resolver"
2022-10-14 16:11:16 +00:00
2021-11-05 21:42:49 +00:00
"github.com/satisfactorymodding/ficsit-cli/cli"
"github.com/satisfactorymodding/ficsit-cli/cli/provider"
2021-12-02 04:00:33 +00:00
"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 {
2022-04-14 01:27:39 +00:00
headerComponent tea.Model
2022-10-14 16:11:16 +00:00
global *cli.GlobalContext
dependencyResolver resolver.DependencyResolver
2022-10-14 16:11:16 +00:00
currentSize tea.WindowSizeMsg
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func newModel(global *cli.GlobalContext) *rootModel {
m := &rootModel{
2022-04-14 01:27:39 +00:00
global: global,
2021-12-02 04:00:33 +00:00
currentSize: tea.WindowSizeMsg{
Width: 20,
Height: 14,
},
dependencyResolver: resolver.NewDependencyResolver(global.Provider),
2021-12-02 04:00:33 +00:00
}
m.headerComponent = components.NewHeaderComponent(m)
return m
2021-11-05 21:42:49 +00:00
}
func (m *rootModel) GetCurrentProfile() *cli.Profile {
2021-12-04 18:02:05 +00:00
return m.global.Profiles.GetProfile(m.global.Profiles.SelectedProfile)
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) SetCurrentProfile(profile *cli.Profile) error {
m.global.Profiles.SelectedProfile = profile.Name
2022-04-14 01:27:39 +00:00
if m.GetCurrentInstallation() != nil {
if err := m.GetCurrentInstallation().SetProfile(m.global, profile.Name); err != nil {
return fmt.Errorf("failed setting profile on installation: %w", err)
}
2022-04-14 01:27:39 +00:00
}
2022-06-06 23:55:26 +00:00
return nil
2021-11-05 21:42:49 +00:00
}
func (m *rootModel) GetCurrentInstallation() *cli.Installation {
2021-12-04 18:02:05 +00:00
return m.global.Installations.GetInstallation(m.global.Installations.SelectedInstallation)
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) SetCurrentInstallation(installation *cli.Installation) error {
m.global.Installations.SelectedInstallation = installation.Path
2022-04-14 01:27:39 +00:00
m.global.Profiles.SelectedProfile = installation.Profile
2022-06-06 23:55:26 +00:00
return nil
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func (m *rootModel) GetAPIClient() graphql.Client {
2022-04-14 01:27:39 +00:00
return m.global.APIClient
2021-11-05 21:42:49 +00:00
}
func (m *rootModel) GetProvider() provider.Provider {
return m.global.Provider
}
func (m *rootModel) GetResolver() resolver.DependencyResolver {
return m.dependencyResolver
}
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)), tea.WithAltScreen(), tea.WithMouseCellMotion()).Run(); err != nil {
return fmt.Errorf("internal tea error: %w", err)
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
}