2023-01-16 22:48:14 +00:00
|
|
|
package profile
|
2021-12-04 18:02:05 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-03 22:17:02 +00:00
|
|
|
"time"
|
|
|
|
|
2023-01-15 23:57:11 +00:00
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
2021-12-04 18:02:05 +00:00
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
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/tea/components"
|
2023-01-16 22:48:14 +00:00
|
|
|
"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 = (*newProfile)(nil)
|
|
|
|
|
2023-01-15 23:57:11 +00:00
|
|
|
type keyMap struct {
|
|
|
|
Back key.Binding
|
|
|
|
Quit key.Binding
|
|
|
|
Enter key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyMap) ShortHelp() []key.Binding {
|
|
|
|
return []key.Binding{k.Enter, k.Back}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyMap) FullHelp() [][]key.Binding {
|
|
|
|
return [][]key.Binding{
|
2023-01-15 23:58:29 +00:00
|
|
|
{k.Enter, k.Back},
|
2023-01-15 23:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:02:05 +00:00
|
|
|
type newProfile struct {
|
|
|
|
root components.RootModel
|
|
|
|
parent tea.Model
|
2022-06-03 22:17:02 +00:00
|
|
|
error *components.ErrorComponent
|
2023-01-15 23:57:11 +00:00
|
|
|
help help.Model
|
2023-12-16 14:19:53 +00:00
|
|
|
title string
|
2023-01-15 23:57:11 +00:00
|
|
|
keys keyMap
|
2023-12-16 14:19:53 +00:00
|
|
|
input textinput.Model
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNewProfile(root components.RootModel, parent tea.Model) tea.Model {
|
|
|
|
model := newProfile{
|
|
|
|
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("New Profile"),
|
2023-01-15 23:57:11 +00:00
|
|
|
help: help.New(),
|
|
|
|
keys: keyMap{
|
|
|
|
Back: key.NewBinding(
|
2023-01-16 22:48:14 +00:00
|
|
|
key.WithKeys(keys.KeyEscape, keys.KeyControlC),
|
|
|
|
key.WithHelp(keys.KeyEscape, "back"),
|
2023-01-15 23:57:11 +00:00
|
|
|
),
|
|
|
|
Enter: key.NewBinding(
|
2023-01-16 22:48:14 +00:00
|
|
|
key.WithKeys(keys.KeyEnter),
|
|
|
|
key.WithHelp(keys.KeyEnter, "create"),
|
2023-01-15 23:57:11 +00:00
|
|
|
),
|
|
|
|
Quit: key.NewBinding(
|
2023-01-16 22:48:14 +00:00
|
|
|
key.WithKeys(keys.KeyControlC),
|
2023-01-15 23:57:11 +00:00
|
|
|
),
|
|
|
|
},
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
model.input.Focus()
|
|
|
|
model.input.Width = root.Size().Width
|
|
|
|
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m newProfile) Init() tea.Cmd {
|
2022-06-05 04:07:19 +00:00
|
|
|
return textinput.Blink
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m newProfile) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
2023-01-15 23:57:11 +00:00
|
|
|
switch {
|
|
|
|
case key.Matches(msg, m.keys.Quit):
|
2021-12-04 18:02:05 +00:00
|
|
|
return m, tea.Quit
|
2023-01-15 23:57:11 +00:00
|
|
|
case key.Matches(msg, m.keys.Back):
|
2021-12-04 18:02:05 +00:00
|
|
|
return m.parent, nil
|
2023-01-15 23:57:11 +00:00
|
|
|
case key.Matches(msg, m.keys.Enter):
|
2021-12-04 18:02:05 +00:00
|
|
|
if _, err := m.root.GetGlobal().Profiles.AddProfile(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, updateProfileListCmd
|
|
|
|
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 newProfile) View() string {
|
2023-01-15 23:57:11 +00:00
|
|
|
style := lipgloss.NewStyle().Padding(1, 2)
|
|
|
|
inputView := style.Render(m.input.View())
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
if m.error != nil {
|
2022-10-14 16:11:16 +00:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, m.error.View(), inputView)
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 04:07:19 +00:00
|
|
|
infoBox := lipgloss.NewStyle().
|
|
|
|
BorderStyle(lipgloss.ThickBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("39")).
|
|
|
|
Padding(0, 1).
|
|
|
|
Margin(0, 0, 0, 2).
|
|
|
|
Render("Enter the name of the profile")
|
|
|
|
|
2023-01-15 23:57:11 +00:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView, infoBox, style.Render(m.help.View(m.keys)))
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|