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

170 lines
4.4 KiB
Go
Raw Normal View History

package mods
2021-12-02 04:00:33 +00:00
import (
"context"
"fmt"
"time"
2021-12-02 04:00:33 +00:00
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
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/ficsit"
"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 = (*selectModVersionList)(nil)
type selectModVersionList struct {
root components.RootModel
list list.Model
parent tea.Model
items chan []list.Item
err chan string
error *components.ErrorComponent
2021-12-02 04:00:33 +00:00
}
func NewModVersionList(root components.RootModel, parent tea.Model, mod utils.Mod) tea.Model {
2022-04-14 01:27:39 +00:00
l := list.New([]list.Item{}, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
2021-12-02 04:00:33 +00:00
l.SetShowStatusBar(true)
l.SetFilteringEnabled(false)
l.SetSpinner(spinner.MiniDot)
l.Title = fmt.Sprintf("Versions (%s)", mod.Name)
l.Styles = utils.ListStyles
l.SetSize(l.Width(), l.Height())
l.KeyMap.Quit.SetHelp("q", "back")
2021-12-02 04:00:33 +00:00
m := &selectModVersionList{
root: root,
list: l,
parent: parent,
items: make(chan []list.Item),
err: make(chan string),
2021-12-02 04:00:33 +00:00
}
go func() {
items := make([]list.Item, 0)
2022-04-14 01:27:39 +00:00
allVersions := make([]ficsit.ModVersionsModVersionsVersion, 0)
2021-12-02 04:00:33 +00:00
offset := 0
for {
2022-06-06 23:55:26 +00:00
versions, err := ficsit.ModVersions(context.TODO(), root.GetAPIClient(), mod.Reference, ficsit.VersionFilter{
2021-12-02 04:00:33 +00:00
Limit: 100,
Offset: offset,
Order: ficsit.OrderDesc,
Order_by: ficsit.VersionFieldsCreatedAt,
})
if err != nil {
m.err <- err.Error()
return
2021-12-02 04:00:33 +00:00
}
2022-04-14 01:27:39 +00:00
if len(versions.Mod.Versions) == 0 {
2021-12-02 04:00:33 +00:00
break
}
2022-04-14 01:27:39 +00:00
allVersions = append(allVersions, versions.Mod.Versions...)
2021-12-02 04:00:33 +00:00
2022-04-14 01:27:39 +00:00
for i := 0; i < len(versions.Mod.Versions); i++ {
2021-12-02 04:00:33 +00:00
currentOffset := offset
currentI := i
2022-04-14 01:27:39 +00:00
items = append(items, utils.SimpleItem[selectModVersionList]{
ItemTitle: versions.Mod.Versions[i].Version,
Activate: func(msg tea.Msg, currentModel selectModVersionList) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
version := allVersions[currentOffset+currentI]
err := root.GetCurrentProfile().AddMod(mod.Reference, version.Version)
if err != nil {
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
2021-12-02 04:00:33 +00:00
}
2022-04-14 01:27:39 +00:00
return currentModel.parent, nil
2021-12-02 04:00:33 +00:00
},
})
}
2022-04-14 01:27:39 +00:00
offset += len(versions.Mod.Versions)
2021-12-02 04:00:33 +00:00
}
m.items <- items
}()
return m
}
func (m selectModVersionList) Init() tea.Cmd {
return utils.Ticker()
}
func (m selectModVersionList) 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 "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[selectModVersionList])
2021-12-02 04:00:33 +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
}
}
2021-12-04 18:02:05 +00:00
return m, nil
2021-12-02 04:00:33 +00:00
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(m.root.Height(), 2, 0).GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
m.root.SetSize(msg)
case spinner.TickMsg:
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
case utils.TickMsg:
select {
case items := <-m.items:
m.list.StopSpinner()
cmd := m.list.SetItems(items)
return m, cmd
case err := <-m.err:
errorComponent, cmd := components.NewErrorComponent(err, time.Second*5)
m.error = errorComponent
return m, cmd
2021-12-02 04:00:33 +00:00
default:
start := m.list.StartSpinner()
return m, tea.Batch(utils.Ticker(), start)
}
}
return m, nil
}
func (m selectModVersionList) 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-02 04:00:33 +00:00
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
}