-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
50 lines (42 loc) · 1.32 KB
/
debug.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
package cli
import (
"time"
"github.com/gosuri/uitable"
"github.com/i582/cfmt"
)
// DebugData for showing on terminal
type DebugData struct {
Method string
Path string
Duration time.Duration
BodySize int
Code int
CodeStatus string
}
// WriteDebug to showing on terminal
func WriteDebug(d *DebugData) {
table := uitable.New()
table.MaxColWidth = 50
table.AddRow(
cfmt.Sprintf("{{%s}}::bold", "METHOD"),
cfmt.Sprintf("{{%s}}::bold", "PATH"),
cfmt.Sprintf("{{%s}}::bold", "SIZE"),
cfmt.Sprintf("{{%s}}::bold", "DURATION"),
cfmt.Sprintf("{{%s}}::bold", "StatusCode"))
method := cfmt.Sprintf("{{%s}}::green|bold", d.Method)
size := cfmt.Sprintf("{{%d B}}::bold", d.BodySize)
durationCalculate := float32(d.Duration / time.Millisecond)
duration := cfmt.Sprintf("{{%.2fms}}::green|bold", durationCalculate)
if durationCalculate >= 200 && durationCalculate <= 1000 {
duration = cfmt.Sprintf("{{%.2fms}}::red|bold", float32(d.Duration/time.Millisecond))
}
if durationCalculate >= 1000 {
duration = cfmt.Sprintf("{{%.2fs}}::red|bold", float32(d.Duration/time.Second))
}
code := cfmt.Sprintf("{{%d %s}}::green|bold", d.Code, d.CodeStatus)
if d.Code >= 300 {
code = cfmt.Sprintf("{{%d %s}}::red|bold", d.Code, d.CodeStatus)
}
table.AddRow(method, d.Path, size, duration, code)
cfmt.Println(table)
}