From 2b1fb662fbe2a891544cac71ca8d93a105ad8181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Wed, 12 Sep 2018 14:03:07 +0200 Subject: [PATCH] Fix showing data usage indicators --- internal/log/handlers/cli/results.go | 20 +++++++++++++++----- internal/util/util.go | 6 +++++- nettests/nettests.go | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/internal/log/handlers/cli/results.go b/internal/log/handlers/cli/results.go index 4f1a795..4ed7365 100644 --- a/internal/log/handlers/cli/results.go +++ b/internal/log/handlers/cli/results.go @@ -24,6 +24,18 @@ func formatSpeed(speed float64) string { return fmt.Sprintf("%.2f Tbit/s", float32(speed)/(1000*1000*1000)) } +func formatSize(size float64) string { + if size < 1024 { + return fmt.Sprintf("%.1fK", size) + } else if size < 1024*1024 { + return fmt.Sprintf("%.1fM", size/1024.0) + } else if size < 1024*1024*1024 { + return fmt.Sprintf("%.1fG", size/(1024.0*1024.0)) + } + // WTF, you crazy? + return fmt.Sprintf("%.1fT", size/(1024*1024*1024)) +} + var summarizers = map[string]func(uint64, uint64, string) []string{ "websites": func(totalCount uint64, anomalyCount uint64, ss string) []string { return []string{ @@ -102,9 +114,7 @@ func logResultItem(w io.Writer, f log.Fields) error { util.RightPad(summary[2], colWidth))) if index == totalCount-1 { - fmt.Fprintf(w, "└┬──────────────┬──────────────┬──────────────┬") - fmt.Fprintf(w, strings.Repeat("─", colWidth*2-44)) - fmt.Fprintf(w, "┘\n") + fmt.Fprintf(w, "└┬──────────────┬──────────────┬──────────────────┬┘\n") } return nil } @@ -125,8 +135,8 @@ func logResultSummary(w io.Writer, f log.Fields) error { fmt.Fprintf(w, " │ %s │ %s │ %s │\n", util.RightPad(fmt.Sprintf("%d tests", tests), 12), util.RightPad(fmt.Sprintf("%d nets", networks), 12), - util.RightPad(fmt.Sprintf("%.0f ⬆ %.0f ⬇", dataUp, dataDown), 12)) - fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────┘\n") + util.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), 16)) + fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────────┘\n") return nil } diff --git a/internal/util/util.go b/internal/util/util.go index c47d301..7d64ccf 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -38,7 +38,11 @@ func EscapeAwareRuneCountInString(s string) int { } func RightPad(str string, length int) string { - return str + strings.Repeat(" ", length-EscapeAwareRuneCountInString(str)) + c := length - EscapeAwareRuneCountInString(str) + if c < 0 { + c = 0 + } + return str + strings.Repeat(" ", c) } // WrapString wraps the given string within lim width in characters. diff --git a/nettests/nettests.go b/nettests/nettests.go index 89ec036..8da672c 100644 --- a/nettests/nettests.go +++ b/nettests/nettests.go @@ -286,7 +286,7 @@ func (c *Controller) Init(nt *mk.Nettest) error { } c.res.DataUsageDown += e.Value.DownloadedKB - c.res.DataUsageDown += e.Value.UploadedKB + c.res.DataUsageUp += e.Value.UploadedKB }) log.Debugf("Registered all the handlers")