ficsit-cli-flake/tea/scenes/mods/mod.go

165 lines
4.6 KiB
Go
Raw Normal View History

package mods
2021-12-02 04:00:33 +00:00
import (
2021-12-04 18:02:05 +00:00
"time"
"github.com/charmbracelet/bubbles/key"
2021-12-02 04:00:33 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rs/zerolog/log"
2022-10-14 16:11:16 +00:00
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/keys"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
)
var _ tea.Model = (*modMenu)(nil)
type modMenu struct {
root components.RootModel
list list.Model
parent tea.Model
}
func NewModMenu(root components.RootModel, parent tea.Model, mod utils.Mod) tea.Model {
model := modMenu{
root: root,
parent: parent,
}
var items []list.Item
if root.GetCurrentProfile().HasMod(mod.Reference) {
items = []list.Item{
2022-04-14 01:27:39 +00:00
utils.SimpleItem[modMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Remove Mod",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
root.GetCurrentProfile().RemoveMod(mod.Reference)
return currentModel.parent, currentModel.parent.Init()
2021-12-02 04:00:33 +00:00
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[modMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Change Version",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
newModel := NewModVersion(root, currentModel.parent, mod)
2021-12-02 04:00:33 +00:00
return newModel, newModel.Init()
},
},
}
if root.GetCurrentProfile().IsModEnabled(mod.Reference) {
items = append(items, utils.SimpleItem[modMenu]{
ItemTitle: "Disable Mod",
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
root.GetCurrentProfile().SetModEnabled(mod.Reference, false)
return currentModel.parent, currentModel.parent.Init()
},
})
} else {
items = append(items, utils.SimpleItem[modMenu]{
ItemTitle: "Enable Mod",
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
root.GetCurrentProfile().SetModEnabled(mod.Reference, true)
return currentModel.parent, currentModel.parent.Init()
},
})
}
2021-12-02 04:00:33 +00:00
} else {
items = []list.Item{
2022-04-14 01:27:39 +00:00
utils.SimpleItem[modMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Install Mod",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
err := root.GetCurrentProfile().AddMod(mod.Reference, ">=0.0.0")
if err != nil {
log.Error().Err(err).Msg(errors.ErrorFailedAddMod)
cmd := currentModel.list.NewStatusMessage(errors.ErrorFailedAddMod)
2021-12-04 18:02:05 +00:00
return currentModel, cmd
2021-12-02 04:00:33 +00:00
}
2022-04-14 01:27:39 +00:00
return currentModel.parent, nil
2021-12-02 04:00:33 +00:00
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[modMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Install Mod with specific version",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
newModel := NewModVersion(root, currentModel.parent, mod)
2021-12-02 04:00:33 +00:00
return newModel, newModel.Init()
},
},
}
}
2022-04-14 01:27:39 +00:00
items = append(items, utils.SimpleItem[modMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "View Mod info",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
newModel := NewModInfo(root, currentModel, mod)
return newModel, newModel.Init()
},
})
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 = mod.Name
model.list.Styles = utils.ListStyles
model.list.SetSize(model.list.Width(), model.list.Height())
2021-12-04 18:02:05 +00:00
model.list.StatusMessageLifetime = time.Second * 3
2023-01-15 23:57:11 +00:00
model.list.KeyMap.Quit.SetHelp("q", "back")
model.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
}
}
2021-12-02 04:00:33 +00:00
return model
}
func (m modMenu) Init() tea.Cmd {
return nil
}
func (m modMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case keys.KeyControlC:
2021-12-02 04:00:33 +00:00
return m, tea.Quit
case "q":
if m.parent != nil {
m.parent.Update(m.root.Size())
return m.parent, nil
}
return m, tea.Quit
case keys.KeyEnter:
2022-04-14 01:27:39 +00:00
i, ok := m.list.SelectedItem().(utils.SimpleItem[modMenu])
2021-12-02 04:00:33 +00:00
if ok {
if i.Activate != nil {
newModel, cmd := i.Activate(msg, m)
if newModel != nil || cmd != nil {
if newModel == nil {
newModel = m
}
return newModel, cmd
}
return m, nil
}
}
2021-12-04 18:02:05 +00:00
return m, nil
2021-12-02 04:00:33 +00:00
default:
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
top, right, bottom, left := lipgloss.NewStyle().Margin(2, 2).GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
m.root.SetSize(msg)
}
return m, nil
}
func (m modMenu) View() string {
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
}