Skip to content

Commit

Permalink
Merge pull request #34 from rkfg/sort
Browse files Browse the repository at this point in the history
Allow sorting by all columns
  • Loading branch information
edouardparis committed Aug 15, 2021
2 parents f96c501 + 651cdfb commit b272758
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
27 changes: 26 additions & 1 deletion ui/models/sort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import "time"
import (
"strings"
"time"
)

type Order int

Expand Down Expand Up @@ -30,6 +33,13 @@ func Int64Sort(a, b int64, o Order) bool {
return a > b
}

func UInt64Sort(a, b uint64, o Order) bool {
if o == Asc {
return a < b
}
return a > b
}

func DateSort(a, b *time.Time, o Order) bool {
if o == Desc {
if a == nil || b == nil {
Expand All @@ -45,3 +55,18 @@ func DateSort(a, b *time.Time, o Order) bool {

return a.Before(*b)
}

func StringSort(a, b string, o Order) bool {
result := strings.Compare(a, b)
if o == Asc {
return result < 0
}
return result > 0
}

func BoolSort(a, b bool, o Order) bool {
if o == Asc {
return !a && b
}
return a && !b
}
31 changes: 29 additions & 2 deletions ui/views/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,25 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
switch columns[i] {
case "STATUS":
channels.columns[i] = channelsColumn{
width: 13,
name: fmt.Sprintf("%-13s", columns[i]),
width: 13,
name: fmt.Sprintf("%-13s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
// status meanings are kinda the opposite of their numerical value
return models.IntSort(-c1.Status, -c2.Status, order)
}
},
display: status,
}
case "ALIAS":
channels.columns[i] = channelsColumn{
width: 25,
name: fmt.Sprintf("%-25s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.StringSort(c1.Node.Alias, c2.Node.Alias, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
var alias string
if c.Node == nil || c.Node.Alias == "" {
Expand Down Expand Up @@ -460,6 +471,12 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 7,
name: fmt.Sprintf("%-7s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
// public > private
return models.BoolSort(!c1.Private, !c2.Private, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
if c.Private {
return color.Red(opts...)("private")
Expand All @@ -471,6 +488,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 19,
name: fmt.Sprintf("%-19s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.UInt64Sort(c1.ID, c2.ID, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
if c.ID == 0 {
return fmt.Sprintf("%-19s", "")
Expand All @@ -482,6 +504,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 14,
name: fmt.Sprintf("%-14s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.UInt64Sort(c1.ID, c2.ID, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
if c.ID == 0 {
return fmt.Sprintf("%-14s", "")
Expand Down

0 comments on commit b272758

Please sign in to comment.