6
6
"github.com/algorandfoundation/hack-tui/ui/style"
7
7
tea "github.com/charmbracelet/bubbletea"
8
8
"github.com/charmbracelet/lipgloss"
9
+ "math"
9
10
"strconv"
10
11
"strings"
11
12
"time"
@@ -44,6 +45,21 @@ func (m StatusViewModel) HandleMessage(msg tea.Msg) (StatusViewModel, tea.Cmd) {
44
45
return m , nil
45
46
}
46
47
48
+ func getBitRate (bytes int ) string {
49
+ txString := fmt .Sprintf ("%d B/s " , bytes )
50
+ if bytes >= 1024 {
51
+ txString = fmt .Sprintf ("%d KB/s " , bytes / (1 << 10 ))
52
+ }
53
+ if bytes >= int (math .Pow (1024 , 2 )) {
54
+ txString = fmt .Sprintf ("%d MB/s " , bytes / (1 << 20 ))
55
+ }
56
+ if bytes >= int (math .Pow (1024 , 3 )) {
57
+ txString = fmt .Sprintf ("%d GB/s " , bytes / (1 << 30 ))
58
+ }
59
+
60
+ return txString
61
+ }
62
+
47
63
// View handles the render cycle
48
64
func (m StatusViewModel ) View () string {
49
65
if ! m .IsVisible {
@@ -70,13 +86,13 @@ func (m StatusViewModel) View() string {
70
86
row1 := lipgloss .JoinHorizontal (lipgloss .Left , beginning , middle , end )
71
87
72
88
beginning = style .Blue .Render (" Round time: " ) + fmt .Sprintf ("%.2fs" , float64 (m .Data .Metrics .RoundTime )/ float64 (time .Second ))
73
- end = fmt . Sprintf ( "%d KB/s " , m .Data .Metrics .TX / 1024 ) + style .Green .Render ("TX " )
89
+ end = getBitRate ( m .Data .Metrics .TX ) + style .Green .Render ("TX " )
74
90
middle = strings .Repeat (" " , max (0 , size - (lipgloss .Width (beginning )+ lipgloss .Width (end )+ 2 )))
75
91
76
92
row2 := lipgloss .JoinHorizontal (lipgloss .Left , beginning , middle , end )
77
93
78
94
beginning = style .Blue .Render (" TPS: " ) + fmt .Sprintf ("%.2f" , m .Data .Metrics .TPS )
79
- end = fmt . Sprintf ( "%d KB/s " , m .Data .Metrics .RX / 1024 ) + style .Green .Render ("RX " )
95
+ end = getBitRate ( m .Data .Metrics .RX ) + style .Green .Render ("RX " )
80
96
middle = strings .Repeat (" " , max (0 , size - (lipgloss .Width (beginning )+ lipgloss .Width (end )+ 2 )))
81
97
82
98
row3 := lipgloss .JoinHorizontal (lipgloss .Left , beginning , middle , end )
0 commit comments