Skip to content

Commit

Permalink
Code reorg and feedist coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain committed Feb 22, 2020
1 parent c28fc01 commit e564294
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
29 changes: 13 additions & 16 deletions ui/fee_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"sync"

"github.com/fatih/color"
"github.com/gchaincl/mempool/client"
"github.com/jroimartin/gocui"
)
Expand All @@ -18,6 +19,7 @@ type FeeDistribution struct {
isProjected bool
cancelFn context.CancelFunc
fees client.Fees
title string
}

func NewFeeDistribution(g *gocui.Gui) *FeeDistribution {
Expand Down Expand Up @@ -84,7 +86,7 @@ func (fd *FeeDistribution) Layout(g *gocui.Gui) error {
return err
}

v.Title = "Fee distribution ('esc' to close)"
v.Title = "Fee distribution" + fd.title + "('esc' to close)"
g.SetCurrentView(name)
g.SetViewOnTop(name)
g.SetKeybinding(name, gocui.KeyEsc, gocui.ModNone, fd.close)
Expand All @@ -97,22 +99,17 @@ func (fd *FeeDistribution) Layout(g *gocui.Gui) error {
return nil
}

min, max := 99999, 0
for _, f := range fd.fees {
fee := int(f.FPV)
if fee < min {
min = fee
}
if fee > max {
max = fee
}
}
fmt.Fprintf(v, "Fee span: %d - %d sat/vByte\n", min, max)

fmt.Fprintf(v, "Tx count: %d transactions\n", len(fd.fees))

sort.Sort(fd.fees)
fmt.Fprintf(v, "Median: ~%d sat/vBytes", int(fd.fees[len(fd.fees)/2].FPV))
txs := len(fd.fees)
min, max := fd.fees[0].FPV, fd.fees[txs-1].FPV

var (
white = color.New(color.FgWhite).SprintfFunc()
yellow = color.New(color.FgYellow).SprintfFunc()
)
fmt.Fprintf(v, white("Fee span:")+" %d - %d "+yellow("sat/vByte\n"), ceil(min), ceil(max))
fmt.Fprintf(v, white("Tx count:")+" %d "+white("transactions\n"), txs)
fmt.Fprintf(v, white("Median: ")+" ~%d "+yellow("sat/vBytes\n"), ceil(fd.fees[txs/2].FPV))

return nil
}
Expand Down
27 changes: 0 additions & 27 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ui

import (
"fmt"
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -275,32 +274,6 @@ func (ui *UI) info(g *gocui.Gui, x, y int) error {
return nil
}

func ceil(f float64) int {
return int(
math.Ceil(f),
)
}

func fmtSeconds(s int64) string {
if s < 60 {
return "< 1 minute"
} else if s < 120 {
return fmt.Sprintf("1 minute")
} else if s < 3600 {
return fmt.Sprintf("%d minutes", s/60)
}
return fmt.Sprintf("%d hours", s/3600)
}

func fmtSize(s int) string {
if s < 1024*1024 {
m := float64(s) / 1000.0
return fmt.Sprintf("%dkB", ceil(m))
}
m := float64(s) / (1000.0 * 1000.0)
return fmt.Sprintf("%dMB", ceil(m))
}

func (ui *UI) onBlockClick(g *gocui.Gui, v *gocui.View) error {
name := v.Name()
if strings.HasPrefix(name, "projected-block-") {
Expand Down
32 changes: 32 additions & 0 deletions ui/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ui

import (
"fmt"
"math"
)

func ceil(f float64) int {
return int(
math.Ceil(f),
)
}

func fmtSeconds(s int64) string {
if s < 60 {
return "< 1 minute"
} else if s < 120 {
return fmt.Sprintf("1 minute")
} else if s < 3600 {
return fmt.Sprintf("%d minutes", s/60)
}
return fmt.Sprintf("%d hours", s/3600)
}

func fmtSize(s int) string {
if s < 1024*1024 {
m := float64(s) / 1000.0
return fmt.Sprintf("%dkB", ceil(m))
}
m := float64(s) / (1000.0 * 1000.0)
return fmt.Sprintf("%dMB", ceil(m))
}

0 comments on commit e564294

Please sign in to comment.