ficsit-cli-flake/tea/scenes/main_menu.go

234 lines
7 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package scenes
import (
"log/slog"
2022-06-05 00:38:02 +00:00
"strings"
2021-12-04 18:02:05 +00:00
"time"
"github.com/charmbracelet/bubbles/key"
2021-11-05 21:42:49 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2022-10-14 16:11:16 +00:00
"github.com/spf13/viper"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/errors"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/installation"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/keys"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/mods"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/profile"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
2021-11-05 21:42:49 +00:00
)
2021-12-02 04:00:33 +00:00
var _ tea.Model = (*mainMenu)(nil)
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
type mainMenu struct {
2022-06-05 00:38:02 +00:00
root components.RootModel
list list.Model
error *components.ErrorComponent
banner string
2021-11-05 21:42:49 +00:00
}
2022-06-05 00:38:02 +00:00
const logoBanner = `
`
2021-12-02 04:00:33 +00:00
func NewMainMenu(root components.RootModel) tea.Model {
2022-06-05 00:38:02 +00:00
trimmedBanner := strings.TrimSpace(logoBanner)
var finalBanner strings.Builder
for i, s := range strings.Split(trimmedBanner, "\n") {
if i > 0 {
finalBanner.WriteRune('\n')
}
foreground := utils.LogoForegroundStyles[i]
background := utils.LogoBackgroundStyles[i]
for _, c := range s {
if c == '█' {
finalBanner.WriteString(foreground.Render("█"))
} else if c != ' ' {
finalBanner.WriteString(background.Render(string(c)))
} else {
finalBanner.WriteRune(c)
}
}
}
2021-12-02 04:00:33 +00:00
model := mainMenu{
2022-06-05 00:38:02 +00:00
root: root,
banner: finalBanner.String(),
2021-11-05 21:42:49 +00:00
}
items := []list.Item{
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Installations",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := installation.NewInstallations(root, currentModel)
2021-12-02 04:00:33 +00:00
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Toggle Vanilla",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if currentModel.root.GetCurrentInstallation() != nil {
currentModel.root.GetCurrentInstallation().Vanilla = !currentModel.root.GetCurrentInstallation().Vanilla
}
return currentModel, nil
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Profiles",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := profile.NewProfiles(root, currentModel)
2021-12-02 04:00:33 +00:00
return newModel, newModel.Init()
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2022-06-06 23:55:26 +00:00
ItemTitle: "All Mods",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewMods(root, currentModel)
2021-12-02 04:00:33 +00:00
return newModel, newModel.Init()
},
2021-11-05 21:42:49 +00:00
},
2022-06-06 23:55:26 +00:00
utils.SimpleItem[mainMenu]{
ItemTitle: "Installed Mods",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewInstalledMods(root, currentModel)
2022-06-06 23:55:26 +00:00
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Update Mods",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewUpdateMods(root, currentModel)
return newModel, newModel.Init()
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Apply Changes",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if err := root.GetGlobal().Save(); err != nil {
slog.Error(errors.ErrorFailedAddMod, slog.Any("err", err))
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
}
newModel := NewApply(root, currentModel)
return newModel, newModel.Init()
2021-12-02 04:00:33 +00:00
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Save",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
if err := root.GetGlobal().Save(); err != nil {
slog.Error(errors.ErrorFailedAddMod, slog.Any("err", err))
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
2021-12-04 18:02:05 +00:00
return currentModel, cmd
2021-12-02 04:00:33 +00:00
}
return nil, nil
},
2021-11-05 21:42:49 +00:00
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Exit",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
return nil, tea.Quit
2021-12-02 04:00:33 +00:00
},
2021-11-05 21:42:49 +00:00
},
}
2022-04-14 01:27:39 +00:00
model.list = list.New(items, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
2021-12-02 04:00:33 +00:00
model.list.SetShowStatusBar(false)
model.list.SetFilteringEnabled(false)
model.list.Title = "Main Menu"
model.list.Styles = utils.ListStyles
model.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
}
}
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
return model
2021-11-05 21:42:49 +00:00
}
func (m mainMenu) Init() tea.Cmd {
return nil
}
func (m mainMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case keys.KeyControlC:
2021-11-05 21:42:49 +00:00
return m, tea.Quit
2021-12-02 04:00:33 +00:00
case "q":
2022-04-14 01:27:39 +00:00
return m, tea.Quit
case keys.KeyEnter:
2022-04-14 01:27:39 +00:00
i, ok := m.list.SelectedItem().(utils.SimpleItem[mainMenu])
2021-11-05 21:42:49 +00:00
if ok {
2021-12-02 04:00:33 +00:00
if i.Activate != nil {
newModel, cmd := i.Activate(msg, m)
if newModel != nil || cmd != nil {
if newModel == nil {
newModel = m
}
return newModel, cmd
}
2021-11-05 21:42:49 +00:00
return m, nil
}
}
2021-12-04 18:02:05 +00:00
return m, nil
2021-11-05 21:42:49 +00:00
default:
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
2021-11-05 22:01:35 +00:00
top, right, bottom, left := lipgloss.NewStyle().Margin(2, 2).GetMargin()
2021-11-05 21:42:49 +00:00
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
2021-12-02 04:00:33 +00:00
m.root.SetSize(msg)
case components.ErrorComponentTimeoutMsg:
m.error = nil
2021-11-05 21:42:49 +00:00
}
return m, nil
}
func (m mainMenu) View() string {
2022-06-05 01:56:46 +00:00
header := m.root.View()
2022-06-05 00:38:02 +00:00
2022-06-05 01:56:46 +00:00
banner := lipgloss.NewStyle().Margin(2, 0, 0, 2).Render(m.banner)
2022-06-06 23:55:26 +00:00
commit := viper.GetString("commit")
if len(commit) > 8 {
commit = commit[:8]
}
version := "\n"
version += utils.LabelStyle.Render("Version: ")
version += viper.GetString("version") + " - " + commit
header = lipgloss.JoinVertical(lipgloss.Left, version, header)
totalHeight := lipgloss.Height(header) + len(m.list.Items()) + lipgloss.Height(banner) + 5
2022-06-05 01:56:46 +00:00
if totalHeight < m.root.Size().Height {
2022-06-06 23:55:26 +00:00
header = lipgloss.JoinVertical(lipgloss.Left, banner, header)
2022-06-05 01:56:46 +00:00
}
2022-06-05 00:38:02 +00:00
if m.error != nil {
2022-10-14 16:11:16 +00:00
err := m.error.View()
2022-06-05 00:38:02 +00:00
m.list.SetSize(m.list.Width(), m.root.Size().Height-lipgloss.Height(header)-lipgloss.Height(err))
return lipgloss.JoinVertical(lipgloss.Left, header, err, m.list.View())
}
m.list.SetSize(m.list.Width(), m.root.Size().Height-lipgloss.Height(header))
2022-06-05 00:38:02 +00:00
return lipgloss.JoinVertical(lipgloss.Left, header, m.list.View())
2021-11-05 21:42:49 +00:00
}