2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-04 06:00:14 +00:00

Merge pull request #34 from rkfg/sort

Allow sorting by all columns
This commit is contained in:
Edouard 2021-08-15 11:51:43 +02:00 committed by GitHub
commit b272758f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package models
import "time"
import (
"strings"
"time"
)
type Order int
@ -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 {
@ -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
}

View File

@ -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 == "" {
@ -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")
@ -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", "")
@ -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", "")