ficsit-cli-flake/tea/root.go

93 lines
2.5 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/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
global *cli.GlobalContext
currentSize tea.WindowSizeMsg
headerComponent tea.Model
dependencyResolver cli.DependencyResolver
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,
},
2022-04-14 01:27:39 +00:00
dependencyResolver: cli.NewDependencyResolver(global.APIClient),
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 err := m.GetCurrentInstallation().SetProfile(m.global, profile.Name); err != nil {
return errors.Wrap(err, "failed setting profile on installation")
}
2021-12-02 04:00:33 +00:00
return m.global.Save()
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
2021-12-02 04:00:33 +00:00
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 {
2022-04-14 01:27:39 +00:00
return m.global.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
}
2022-04-14 01:27:39 +00:00
func (m *rootModel) ResolveModDependencies(constraints map[string]string) (map[string]cli.ModVersion, error) {
return m.dependencyResolver.ResolveModDependencies(constraints)
}
2021-12-02 04:00:33 +00:00
func RunTea(global *cli.GlobalContext) error {
2021-12-04 18:02:05 +00:00
if err := tea.NewProgram(scenes.NewMainMenu(newModel(global)), tea.WithAltScreen(), tea.WithMouseCellMotion()).Start(); err != nil {
2021-12-02 04:00:33 +00:00
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
}