2021-12-04 18:02:05 +00:00
|
|
|
package scenes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-03 22:17:02 +00:00
|
|
|
"time"
|
2021-12-04 18:02:05 +00:00
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/tea/components"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ tea.Model = (*renameProfile)(nil)
|
|
|
|
|
|
|
|
type renameProfile struct {
|
|
|
|
root components.RootModel
|
|
|
|
parent tea.Model
|
|
|
|
input textinput.Model
|
|
|
|
title string
|
|
|
|
oldName string
|
2022-06-03 22:17:02 +00:00
|
|
|
error *components.ErrorComponent
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRenameProfile(root components.RootModel, parent tea.Model, profileData *cli.Profile) tea.Model {
|
|
|
|
model := renameProfile{
|
|
|
|
root: root,
|
|
|
|
parent: parent,
|
2022-04-14 01:27:39 +00:00
|
|
|
input: textinput.New(),
|
2021-12-04 18:02:05 +00:00
|
|
|
title: utils.NonListTitleStyle.Render(fmt.Sprintf("Rename Profile: %s", profileData.Name)),
|
|
|
|
oldName: profileData.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
model.input.SetValue(profileData.Name)
|
|
|
|
model.input.Focus()
|
|
|
|
model.input.Width = root.Size().Width
|
|
|
|
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m renameProfile) Init() tea.Cmd {
|
2022-06-05 04:07:19 +00:00
|
|
|
return textinput.Blink
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m renameProfile) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch keypress := msg.String(); keypress {
|
|
|
|
case KeyControlC:
|
|
|
|
return m, tea.Quit
|
|
|
|
case KeyEscape:
|
|
|
|
return m.parent, nil
|
|
|
|
case KeyEnter:
|
2022-06-08 20:56:32 +00:00
|
|
|
if err := m.root.GetGlobal().Profiles.RenameProfile(m.root.GetGlobal(), m.oldName, m.input.Value()); err != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
|
|
|
|
m.error = errorComponent
|
|
|
|
return m, cmd
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m.parent, updateProfileNamesCmd
|
|
|
|
default:
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.root.SetSize(msg)
|
2022-06-03 22:17:02 +00:00
|
|
|
case components.ErrorComponentTimeoutMsg:
|
|
|
|
m.error = nil
|
2022-06-05 04:07:19 +00:00
|
|
|
default:
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
return m, cmd
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m renameProfile) View() string {
|
|
|
|
inputView := lipgloss.NewStyle().Padding(1, 2).Render(m.input.View())
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
if m.error != nil {
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, (*m.error).View(), inputView)
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:02:05 +00:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView)
|
|
|
|
}
|