This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathui.go
114 lines (94 loc) · 2.28 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
package main
import (
"fmt"
"strings"
human "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
net "gx/ipfs/QmXfkENeeBvh3zYA51MaSdGUdBjhQ99cP5WQe8zgr6wchG/go-libp2p-net"
)
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)
}
func padPrint(line int, label, value string) {
putMessage(line, fmt.Sprintf("%s%s%s", label, strings.Repeat(" ", 20-len(label)), value))
}
func putMessage(line int, mes string) {
fmt.Printf("\033[%d;0H%s%s", line, QClrLine, mes)
}
func printDataSharedLine(line int, bup uint64, 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 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) {}