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

157 lines
4.2 KiB
Go
Raw Normal View History

package profile
2021-12-04 18:02:05 +00:00
import (
"fmt"
"time"
"github.com/charmbracelet/bubbles/key"
2021-12-04 18:02:05 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2022-10-14 16:11:16 +00:00
2021-12-04 18:02:05 +00:00
"github.com/satisfactorymodding/ficsit-cli/cli"
"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"
)
var _ tea.Model = (*profile)(nil)
type profile struct {
list list.Model
2022-10-14 16:11:16 +00:00
root components.RootModel
2021-12-04 18:02:05 +00:00
parent tea.Model
profile *cli.Profile
error *components.ErrorComponent
2022-10-14 16:11:16 +00:00
hadRenamed bool
2021-12-04 18:02:05 +00:00
}
func NewProfile(root components.RootModel, parent tea.Model, profileData *cli.Profile) tea.Model {
model := profile{
root: root,
parent: parent,
profile: profileData,
}
items := []list.Item{
2022-04-14 01:27:39 +00:00
utils.SimpleItem[profile]{
2021-12-04 18:02:05 +00:00
ItemTitle: "Select",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel profile) (tea.Model, tea.Cmd) {
2021-12-04 18:02:05 +00:00
if err := root.SetCurrentProfile(profileData); err != nil {
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
2021-12-04 18:02:05 +00:00
}
2022-04-14 01:27:39 +00:00
return currentModel.parent, nil
2021-12-04 18:02:05 +00:00
},
},
}
if profileData.Name != cli.DefaultProfileName {
items = append(items,
2022-04-14 01:27:39 +00:00
utils.SimpleItem[profile]{
2021-12-04 18:02:05 +00:00
ItemTitle: "Rename",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel profile) (tea.Model, tea.Cmd) {
2021-12-04 18:02:05 +00:00
newModel := NewRenameProfile(root, currentModel, profileData)
return newModel, newModel.Init()
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[profile]{
2021-12-04 18:02:05 +00:00
ItemTitle: "Delete",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel profile) (tea.Model, tea.Cmd) {
2021-12-04 18:02:05 +00:00
if err := root.GetGlobal().Profiles.DeleteProfile(profileData.Name); err != nil {
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
2021-12-04 18:02:05 +00:00
}
2022-04-14 01:27:39 +00:00
return currentModel.parent, updateProfileListCmd
2021-12-04 18:02:05 +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-04 18:02:05 +00:00
model.list.SetShowStatusBar(false)
model.list.SetFilteringEnabled(false)
model.list.Title = fmt.Sprintf("Profile: %s", profileData.Name)
model.list.Styles = utils.ListStyles
model.list.SetSize(model.list.Width(), model.list.Height())
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-04 18:02:05 +00:00
return model
}
func (m profile) Init() tea.Cmd {
return nil
}
func (m profile) 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-04 18:02:05 +00:00
return m, tea.Quit
case "q":
if m.parent != nil {
m.parent.Update(m.root.Size())
if m.hadRenamed {
return m.parent, updateProfileNamesCmd
}
return m.parent, nil
}
return m, nil
case keys.KeyEnter:
2022-04-14 01:27:39 +00:00
i, ok := m.list.SelectedItem().(utils.SimpleItem[profile])
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
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)
case updateProfileNames:
m.hadRenamed = true
m.list.Title = fmt.Sprintf("Profile: %s", m.profile.Name)
case components.ErrorComponentTimeoutMsg:
m.error = nil
2021-12-04 18:02:05 +00:00
}
return m, nil
}
func (m profile) View() string {
if m.error != nil {
2022-10-14 16:11:16 +00:00
err := m.error.View()
m.list.SetSize(m.list.Width(), m.root.Size().Height-m.root.Height()-lipgloss.Height(err))
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), err, m.list.View())
}
m.list.SetSize(m.list.Width(), m.root.Size().Height-m.root.Height())
2021-12-04 18:02:05 +00:00
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
}