feat transactions sort

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

@ -206,6 +206,8 @@ func (c *controller) Order(order models.Order) func(*gocui.Gui, *gocui.View) err
switch view.Name() {
case views.CHANNELS:
c.views.Channels.Sort("", order)
case views.TRANSACTIONS:
c.views.Transactions.Sort("", order)
}
return nil
}

@ -16,6 +16,13 @@ func IntSort(a, b int, o Order) bool {
return a < b
}
func Int32Sort(a, b int32, 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

@ -2,6 +2,8 @@ package models
import (
"context"
"sort"
"sync"
"github.com/edouardparis/lntop/network/models"
)
@ -12,9 +14,10 @@ type Transactions struct {
current *models.Transaction
list []*models.Transaction
sort TransactionsSort
mu sync.RWMutex
}
func (t Transactions) Current() *models.Transaction {
func (t *Transactions) Current() *models.Transaction {
return t.current
}
@ -22,7 +25,7 @@ func (t *Transactions) SetCurrent(index int) {
t.current = t.Get(index)
}
func (t Transactions) List() []*models.Transaction {
func (t *Transactions) List() []*models.Transaction {
return t.list
}
@ -38,8 +41,12 @@ func (t *Transactions) Less(i, j int) bool {
return t.sort(t.list[i], t.list[j])
}
func (t *Transactions) WithSort(s TransactionsSort) {
func (t *Transactions) Sort(s TransactionsSort) {
if s == nil {
return
}
t.sort = s
sort.Sort(t)
}
func (t *Transactions) Get(index int) *models.Transaction {
@ -50,11 +57,40 @@ func (t *Transactions) Get(index int) *models.Transaction {
return t.list[index]
}
func (t *Transactions) Contains(tx *models.Transaction) bool {
if tx == nil {
return false
}
for i := range t.list {
if t.list[i].TxHash == tx.TxHash {
return true
}
}
return false
}
func (t *Transactions) Add(tx *models.Transaction) {
t.mu.Lock()
defer t.mu.Unlock()
if t.Contains(tx) {
return
}
t.list = append(t.list, tx)
}
func (m *Models) RefreshTransactions(ctx context.Context) error {
transactions, err := m.network.GetTransactions(ctx)
if err != nil {
return err
}
*m.Transactions = Transactions{list: transactions}
for i := range transactions {
m.Transactions.Add(transactions[i])
}
if m.Transactions.sort != nil {
sort.Sort(m.Transactions)
}
return nil
}

@ -44,6 +44,8 @@ type Transactions struct {
type transactionsColumn struct {
name string
width int
sorted bool
sort func(models.Order) models.TransactionsSort
display func(*netmodels.Transaction, ...color.Option) string
}
@ -126,6 +128,21 @@ func (c *Transactions) Speed() (int, int, int, int) {
1, 1
}
func (c *Transactions) Sort(column string, order models.Order) {
if column == "" {
index := c.currentColumnIndex()
col := c.columns[index]
if col.sort == nil {
return
}
c.transactions.Sort(col.sort(order))
for i := range c.columns {
c.columns[i].sorted = (i == index)
}
}
}
func (c Transactions) Delete(g *gocui.Gui) error {
err := g.DeleteView(TRANSACTIONS_COLUMNS)
if err != nil {
@ -211,6 +228,10 @@ func (c *Transactions) 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(" ")
@ -253,6 +274,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%-15s", columns[i]),
width: 15,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.DateSort(&tx1.Date, &tx2.Date, order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
return color.Cyan(opts...)(
fmt.Sprintf("%15s", tx.Date.Format("15:04:05 Jan _2")),
@ -263,6 +289,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%8s", columns[i]),
width: 8,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.Int32Sort(tx1.BlockHeight, tx2.BlockHeight, order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
return color.White(opts...)(fmt.Sprintf("%8d", tx.BlockHeight))
},
@ -271,6 +302,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%10s", columns[i]),
width: 10,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.IntSort(len(tx1.DestAddresses), len(tx2.DestAddresses), order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
return color.White(opts...)(fmt.Sprintf("%10d", len(tx.DestAddresses)))
},
@ -279,6 +315,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%8s", columns[i]),
width: 8,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.Int64Sort(tx1.TotalFees, tx2.TotalFees, order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
return color.White(opts...)(fmt.Sprintf("%8d", tx.TotalFees))
},
@ -287,6 +328,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%8s", columns[i]),
width: 8,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.Int32Sort(tx1.NumConfirmations, tx2.NumConfirmations, order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
n := fmt.Sprintf("%8d", tx.NumConfirmations)
if tx.NumConfirmations < 6 {
@ -314,6 +360,11 @@ func NewTransactions(cfg *config.View, txs *models.Transactions) *Transactions {
transactions.columns[i] = transactionsColumn{
name: fmt.Sprintf("%13s", columns[i]),
width: 13,
sort: func(order models.Order) models.TransactionsSort {
return func(tx1, tx2 *netmodels.Transaction) bool {
return models.Int64Sort(tx1.Amount, tx2.Amount, order)
}
},
display: func(tx *netmodels.Transaction, opts ...color.Option) string {
return color.White(opts...)(printer.Sprintf("%13d", tx.Amount))
},

Loading…
Cancel
Save