ficsit-cli-flake/tea/scenes/mods/mod_semver.go

89 lines
2.1 KiB
Go
Raw Normal View History

package mods
2021-12-02 04:00:33 +00:00
import (
"time"
2021-12-02 04:00:33 +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-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/keys"
2021-12-02 04:00:33 +00:00
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
)
var _ tea.Model = (*modSemver)(nil)
type modSemver struct {
2022-10-14 16:11:16 +00:00
input textinput.Model
2021-12-02 04:00:33 +00:00
root components.RootModel
parent tea.Model
error *components.ErrorComponent
2022-10-14 16:11:16 +00:00
mod utils.Mod
title string
2021-12-02 04:00:33 +00:00
}
func NewModSemver(root components.RootModel, parent tea.Model, mod utils.Mod) tea.Model {
model := modSemver{
root: root,
parent: parent,
2022-04-14 01:27:39 +00:00
input: textinput.New(),
2021-12-02 04:00:33 +00:00
title: lipgloss.NewStyle().Padding(0, 2).Render(utils.TitleStyle.Render(mod.Name)),
mod: mod,
}
model.input.Placeholder = ">=1.2.3"
model.input.Focus()
model.input.Width = root.Size().Width
return model
}
func (m modSemver) Init() tea.Cmd {
return textinput.Blink
2021-12-02 04:00:33 +00:00
}
func (m modSemver) 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-02 04:00:33 +00:00
return m, tea.Quit
case keys.KeyEscape:
2021-12-04 18:02:05 +00:00
return m.parent, nil
case keys.KeyEnter:
2021-12-02 04:00:33 +00:00
err := m.root.GetCurrentProfile().AddMod(m.mod.Reference, m.input.Value())
if err != nil {
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
m.error = errorComponent
return m, cmd
2021-12-02 04:00:33 +00:00
}
return m.parent, nil
default:
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
m.root.SetSize(msg)
case components.ErrorComponentTimeoutMsg:
m.error = nil
default:
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
2021-12-02 04:00:33 +00:00
}
return m, nil
}
func (m modSemver) View() string {
inputView := lipgloss.NewStyle().Padding(1, 2).Render(m.input.View())
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)
}
2021-12-02 04:00:33 +00:00
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.title, inputView)
}