Skip to content

Commit 01bfbd9

Browse files
committed
better numbers and name display in table
1 parent 973e069 commit 01bfbd9

File tree

7 files changed

+85
-11
lines changed

7 files changed

+85
-11
lines changed

config/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"embed"
55
"fmt"
6+
"log"
67
"math/rand"
78
"time"
89
)
@@ -66,6 +67,7 @@ func GetChainConfig(
6667
// ReadSpecs will read network specification from the embedded file
6768
func (cg *ChainConfig) ReadSpecs() ([]byte, error) {
6869
if cg.NetworkSpecsVersion >= 1500 {
70+
log.Printf("Loading decoder for spec 1500")
6971
return networkSpecs.ReadFile(fmt.Sprintf("specs/%v.1500.json", cg.NetworkSpecs))
7072
}
7173
return networkSpecs.ReadFile(fmt.Sprintf("specs/%v.json", cg.NetworkSpecs))

internal/client/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ func NewClientWithExternalCache(cfg config.ChainConfig, cache *mcache.CacheDrive
9999
}
100100

101101
func (c *Client) registerDecoder(cfg config.ChainConfig) error {
102-
log.Printf("Loading decoder %v.%v", cfg.NetworkSpecs, cfg.NetworkSpecsVersion)
103-
// Decoder metadata
104102
var hexMetadata string
105103
err := client.CallWithBlockHash(c.api.Client, &hexMetadata, "state_getMetadata", nil)
106104
if err != nil {

internal/display/table.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/jedib0t/go-pretty/v6/text"
77
"github.com/zooper-corp/mooncli/config"
88
"github.com/zooper-corp/mooncli/internal/client"
9-
"math"
9+
"github.com/zooper-corp/mooncli/internal/tools"
1010
"os"
1111
)
1212

@@ -78,21 +78,19 @@ func DumpTable(data client.CollatorPool, client *client.Client, options config.T
7878
revokeRound := data.RoundNumber + options.RevokeRounds
7979
for _, info := range data.Collators {
8080
t.AppendRow(table.Row{
81-
info.DisplayName(),
81+
tools.ToAscii(info.DisplayName()),
8282
info.Rank,
8383
info.Selected,
8484
// Counted
85-
fmt.Sprintf("%vK", math.Round(info.Counted.Float64()/1000)),
85+
tools.Humanize(info.Counted.Float64()),
8686
// Blocks
8787
fmt.Sprintf("%v", info.History[data.RoundNumber].Blocks),
8888
fmt.Sprintf("%.1f", info.AverageBlocks()),
8989
// Balance
90-
fmt.Sprintf("%v", math.Round(info.Balance.Free.Float64())),
90+
tools.Humanize(info.Balance.Free.Float64()),
9191
// Revokes
92-
fmt.Sprintf("%vK", math.Round(info.RevokeAt(revokeRound).Counted.Float64()/1000)),
93-
fmt.Sprintf("%vK", math.Round(
94-
(info.Counted.Float64()-info.RevokeAt(revokeRound).Counted.Float64())/1000),
95-
),
92+
tools.Humanize(info.RevokeAt(revokeRound).Counted.Float64()),
93+
tools.Humanize(info.Counted.Float64() - info.RevokeAt(revokeRound).Counted.Float64()),
9694
fmt.Sprintf("%v", info.Revokes[revokeRound].Rank),
9795
})
9896
}

internal/tools/math.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package tools
22

3-
import "golang.org/x/exp/constraints"
3+
import (
4+
"fmt"
5+
"golang.org/x/exp/constraints"
6+
"math"
7+
)
48

59
func Min[T constraints.Ordered](a, b T) T {
610
if a < b {
711
return a
812
}
913
return b
1014
}
15+
16+
func Humanize(v float64) string {
17+
switch {
18+
case v > math.Pow10(6):
19+
return fmt.Sprintf("%.1fM", v/math.Pow10(6))
20+
case v > math.Pow10(3):
21+
return fmt.Sprintf("%.1fK", v/math.Pow10(3))
22+
case v > math.Pow10(2):
23+
return fmt.Sprintf("%.0f", v/math.Pow10(3))
24+
case v == math.Round(v):
25+
return fmt.Sprintf("%.0f", v)
26+
case v < 1:
27+
return fmt.Sprintf("%.3f", v)
28+
default:
29+
return fmt.Sprintf("%.1f", v)
30+
}
31+
}

internal/tools/math_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tools
2+
3+
import "testing"
4+
5+
func TestMin(t *testing.T) {
6+
a := Min(123, 13)
7+
if a != 13 {
8+
t.Errorf("Expected '13' got '%v'", a)
9+
}
10+
}
11+
12+
func TestHumanize(t *testing.T) {
13+
h := Humanize(123416789.123)
14+
if h != "123.4M" {
15+
t.Errorf("Expected '123.4M' got '%v'", h)
16+
}
17+
h = Humanize(123416.123)
18+
if h != "123.4K" {
19+
t.Errorf("Expected '123.4K' got '%v'", h)
20+
}
21+
h = Humanize(123.123)
22+
if h != "123.1" {
23+
t.Errorf("Expected '123.1' got '%v'", h)
24+
}
25+
h = Humanize(0.123)
26+
if h != "0.123" {
27+
t.Errorf("Expected '0.123' got '%v'", h)
28+
}
29+
}

internal/tools/text.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tools
2+
3+
import (
4+
"strings"
5+
"unicode"
6+
)
7+
8+
func ToAscii(s string) string {
9+
t := strings.Map(func(r rune) rune {
10+
if r > unicode.MaxASCII {
11+
return -1
12+
}
13+
return r
14+
}, s)
15+
return t
16+
}

internal/tools/text_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tools
2+
3+
import "testing"
4+
5+
func TestToAscii(t *testing.T) {
6+
a := ToAscii("foo👾bar")
7+
if a != "foobar" {
8+
t.Errorf("Expecting 'foobar' got '%v'", a)
9+
}
10+
}

0 commit comments

Comments
 (0)