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

187 lines
5.4 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package scenes
import (
2022-06-05 00:38:02 +00:00
"strings"
2021-12-04 18:02:05 +00:00
"time"
2021-11-05 21:42:49 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2021-12-02 04:00:33 +00:00
"github.com/rs/zerolog/log"
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"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) {
2021-12-02 04:00:33 +00:00
newModel := NewInstallations(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: "Profiles",
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
newModel := NewProfiles(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: "Mods",
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
newModel := NewMods(root, currentModel)
return newModel, newModel.Init()
},
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: "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 {
log.Error().Err(err).Msg(ErrorFailedAddMod)
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 {
2021-12-04 18:02:05 +00:00
log.Error().Err(err).Msg(ErrorFailedAddMod)
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
2021-12-04 18:02:05 +00:00
model.list.DisableQuitKeybindings()
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 {
2021-12-02 04:00:33 +00:00
case 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
2021-12-02 04:00:33 +00:00
case 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)
totalHeight := m.root.Height() + len(m.list.Items()) + lipgloss.Height(banner) + 4
if totalHeight < m.root.Size().Height {
header = lipgloss.JoinVertical(lipgloss.Left, banner, m.root.View())
}
2022-06-05 00:38:02 +00:00
if m.error != nil {
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())
}
2022-06-05 00:38:02 +00:00
m.list.SetSize(m.list.Width(), m.root.Size().Height-lipgloss.Height(header))
return lipgloss.JoinVertical(lipgloss.Left, header, m.list.View())
2021-11-05 21:42:49 +00:00
}