Linting fixes

This commit is contained in:
Vilsol 2021-12-04 05:48:15 +02:00
parent 072e6d911e
commit 94b9b16844
2 changed files with 25 additions and 13 deletions

View file

@ -3,6 +3,8 @@ package utils
import ( import (
"bytes" "bytes"
"time" "time"
"github.com/pkg/errors"
) )
//goland:noinspection GoUnusedExportedFunction //goland:noinspection GoUnusedExportedFunction
@ -16,7 +18,7 @@ func UnmarshalDateTime(b []byte, v *time.Time) error {
parsed, err := time.Parse(time.RFC3339, string(trimmed)) parsed, err := time.Parse(time.RFC3339, string(trimmed))
if err != nil { if err != nil {
return err return errors.Wrap(err, "failed to parse date time")
} }
*v = parsed *v = parsed

View file

@ -2,9 +2,10 @@ package scenes
import ( import (
"context" "context"
"github.com/charmbracelet/bubbles/key"
"sort" "sort"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@ -18,6 +19,15 @@ import (
var _ tea.Model = (*modsList)(nil) var _ tea.Model = (*modsList)(nil)
type sortOrder string
const (
sortOrderAsc sortOrder = "asc"
sortOrderDesc sortOrder = "desc"
)
const modsTitle = "Mods"
type modsList struct { type modsList struct {
root components.RootModel root components.RootModel
list list.Model list list.Model
@ -25,7 +35,7 @@ type modsList struct {
items chan []list.Item items chan []list.Item
sortingField string sortingField string
sortingOrder string sortingOrder sortOrder
showSortFieldList bool showSortFieldList bool
sortFieldList list.Model sortFieldList list.Model
@ -41,7 +51,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
l.SetShowFilter(true) l.SetShowFilter(true)
l.SetFilteringEnabled(true) l.SetFilteringEnabled(true)
l.SetSpinner(spinner.MiniDot) l.SetSpinner(spinner.MiniDot)
l.Title = "Mods" l.Title = modsTitle
l.Styles = utils.ListStyles l.Styles = utils.ListStyles
l.SetSize(l.Width(), l.Height()) l.SetSize(l.Width(), l.Height())
l.KeyMap.Quit.SetHelp("q", "back") l.KeyMap.Quit.SetHelp("q", "back")
@ -87,7 +97,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
sortFieldList.SetShowStatusBar(true) sortFieldList.SetShowStatusBar(true)
sortFieldList.SetShowFilter(false) sortFieldList.SetShowFilter(false)
sortFieldList.SetFilteringEnabled(false) sortFieldList.SetFilteringEnabled(false)
sortFieldList.Title = "Mods" sortFieldList.Title = modsTitle
sortFieldList.Styles = utils.ListStyles sortFieldList.Styles = utils.ListStyles
sortFieldList.SetSize(l.Width(), l.Height()) sortFieldList.SetSize(l.Width(), l.Height())
sortFieldList.KeyMap.Quit.SetHelp("q", "back") sortFieldList.KeyMap.Quit.SetHelp("q", "back")
@ -97,7 +107,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
ItemTitle: "Ascending", ItemTitle: "Ascending",
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) { Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
m := currentModel.(modsList) m := currentModel.(modsList)
m.sortingOrder = "asc" m.sortingOrder = sortOrderAsc
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder)) cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
m.list.ResetSelected() m.list.ResetSelected()
return m, cmd return m, cmd
@ -107,7 +117,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
ItemTitle: "Descending", ItemTitle: "Descending",
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) { Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
m := currentModel.(modsList) m := currentModel.(modsList)
m.sortingOrder = "desc" m.sortingOrder = sortOrderDesc
cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder)) cmd := m.list.SetItems(sortItems(m.list.Items(), m.sortingField, m.sortingOrder))
m.list.ResetSelected() m.list.ResetSelected()
return m, cmd return m, cmd
@ -117,7 +127,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
sortOrderList.SetShowStatusBar(true) sortOrderList.SetShowStatusBar(true)
sortOrderList.SetShowFilter(false) sortOrderList.SetShowFilter(false)
sortOrderList.SetFilteringEnabled(false) sortOrderList.SetFilteringEnabled(false)
sortOrderList.Title = "Mods" sortOrderList.Title = modsTitle
sortOrderList.Styles = utils.ListStyles sortOrderList.Styles = utils.ListStyles
sortOrderList.SetSize(l.Width(), l.Height()) sortOrderList.SetSize(l.Width(), l.Height())
sortOrderList.KeyMap.Quit.SetHelp("q", "back") sortOrderList.KeyMap.Quit.SetHelp("q", "back")
@ -128,7 +138,7 @@ func NewMods(root components.RootModel, parent tea.Model) tea.Model {
parent: parent, parent: parent,
items: make(chan []list.Item), items: make(chan []list.Item),
sortingField: "last_version_date", sortingField: "last_version_date",
sortingOrder: "desc", sortingOrder: sortOrderDesc,
sortFieldList: sortFieldList, sortFieldList: sortFieldList,
sortOrderList: sortOrderList, sortOrderList: sortOrderList,
} }
@ -291,14 +301,14 @@ func (m modsList) View() string {
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), bottom) return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), bottom)
} }
func sortItems(items []list.Item, field string, direction string) []list.Item { func sortItems(items []list.Item, field string, direction sortOrder) []list.Item {
sortedItems := make([]list.Item, len(items)) sortedItems := make([]list.Item, len(items))
copy(sortedItems, items) copy(sortedItems, items)
switch field { switch field {
case "last_version_date": case "last_version_date":
switch direction { switch direction {
case "asc": case sortOrderAsc:
sort.Slice(sortedItems, func(i, j int) bool { sort.Slice(sortedItems, func(i, j int) bool {
a := sortedItems[i].(utils.SimpleItem) a := sortedItems[i].(utils.SimpleItem)
b := sortedItems[j].(utils.SimpleItem) b := sortedItems[j].(utils.SimpleItem)
@ -317,7 +327,7 @@ func sortItems(items []list.Item, field string, direction string) []list.Item {
} }
case "created_at": case "created_at":
switch direction { switch direction {
case "asc": case sortOrderAsc:
sort.Slice(sortedItems, func(i, j int) bool { sort.Slice(sortedItems, func(i, j int) bool {
a := sortedItems[i].(utils.SimpleItem) a := sortedItems[i].(utils.SimpleItem)
b := sortedItems[j].(utils.SimpleItem) b := sortedItems[j].(utils.SimpleItem)
@ -336,7 +346,7 @@ func sortItems(items []list.Item, field string, direction string) []list.Item {
} }
case "name": case "name":
switch direction { switch direction {
case "asc": case sortOrderAsc:
sort.Slice(sortedItems, func(i, j int) bool { sort.Slice(sortedItems, func(i, j int) bool {
a := sortedItems[i].(utils.SimpleItem) a := sortedItems[i].(utils.SimpleItem)
b := sortedItems[j].(utils.SimpleItem) b := sortedItems[j].(utils.SimpleItem)