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

131 lines
3.3 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package scenes
2021-12-02 04:00:33 +00:00
import (
2022-04-14 01:27:39 +00:00
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
2021-12-02 04:00:33 +00:00
tea "github.com/charmbracelet/bubbletea"
2022-04-14 01:27:39 +00:00
"github.com/charmbracelet/lipgloss"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/components"
2022-04-14 01:27:39 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
2021-12-02 04:00:33 +00:00
)
2021-11-05 21:42:49 +00:00
2022-04-14 01:27:39 +00:00
var _ tea.Model = (*installations)(nil)
type installations struct {
root components.RootModel
list list.Model
parent tea.Model
}
2021-12-02 04:00:33 +00:00
func NewInstallations(root components.RootModel, parent tea.Model) tea.Model {
2022-04-14 01:27:39 +00:00
l := list.New(installationsToList(root), utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
l.SetShowStatusBar(true)
l.SetFilteringEnabled(true)
l.SetSpinner(spinner.MiniDot)
l.Title = "Installations"
l.Styles = utils.ListStyles
l.SetSize(l.Width(), l.Height())
l.KeyMap.Quit.SetHelp("q", "back")
l.DisableQuitKeybindings()
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithHelp("n", "new installation")),
}
}
l.AdditionalFullHelpKeys = l.AdditionalShortHelpKeys
return &installations{
root: root,
list: l,
parent: parent,
}
}
func (m installations) Init() tea.Cmd {
2021-11-05 21:42:49 +00:00
return nil
}
2022-04-14 01:27:39 +00:00
func (m installations) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// List enables its own keybindings when they were previously disabled
m.list.DisableQuitKeybindings()
switch msg := msg.(type) {
case tea.KeyMsg:
if m.list.SettingFilter() {
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
switch keypress := msg.String(); keypress {
case "n":
newModel := NewNewInstallation(m.root, m)
return newModel, newModel.Init()
case KeyControlC:
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 KeyEnter:
i, ok := m.list.SelectedItem().(utils.SimpleItem[installations])
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
}
}
return m, nil
}
case tea.WindowSizeMsg:
top, right, bottom, left := lipgloss.NewStyle().Margin(m.root.Height(), 2, 0).GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
m.root.SetSize(msg)
case updateInstallationList:
m.list.ResetSelected()
cmd := m.list.SetItems(installationsToList(m.root))
return m, cmd
case updateInstallationNames:
cmd := m.list.SetItems(installationsToList(m.root))
return m, cmd
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m installations) View() string {
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
}
func installationsToList(root components.RootModel) []list.Item {
items := make([]list.Item, len(root.GetGlobal().Installations.Installations))
i := 0
for _, installation := range root.GetGlobal().Installations.Installations {
temp := installation
items[i] = utils.SimpleItem[installations]{
ItemTitle: temp.Path,
Activate: func(msg tea.Msg, currentModel installations) (tea.Model, tea.Cmd) {
newModel := NewInstallation(root, currentModel, temp)
return newModel, newModel.Init()
},
}
i++
}
return items
}