feat order

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

@ -9,6 +9,7 @@ var (
yellowBold = color.New(color.FgYellow, color.Bold).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
greenBold = color.New(color.FgGreen, color.Bold).SprintFunc()
greenBg = color.New(color.FgBlack, color.BgGreen).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
redBold = color.New(color.FgRed, color.Bold).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
@ -54,6 +55,11 @@ func Green(opts ...Option) func(a ...interface{}) string {
if options.bold {
return greenBold
}
if options.bg {
return greenBg
}
return green
}

@ -197,6 +197,20 @@ func (c *controller) Menu(g *gocui.Gui, v *gocui.View) error {
return nil
}
func (c *controller) Order(order models.Order) func(*gocui.Gui, *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
view := c.views.Get(v)
if view == nil {
return nil
}
switch view.Name() {
case views.CHANNELS:
c.views.Channels.Sort("", order)
}
return nil
}
}
func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error {
maxX, maxY := g.Size()
view := c.views.Get(v)

@ -1,6 +1,7 @@
package ui
import (
"github.com/edouardparis/lntop/ui/models"
"github.com/jroimartin/gocui"
)
@ -69,5 +70,15 @@ func setKeyBinding(c *controller, g *gocui.Gui) error {
return err
}
err = g.SetKeybinding("", 'a', gocui.ModNone, c.Order(models.Asc))
if err != nil {
return err
}
err = g.SetKeybinding("", 'd', gocui.ModNone, c.Order(models.Desc))
if err != nil {
return err
}
return nil
}

@ -33,8 +33,12 @@ func (c *Channels) Less(i, j int) bool {
return c.sort(c.list[i], c.list[j])
}
func (c *Channels) WithSort(s ChannelsSort) {
func (c *Channels) Sort(s ChannelsSort) {
if s == nil {
return
}
c.sort = s
sort.Sort(c)
}
func (c *Channels) Current() *models.Channel {

@ -50,7 +50,8 @@ type Channels struct {
type channelsColumn struct {
name string
width int
sort func(*netmodels.Channel, *netmodels.Channel, models.Order) bool
sorted bool
sort func(models.Order) models.ChannelsSort
display func(*netmodels.Channel, ...color.Option) string
}
@ -77,6 +78,21 @@ func (c Channels) currentColumnIndex() int {
return index
}
func (c Channels) Sort(column string, order models.Order) {
if column == "" {
index := c.currentColumnIndex()
col := c.columns[index]
if col.sort == nil {
return
}
c.channels.Sort(col.sort(order))
for i := range c.columns {
c.columns[i].sorted = (i == index)
}
}
}
func (c Channels) Origin() (int, int) {
return c.ox, c.oy
}
@ -218,6 +234,10 @@ func (c *Channels) display() {
buffer.WriteString(color.Cyan(color.Background)(c.columns[i].name))
buffer.WriteString(" ")
continue
} else if c.columns[i].sorted {
buffer.WriteString(color.Black(color.Background)(c.columns[i].name))
buffer.WriteString(" ")
continue
}
buffer.WriteString(c.columns[i].name)
buffer.WriteString(" ")
@ -304,8 +324,10 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
channels.columns[i] = channelsColumn{
width: 12,
name: fmt.Sprintf("%12s", columns[i]),
sort: func(c1, c2 *netmodels.Channel, order models.Order) bool {
return models.Int64Sort(c1.LocalBalance, c2.LocalBalance, order)
sort: func(order models.Order) models.ChannelsSort {
return func(c1, c2 *netmodels.Channel) bool {
return models.Int64Sort(c1.LocalBalance, c2.LocalBalance, order)
}
},
display: func(c *netmodels.Channel, opts ...color.Option) string {
return color.Cyan(opts...)(printer.Sprintf("%12d", c.LocalBalance))

Loading…
Cancel
Save