-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdisplay.go
More file actions
85 lines (73 loc) · 2.17 KB
/
display.go
File metadata and controls
85 lines (73 loc) · 2.17 KB
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
package main
import (
"fmt"
"strings"
"time"
"github.com/pterm/pterm"
)
type Display struct {
pwh *WrapperHolder
noheader bool
area *pterm.AreaPrinter
host_format_string string
longest_host_string int
}
func NewDisplay(pwh *WrapperHolder) *Display {
return &Display{
pwh: pwh,
}
}
func (d *Display) SetNoHeader(v bool) {
d.noheader = v
}
func (d *Display) Start() {
d.area, _ = pterm.DefaultArea.Start()
d.longest_host_string = 0
for _, wrapper := range d.pwh.ping_wrappers {
if len(wrapper.Host()) > d.longest_host_string {
d.longest_host_string = len(wrapper.Host())
}
}
d.host_format_string = "%-" + fmt.Sprintf("%v", d.longest_host_string+2) + "s"
}
func (d *Display) Stop() {
d.area.Stop()
}
func (d *Display) Update() {
var sb strings.Builder
if !d.noheader {
sb.WriteString(VersionString())
}
for _, wrapper := range d.pwh.ping_wrappers {
sb.WriteString(fmt.Sprintf(d.host_format_string, wrapper.Host()))
stats := wrapper.CalcStats(2 * 1e9)
if stats.error_message != "" {
sb.WriteString(bold_red.Sprintf("❌ %v", stats.error_message))
} else if stats.last_seen_nano > 2*1e9 {
if stats.lastrecv == 0 {
sb.WriteString(bold_red.Sprintf("❌ never had reply"))
} else {
sb.WriteString(bold_red.Sprintf("❌ last reply %s ago", time.Duration(stats.last_seen_nano).Round(time.Second)))
}
} else {
sb.WriteString(bold_green.Sprintf("✅ %-8s", stats.lastrtt_as_string))
if stats.last_loss_nano > 0 {
last_log := fmt.Sprintf(
" (last loss %s: %s ago for %s)",
time.Unix(0, stats.last_loss_nano).Format("2006-01-02 15:04:05"),
time.Duration(time.Now().UnixNano()-stats.last_loss_nano).Round(time.Second),
time.Duration(stats.last_loss_duration).Round(time.Second/10),
)
if d.longest_host_string+12+len(last_log) >= pterm.GetTerminalWidth() {
sb.WriteString(fmt.Sprintf("\n%"+fmt.Sprintf("%v", pterm.GetTerminalWidth())+"s", last_log))
} else {
sb.WriteString(last_log)
}
}
}
sb.WriteString("\n")
}
d.area.Update(sb.String())
}
var bold_red = pterm.NewStyle(pterm.FgRed, pterm.Bold)
var bold_green = pterm.NewStyle(pterm.FgGreen, pterm.Bold)