2023-01-16 22:48:14 +00:00
|
|
|
package mods
|
2021-11-05 21:42:49 +00:00
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2022-06-06 23:55:26 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-12-04 03:42:31 +00:00
|
|
|
"sort"
|
2022-06-03 22:17:02 +00:00
|
|
|
"time"
|
2021-11-05 21:42:49 +00:00
|
|
|
|
2021-12-04 03:48:15 +00:00
|
|
|
"github.com/charmbracelet/bubbles/key"
|
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-06-06 23:55:26 +00:00
|
|
|
"github.com/muesli/reflow/truncate"
|
2022-10-14 16:11:16 +00:00
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli"
|
2021-12-02 04:00:33 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/ficsit"
|
|
|
|
"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-02 04:00:33 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ tea.Model = (*modsList)(nil)
|
|
|
|
|
2021-12-04 03:48:15 +00:00
|
|
|
type sortOrder string
|
|
|
|
|
|
|
|
const (
|
|
|
|
sortOrderAsc sortOrder = "asc"
|
|
|
|
sortOrderDesc sortOrder = "desc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const modsTitle = "Mods"
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
type listUpdate struct {
|
|
|
|
Items []list.Item
|
|
|
|
Done bool
|
|
|
|
}
|
|
|
|
|
2023-01-16 22:48:14 +00:00
|
|
|
// type keys
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
type modsList struct {
|
2022-10-14 16:11:16 +00:00
|
|
|
list list.Model
|
2021-12-04 03:42:31 +00:00
|
|
|
sortFieldList list.Model
|
|
|
|
sortOrderList list.Model
|
2022-10-14 16:11:16 +00:00
|
|
|
root components.RootModel
|
|
|
|
parent tea.Model
|
|
|
|
items chan listUpdate
|
|
|
|
err chan string
|
|
|
|
error *components.ErrorComponent
|
|
|
|
sortingField string
|
|
|
|
sortingOrder sortOrder
|
|
|
|
showSortFieldList bool
|
|
|
|
showSortOrderList bool
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMods(root components.RootModel, parent tea.Model) tea.Model {
|
2023-06-18 15:41:24 +00:00
|
|
|
l := list.New([]list.Item{}, ListDelegate{
|
2022-06-06 23:55:26 +00:00
|
|
|
ItemDelegate: utils.NewItemDelegate(),
|
|
|
|
Context: root.GetGlobal(),
|
|
|
|
}, root.Size().Width, root.Size().Height-root.Height())
|
2021-12-02 04:00:33 +00:00
|
|
|
l.SetShowStatusBar(true)
|
2021-12-04 03:42:31 +00:00
|
|
|
l.SetShowFilter(true)
|
|
|
|
l.SetFilteringEnabled(true)
|
2021-12-02 04:00:33 +00:00
|
|
|
l.SetSpinner(spinner.MiniDot)
|
2021-12-04 03:48:15 +00:00
|
|
|
l.Title = modsTitle
|
2021-12-02 04:00:33 +00:00
|
|
|
l.Styles = utils.ListStyles
|
|
|
|
l.SetSize(l.Width(), l.Height())
|
2023-01-16 22:48:14 +00:00
|
|
|
l.KeyMap.Quit.SetHelp("q", "back")
|
2021-12-04 18:02:05 +00:00
|
|
|
|
2021-12-04 03:42:31 +00:00
|
|
|
l.AdditionalShortHelpKeys = func() []key.Binding {
|
|
|
|
return []key.Binding{
|
2023-01-16 22:48:14 +00:00
|
|
|
key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "sort")),
|
|
|
|
key.NewBinding(key.WithKeys("o"), key.WithHelp("o", "order")),
|
2023-06-20 03:11:25 +00:00
|
|
|
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
|
2021-12-04 18:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 22:48:14 +00:00
|
|
|
l.AdditionalFullHelpKeys = l.AdditionalShortHelpKeys
|
2021-12-04 18:02:05 +00:00
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
sortFieldList := list.New([]list.Item{
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Name",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "name"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Last Version Date",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "last_version_date"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Creation Date",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "created_at"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Downloads",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "downloads"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2022-06-04 18:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Views",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "views"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2022-06-04 18:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Popularity (recent downloads)",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "popularity"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2022-06-04 18:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Hotness (recent views)",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingField = "hotness"
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2022-06-04 18:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
}, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
|
|
|
|
sortFieldList.SetShowStatusBar(true)
|
|
|
|
sortFieldList.SetShowFilter(false)
|
|
|
|
sortFieldList.SetFilteringEnabled(false)
|
2021-12-04 03:48:15 +00:00
|
|
|
sortFieldList.Title = modsTitle
|
2021-12-04 03:42:31 +00:00
|
|
|
sortFieldList.Styles = utils.ListStyles
|
|
|
|
sortFieldList.SetSize(l.Width(), l.Height())
|
2023-06-20 03:11:25 +00:00
|
|
|
sortFieldList.AdditionalShortHelpKeys = func() []key.Binding {
|
|
|
|
return []key.Binding{
|
|
|
|
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 03:42:31 +00:00
|
|
|
|
2022-04-14 01:27:39 +00:00
|
|
|
sortOrderList := list.New([]list.Item{
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Ascending",
|
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingOrder = sortOrderAsc
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
2022-10-14 16:11:16 +00:00
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
|
|
|
ItemTitle: "Descending",
|
2022-06-06 23:55:26 +00:00
|
|
|
Activate: func(msg tea.Msg, m modsList) (tea.Model, tea.Cmd) {
|
|
|
|
m.sortingOrder = sortOrderDesc
|
|
|
|
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
|
|
|
|
m.list.ResetSelected()
|
|
|
|
return m, cmd
|
|
|
|
},
|
2021-12-04 03:42:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
|
|
|
|
sortOrderList.SetShowStatusBar(true)
|
|
|
|
sortOrderList.SetShowFilter(false)
|
|
|
|
sortOrderList.SetFilteringEnabled(false)
|
2021-12-04 03:48:15 +00:00
|
|
|
sortOrderList.Title = modsTitle
|
2021-12-04 03:42:31 +00:00
|
|
|
sortOrderList.Styles = utils.ListStyles
|
|
|
|
sortOrderList.SetSize(l.Width(), l.Height())
|
2023-06-20 03:11:25 +00:00
|
|
|
sortOrderList.AdditionalShortHelpKeys = func() []key.Binding {
|
|
|
|
return []key.Binding{
|
|
|
|
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 04:00:33 +00:00
|
|
|
|
|
|
|
m := &modsList{
|
2021-12-04 03:42:31 +00:00
|
|
|
root: root,
|
|
|
|
list: l,
|
|
|
|
parent: parent,
|
2022-06-06 23:55:26 +00:00
|
|
|
items: make(chan listUpdate),
|
2021-12-04 03:42:31 +00:00
|
|
|
sortingField: "last_version_date",
|
2021-12-04 03:48:15 +00:00
|
|
|
sortingOrder: sortOrderDesc,
|
2021-12-04 03:42:31 +00:00
|
|
|
sortFieldList: sortFieldList,
|
|
|
|
sortOrderList: sortOrderList,
|
2022-06-03 22:17:02 +00:00
|
|
|
err: make(chan string),
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
items := make([]list.Item, 0)
|
2022-06-06 23:55:26 +00:00
|
|
|
allMods := make([]ficsit.ModsModsGetModsModsMod, 0)
|
2021-12-02 04:00:33 +00:00
|
|
|
offset := 0
|
|
|
|
for {
|
2023-12-06 04:47:41 +00:00
|
|
|
mods, err := root.GetProvider().Mods(context.TODO(), ficsit.ModFilter{
|
2021-12-02 04:00:33 +00:00
|
|
|
Limit: 100,
|
|
|
|
Offset: offset,
|
|
|
|
Order_by: ficsit.ModFieldsLastVersionDate,
|
|
|
|
Order: ficsit.OrderDesc,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-03 22:17:02 +00:00
|
|
|
m.err <- err.Error()
|
|
|
|
return
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
if len(mods.Mods.Mods) == 0 {
|
2021-12-02 04:00:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
allMods = append(allMods, mods.Mods.Mods...)
|
2021-12-02 04:00:33 +00:00
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
for i := 0; i < len(mods.Mods.Mods); i++ {
|
2021-12-02 04:00:33 +00:00
|
|
|
currentOffset := offset
|
|
|
|
currentI := i
|
2022-06-06 23:55:26 +00:00
|
|
|
items = append(items, utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod]{
|
2022-04-14 01:27:39 +00:00
|
|
|
SimpleItem: utils.SimpleItem[modsList]{
|
2022-06-06 23:55:26 +00:00
|
|
|
ItemTitle: mods.Mods.Mods[i].Name,
|
2022-04-14 01:27:39 +00:00
|
|
|
Activate: func(msg tea.Msg, currentModel modsList) (tea.Model, tea.Cmd) {
|
|
|
|
mod := allMods[currentOffset+currentI]
|
|
|
|
return NewModMenu(root, currentModel, utils.Mod{
|
|
|
|
Name: mod.Name,
|
|
|
|
Reference: mod.Mod_reference,
|
|
|
|
}), nil
|
|
|
|
},
|
2021-12-02 04:00:33 +00:00
|
|
|
},
|
2022-06-06 23:55:26 +00:00
|
|
|
Extra: allMods[currentOffset+currentI],
|
2021-12-02 04:00:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
offset += len(mods.Mods.Mods)
|
|
|
|
|
|
|
|
m.items <- listUpdate{
|
|
|
|
Items: items,
|
|
|
|
Done: false,
|
|
|
|
}
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
m.items <- listUpdate{
|
|
|
|
Items: items,
|
|
|
|
Done: true,
|
|
|
|
}
|
2021-12-02 04:00:33 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m modsList) Init() tea.Cmd {
|
2022-06-07 00:32:46 +00:00
|
|
|
if len(m.list.Items()) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
return utils.Ticker()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m modsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2023-01-15 23:57:11 +00:00
|
|
|
m.list.KeyMap.Quit.SetHelp("q", "back")
|
2023-06-20 03:11:25 +00:00
|
|
|
m.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
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
2021-12-04 03:42:31 +00:00
|
|
|
if m.list.SettingFilter() {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.list, cmd = m.list.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
switch keypress := msg.String(); keypress {
|
2021-12-04 03:42:31 +00:00
|
|
|
case "s":
|
|
|
|
m.showSortFieldList = !m.showSortFieldList
|
|
|
|
return m, nil
|
|
|
|
case "o":
|
|
|
|
m.showSortOrderList = !m.showSortOrderList
|
|
|
|
return m, nil
|
2023-01-16 22:48:14 +00:00
|
|
|
case keys.KeyControlC:
|
2021-12-02 04:00:33 +00:00
|
|
|
return m, tea.Quit
|
|
|
|
case "q":
|
2021-12-04 03:42:31 +00:00
|
|
|
if m.showSortFieldList {
|
|
|
|
m.showSortFieldList = false
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.showSortOrderList {
|
|
|
|
m.showSortOrderList = false
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:00:33 +00:00
|
|
|
if m.parent != nil {
|
|
|
|
m.parent.Update(m.root.Size())
|
|
|
|
return m.parent, nil
|
|
|
|
}
|
|
|
|
return m, tea.Quit
|
2023-01-16 22:48:14 +00:00
|
|
|
case keys.KeyEnter:
|
2021-12-04 03:42:31 +00:00
|
|
|
if m.showSortFieldList {
|
|
|
|
m.showSortFieldList = false
|
2022-06-06 23:55:26 +00:00
|
|
|
i, ok := m.sortFieldList.SelectedItem().(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
2021-12-04 03:42:31 +00:00
|
|
|
if ok {
|
|
|
|
return m.processActivation(i, msg)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.showSortOrderList {
|
|
|
|
m.showSortOrderList = false
|
2022-06-06 23:55:26 +00:00
|
|
|
i, ok := m.sortOrderList.SelectedItem().(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
2021-12-04 03:42:31 +00:00
|
|
|
if ok {
|
|
|
|
return m.processActivation(i, msg)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
i, ok := m.list.SelectedItem().(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
2021-12-02 04:00:33 +00:00
|
|
|
if ok {
|
2022-06-06 23:55:26 +00:00
|
|
|
return m.processActivation(i, msg)
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
2021-12-04 03:42:31 +00:00
|
|
|
return m, nil
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
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 utils.TickMsg:
|
|
|
|
select {
|
|
|
|
case items := <-m.items:
|
2022-06-06 23:55:26 +00:00
|
|
|
cmd := m.list.SetItems(items.Items)
|
|
|
|
if items.Done {
|
|
|
|
m.list.StopSpinner()
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
return m, tea.Batch(utils.Ticker(), cmd)
|
2022-06-03 22:17:02 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:42:31 +00:00
|
|
|
if m.showSortFieldList {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.sortFieldList, cmd = m.sortFieldList.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
} else if m.showSortOrderList {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.sortOrderList, cmd = m.sortOrderList.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.list, cmd = m.list.Update(msg)
|
|
|
|
return m, cmd
|
2021-12-02 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m modsList) View() string {
|
2022-06-03 22:17:02 +00:00
|
|
|
var bottomList list.Model
|
2021-12-04 03:42:31 +00:00
|
|
|
if m.showSortFieldList {
|
2022-06-03 22:17:02 +00:00
|
|
|
bottomList = m.sortFieldList
|
2021-12-04 03:42:31 +00:00
|
|
|
} else if m.showSortOrderList {
|
2022-06-03 22:17:02 +00:00
|
|
|
bottomList = m.sortOrderList
|
2021-12-04 03:42:31 +00:00
|
|
|
} else {
|
2022-06-03 22:17:02 +00:00
|
|
|
bottomList = m.list
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.error != nil {
|
2022-10-14 16:11:16 +00:00
|
|
|
err := m.error.View()
|
2022-06-03 22:17:02 +00:00
|
|
|
bottomList.SetSize(bottomList.Width(), m.root.Size().Height-m.root.Height()-lipgloss.Height(err))
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), err, bottomList.View())
|
2021-12-04 03:42:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
bottomList.SetSize(bottomList.Width(), m.root.Size().Height-m.root.Height())
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), bottomList.View())
|
2021-12-04 03:42:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 03:48:15 +00:00
|
|
|
func sortItems(items []list.Item, field string, direction sortOrder) []list.Item {
|
2021-12-04 03:42:31 +00:00
|
|
|
sortedItems := make([]list.Item, len(items))
|
|
|
|
copy(sortedItems, items)
|
|
|
|
|
|
|
|
switch field {
|
|
|
|
case "last_version_date":
|
2022-04-14 01:27:39 +00:00
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Last_version_date.Before(b.Extra.Last_version_date))
|
2022-04-14 01:27:39 +00:00
|
|
|
})
|
2021-12-04 03:42:31 +00:00
|
|
|
case "created_at":
|
2022-04-14 01:27:39 +00:00
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Created_at.Before(b.Extra.Created_at))
|
2022-04-14 01:27:39 +00:00
|
|
|
})
|
2021-12-04 03:42:31 +00:00
|
|
|
case "name":
|
2022-04-14 01:27:39 +00:00
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Name < b.Extra.Name)
|
2022-06-04 18:47:10 +00:00
|
|
|
})
|
|
|
|
case "downloads":
|
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Downloads < b.Extra.Downloads)
|
2022-06-04 18:47:10 +00:00
|
|
|
})
|
|
|
|
case "views":
|
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Views < b.Extra.Views)
|
2022-06-04 18:47:10 +00:00
|
|
|
})
|
|
|
|
case "popularity":
|
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Popularity < b.Extra.Popularity)
|
2022-06-04 18:47:10 +00:00
|
|
|
})
|
|
|
|
case "hotness":
|
|
|
|
sort.Slice(sortedItems, func(i, j int) bool {
|
2022-06-06 23:55:26 +00:00
|
|
|
a := sortedItems[i].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
b := sortedItems[j].(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
return ascDesc(direction, a.Extra.Hotness < b.Extra.Hotness)
|
2022-04-14 01:27:39 +00:00
|
|
|
})
|
2021-12-04 03:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sortedItems
|
|
|
|
}
|
|
|
|
|
2022-06-06 23:55:26 +00:00
|
|
|
func (m modsList) processActivation(item utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod], msg tea.Msg) (tea.Model, tea.Cmd) {
|
2021-12-04 03:42:31 +00:00
|
|
|
if item.Activate != nil {
|
|
|
|
newModel, cmd := item.Activate(msg, m)
|
|
|
|
if newModel != nil || cmd != nil {
|
|
|
|
if newModel == nil {
|
|
|
|
newModel = m
|
|
|
|
}
|
|
|
|
return newModel, cmd
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return m, nil
|
2021-11-05 21:42:49 +00:00
|
|
|
}
|
2022-04-14 01:27:39 +00:00
|
|
|
|
|
|
|
func ascDesc(order sortOrder, result bool) bool {
|
|
|
|
if order == sortOrderAsc {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return !result
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
|
2023-06-18 15:41:24 +00:00
|
|
|
type ListDelegate struct {
|
2022-06-06 23:55:26 +00:00
|
|
|
list.ItemDelegate
|
|
|
|
Context *cli.GlobalContext
|
|
|
|
}
|
|
|
|
|
2023-06-18 15:41:24 +00:00
|
|
|
func (c ListDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
2022-06-06 23:55:26 +00:00
|
|
|
realItem := item.(utils.SimpleItemExtra[modsList, ficsit.ModsModsGetModsModsMod])
|
|
|
|
realDelegate := c.ItemDelegate.(list.DefaultDelegate)
|
|
|
|
|
|
|
|
title := realItem.Title()
|
|
|
|
|
|
|
|
s := &realDelegate.Styles
|
|
|
|
|
|
|
|
if m.Width() <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
textwidth := uint(m.Width() - s.NormalTitle.GetPaddingLeft() - s.NormalTitle.GetPaddingRight())
|
|
|
|
title = truncate.StringWithTail(title, textwidth, "…")
|
|
|
|
|
|
|
|
var (
|
|
|
|
isSelected = index == m.Index()
|
|
|
|
emptyFilter = m.FilterState() == list.Filtering && m.FilterValue() == ""
|
|
|
|
isFiltered = m.FilterState() == list.Filtering || m.FilterState() == list.FilterApplied
|
|
|
|
)
|
|
|
|
|
|
|
|
var matchedRunes []int
|
|
|
|
if isFiltered && index < len(m.VisibleItems()) {
|
|
|
|
// Get indices of matched characters
|
|
|
|
matchedRunes = m.MatchesForItem(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
isInstalled := false
|
2022-06-07 23:36:28 +00:00
|
|
|
isDisabled := false
|
2022-06-06 23:55:26 +00:00
|
|
|
if c.Context != nil {
|
|
|
|
profile := c.Context.Profiles.Profiles[c.Context.Profiles.SelectedProfile]
|
|
|
|
if profile != nil {
|
|
|
|
if profile.HasMod(realItem.Extra.Mod_reference) {
|
|
|
|
isInstalled = true
|
2022-06-07 23:36:28 +00:00
|
|
|
isDisabled = !profile.IsModEnabled(realItem.Extra.Mod_reference)
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if emptyFilter {
|
|
|
|
if isInstalled {
|
2022-06-07 23:36:28 +00:00
|
|
|
if isDisabled {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("220")).Render("✓ " + title)
|
|
|
|
} else {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("40")).Render("✓ " + title)
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
title = s.DimmedTitle.Render(title)
|
|
|
|
} else if isSelected && m.FilterState() != list.Filtering {
|
|
|
|
if isFiltered {
|
|
|
|
unmatched := s.SelectedTitle.Inline(true)
|
|
|
|
matched := unmatched.Copy().Inherit(s.FilterMatch)
|
|
|
|
if isInstalled {
|
2022-06-07 23:36:28 +00:00
|
|
|
if isDisabled {
|
|
|
|
unmatched = unmatched.Foreground(lipgloss.Color("220"))
|
|
|
|
matched = matched.Foreground(lipgloss.Color("220"))
|
|
|
|
} else {
|
|
|
|
unmatched = unmatched.Foreground(lipgloss.Color("40"))
|
|
|
|
matched = matched.Foreground(lipgloss.Color("40"))
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
title = lipgloss.StyleRunes(title, matchedRunes, matched, unmatched)
|
|
|
|
}
|
|
|
|
if isInstalled {
|
2022-06-07 23:36:28 +00:00
|
|
|
if isDisabled {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("220")).Render("✓ ") + title
|
|
|
|
} else {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("40")).Render("✓ ") + title
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
title = s.SelectedTitle.Render(title)
|
|
|
|
} else {
|
|
|
|
if isFiltered {
|
|
|
|
unmatched := s.NormalTitle.Inline(true)
|
|
|
|
matched := unmatched.Copy().Inherit(s.FilterMatch)
|
|
|
|
if isInstalled {
|
2022-06-07 23:36:28 +00:00
|
|
|
if isDisabled {
|
|
|
|
unmatched = unmatched.Foreground(lipgloss.Color("220"))
|
|
|
|
matched = matched.Foreground(lipgloss.Color("220"))
|
|
|
|
} else {
|
|
|
|
unmatched = unmatched.Foreground(lipgloss.Color("40"))
|
|
|
|
matched = matched.Foreground(lipgloss.Color("40"))
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
title = lipgloss.StyleRunes(title, matchedRunes, matched, unmatched)
|
|
|
|
}
|
|
|
|
if isInstalled {
|
2022-06-07 23:36:28 +00:00
|
|
|
if isDisabled {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("220")).Render("✓ ") + title
|
|
|
|
} else {
|
|
|
|
title = lipgloss.NewStyle().Foreground(lipgloss.Color("40")).Render("✓ ") + title
|
|
|
|
}
|
2022-06-06 23:55:26 +00:00
|
|
|
}
|
|
|
|
title = s.NormalTitle.Render(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", title)
|
|
|
|
}
|