ficsit-cli-flake/tea/scenes/profile/profiles.go

130 lines
3.2 KiB
Go
Raw Normal View History

package profile
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
import (
2021-12-04 18:02:05 +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"
2021-12-04 18:02:05 +00:00
"github.com/charmbracelet/lipgloss"
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/keys"
2021-12-04 18:02:05 +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
2021-12-04 18:02:05 +00:00
var _ tea.Model = (*profiles)(nil)
type profiles struct {
root components.RootModel
list list.Model
parent tea.Model
}
2021-12-02 04:00:33 +00:00
func NewProfiles(root components.RootModel, parent tea.Model) tea.Model {
2022-04-14 01:27:39 +00:00
l := list.New(profilesToList(root), utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
2021-12-04 18:02:05 +00:00
l.SetShowStatusBar(true)
l.SetFilteringEnabled(true)
l.SetSpinner(spinner.MiniDot)
l.Title = "Profiles"
l.Styles = utils.ListStyles
l.SetSize(l.Width(), l.Height())
l.KeyMap.Quit.SetHelp("q", "back")
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
2023-01-15 23:57:11 +00:00
key.NewBinding(key.WithKeys("n"), key.WithHelp("n", "new profile")),
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
2021-12-04 18:02:05 +00:00
}
}
2022-04-14 01:27:39 +00:00
l.AdditionalFullHelpKeys = l.AdditionalShortHelpKeys
2021-12-04 18:02:05 +00:00
return &profiles{
root: root,
list: l,
parent: parent,
}
}
func (m profiles) Init() tea.Cmd {
2021-11-05 21:42:49 +00:00
return nil
}
2021-12-04 18:02:05 +00:00
func (m profiles) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
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 := NewNewProfile(m.root, m)
return newModel, newModel.Init()
case keys.KeyControlC:
2021-12-04 18:02:05 +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[profiles])
2021-12-04 18:02:05 +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
}
}
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 updateProfileList:
m.list.ResetSelected()
cmd := m.list.SetItems(profilesToList(m.root))
return m, cmd
case updateProfileNames:
cmd := m.list.SetItems(profilesToList(m.root))
return m, cmd
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m profiles) View() string {
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
}
func profilesToList(root components.RootModel) []list.Item {
items := make([]list.Item, len(root.GetGlobal().Profiles.Profiles))
i := 0
for _, profile := range root.GetGlobal().Profiles.Profiles {
temp := profile
2022-04-14 01:27:39 +00:00
items[i] = utils.SimpleItem[profiles]{
2021-12-04 18:02:05 +00:00
ItemTitle: temp.Name,
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel profiles) (tea.Model, tea.Cmd) {
2021-12-04 18:02:05 +00:00
newModel := NewProfile(root, currentModel, temp)
return newModel, newModel.Init()
},
}
i++
}
return items
}