This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ui.go
162 lines (134 loc) · 2.91 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"strings"
human "github.com/dustin/go-humanize"
net "github.com/libp2p/go-libp2p-core/network"
ma "github.com/multiformats/go-multiaddr"
)
const (
QClrLine = "\033[K"
QReset = "\033[2J"
)
/*
Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
*/
const (
Clear = 0
)
const (
Black = 30 + iota
Red
Green
Yellow
Blue
Magenta
Cyan
LightGray
)
const (
LightBlue = 94
)
func color(color int, s string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color, s)
}
const width = 25
func padPrint(line int, label, value string) {
putMessage(line, fmt.Sprintf("%s%s%s", label, strings.Repeat(" ", width-len(label)), value))
}
func putMessage(line int, mes string) {
fmt.Printf("\033[%d;0H%s%s", line, QClrLine, mes)
}
func printDataSharedLine(line int, bup int, totup int64, rateup float64) {
pad := " "
a := fmt.Sprintf("%d ", bup)[:12]
b := (human.Bytes(uint64(totup)) + pad)[:12]
c := (human.Bytes(uint64(rateup)) + "/s" + pad)[:12]
padPrint(line, "", a+b+c)
}
type GooeyApp struct {
Title string
DataFields []*DataLine
Log *Log
}
func (a *GooeyApp) Print() {
fmt.Println(QReset)
putMessage(1, a.Title)
for _, dl := range a.DataFields {
dl.Print()
}
a.Log.Print()
}
func (a *GooeyApp) NewDataLine(line int, label, defval string) *DataLine {
dl := &DataLine{
Default: defval,
Label: label,
Line: line,
}
a.DataFields = append(a.DataFields, dl)
return dl
}
type DataLine struct {
Label string
Line int
Default string
LastVal string
}
func (dl *DataLine) SetVal(s string) {
dl.LastVal = s
dl.Print()
}
func (dl *DataLine) Print() {
s := dl.Default
if dl.LastVal != "" {
s = dl.LastVal
}
padPrint(dl.Line, dl.Label, s)
}
type Log struct {
Size int
StartLine int
Messages []string
beg int
end int
}
func NewLog(line, size int) *Log {
return &Log{
Size: size,
StartLine: line,
Messages: make([]string, size),
end: -1,
}
}
func (l *Log) Add(m string) {
l.end = (l.end + 1) % l.Size
if l.Messages[l.end] != "" {
l.beg++
}
l.Messages[l.end] = m
}
func (l *Log) Print() {
for i := 0; i < l.Size; i++ {
putMessage(l.StartLine+i, l.Messages[(l.beg+i)%l.Size])
}
}
type LogNotifee struct {
addMes chan<- string
}
func (ln *LogNotifee) Listen(net.Network, ma.Multiaddr) {}
func (ln *LogNotifee) ListenClose(net.Network, ma.Multiaddr) {}
func (ln *LogNotifee) Connected(_ net.Network, c net.Conn) {
ln.addMes <- fmt.Sprintf("New connection from %s", c.RemotePeer().Pretty())
}
func (ln *LogNotifee) Disconnected(_ net.Network, c net.Conn) {
ln.addMes <- fmt.Sprintf("Lost connection to %s", c.RemotePeer().Pretty())
}
func (ln *LogNotifee) OpenedStream(net.Network, net.Stream) {}
func (ln *LogNotifee) ClosedStream(net.Network, net.Stream) {}