fix channels sort

pull/10/head
Edouard Paris 5 years ago
parent e7f0ed7c8d
commit 8d6e40f63c

@ -1,5 +1,7 @@
package models
import "time"
type Order int
const (
@ -7,9 +9,32 @@ const (
Desc
)
func IntSort(a, b int, o Order) bool {
if o == Asc {
return a > b
}
return a < b
}
func Int64Sort(a, b int64, o Order) bool {
if o == Asc {
return a > b
}
return a < b
}
func DateSort(a, b *time.Time, o Order) bool {
if o == Asc {
if a == nil || b == nil {
return b == nil
}
return a.After(*b)
}
if a == nil || b == nil {
return a == nil
}
return a.Before(*b)
}

@ -302,6 +302,14 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 21,
name: fmt.Sprintf("%-21s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.Int64Sort(
c1.LocalBalance*100/c1.Capacity,
c2.LocalBalance*100/c2.Capacity,
order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
index := int(c.LocalBalance * int64(15) / c.Capacity)
var buffer bytes.Buffer
@ -337,6 +345,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 12,
name: fmt.Sprintf("%12s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.Int64Sort(c1.Capacity, c2.Capacity, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
return color.White(opts...)(printer.Sprintf("%12d", c.Capacity))
},
@ -345,6 +358,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 5,
name: fmt.Sprintf("%5s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.IntSort(len(c1.PendingHTLC), len(c2.PendingHTLC), order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
return color.Yellow(opts...)(fmt.Sprintf("%5d", len(c.PendingHTLC)))
},
@ -353,6 +371,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 10,
name: fmt.Sprintf("%-10s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.Int64Sort(c1.UnsettledBalance, c2.UnsettledBalance, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
return color.Yellow(opts...)(printer.Sprintf("%10d", c.UnsettledBalance))
},
@ -361,6 +384,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 6,
name: fmt.Sprintf("%-6s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.Int64Sort(c1.CommitFee, c2.CommitFee, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
return color.White(opts...)(printer.Sprintf("%6d", c.CommitFee))
},
@ -369,6 +397,11 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 15,
name: fmt.Sprintf("%-15s", columns[i]),
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.DateSort(c1.LastUpdate, c2.LastUpdate, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
if c.LastUpdate != nil {
return color.Cyan(opts...)(

Loading…
Cancel
Save