Enable overriding sort criteria of config aliases with --sort flags

This commit is contained in:
Mickaël Menu 2021-02-27 20:24:12 +01:00
parent c10449a425
commit a5b7639bcd
No known key found for this signature in database
GPG Key ID: 53D73664CD359895
2 changed files with 10 additions and 4 deletions

View File

@ -166,8 +166,11 @@ func SorterFromString(str string) (Sorter, error) {
// SortersFromStrings returns a list of Sorter from their string representation. // SortersFromStrings returns a list of Sorter from their string representation.
func SortersFromStrings(strs []string) ([]Sorter, error) { func SortersFromStrings(strs []string) ([]Sorter, error) {
sorters := make([]Sorter, 0) sorters := make([]Sorter, 0)
for _, str := range strs {
sorter, err := SorterFromString(str) // Iterates in reverse order to be able to override sort criteria set in a
// config alias with a `--sort` flag.
for i := len(strs) - 1; i >= 0; i-- {
sorter, err := SorterFromString(strs[i])
if err != nil { if err != nil {
return sorters, err return sorters, err
} }

View File

@ -56,9 +56,12 @@ func TestSortersFromStrings(t *testing.T) {
{Field: SortCreated, Ascending: false}, {Field: SortCreated, Ascending: false},
}) })
test([]string{"c+", "title"}, []Sorter{ // It is parsed in reverse order to be able to override sort criteria set
{Field: SortCreated, Ascending: true}, // in aliases.
test([]string{"c+", "title", "random"}, []Sorter{
{Field: SortRandom, Ascending: true},
{Field: SortTitle, Ascending: true}, {Field: SortTitle, Ascending: true},
{Field: SortCreated, Ascending: true},
}) })
_, err := SortersFromStrings([]string{"c", "foobar"}) _, err := SortersFromStrings([]string{"c", "foobar"})