mirror of
https://github.com/mickael-menu/zk
synced 2024-11-15 12:12:56 +00:00
Enable overriding sort criteria of config aliases with --sort flags
This commit is contained in:
parent
c10449a425
commit
a5b7639bcd
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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"})
|
||||||
|
Loading…
Reference in New Issue
Block a user