From 6babdcb807a70295fabd16cf3a3393224ac94994 Mon Sep 17 00:00:00 2001 From: Carey Klenetsky Date: Thu, 13 Dec 2018 10:56:33 -0800 Subject: [PATCH] Feature - duration string var type Added new KindDurationString and updated Kind() and Format(). Gave KindDurationString the same color as KindDuration. In Update, if we have a KindDurationString, try to parse it to an int64. --- service.go | 10 ++++++++++ ui_multi.go | 4 +++- var.go | 15 +++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/service.go b/service.go index b0c6177..cc5ca1b 100644 --- a/service.go +++ b/service.go @@ -4,6 +4,7 @@ import ( "net/url" "strings" "sync" + "time" "github.com/antonholmquist/jason" ) @@ -86,6 +87,15 @@ func (s *Service) Update(wg *sync.WaitGroup) { } v := guessValue(value) if v != nil { + // Special case: if we have a duration string, guessValue + // will return a string. We should try to parse it and + // push the resulting int64 instead + if name.Kind() == KindDurationString { + if d, err := time.ParseDuration(v.(string)); err == nil { + stack.Push(int64(d)) + continue + } + } stack.Push(v) } } diff --git a/ui_multi.go b/ui_multi.go index 2c7f920..36525f9 100644 --- a/ui_multi.go +++ b/ui_multi.go @@ -4,7 +4,7 @@ import ( "fmt" "time" - "gopkg.in/gizak/termui.v1" + termui "gopkg.in/gizak/termui.v1" ) // TermUI is a termUI implementation of UI interface. @@ -225,6 +225,8 @@ func colorByKind(kind VarKind) termui.Attribute { return termui.ColorRed | termui.AttrBold case KindDuration: return termui.ColorYellow | termui.AttrBold + case KindDurationString: + return termui.ColorYellow | termui.AttrBold case KindString: return termui.ColorGreen | termui.AttrBold default: diff --git a/var.go b/var.go index 8dff7ed..12b1fac 100644 --- a/var.go +++ b/var.go @@ -27,6 +27,7 @@ const ( KindMemory KindDuration KindString + KindDurationString ) // ToSlice converts "dot-separated" notation into the "slice of strings". @@ -76,6 +77,8 @@ func (v VarName) Kind() VarKind { return KindDuration case "str": return KindString + case "durationstr": + return KindDurationString } return KindDefault } @@ -84,15 +87,15 @@ func (v VarName) Kind() VarKind { func Format(v VarValue, kind VarKind) string { switch kind { case KindMemory: - if _, ok := v.(int64); !ok { - break + if i, ok := v.(int64); ok { + return fmt.Sprintf("%s", byten.Size(i)) } - return fmt.Sprintf("%s", byten.Size(v.(int64))) + case KindDurationString: + fallthrough case KindDuration: - if _, ok := v.(int64); !ok { - break + if i, ok := v.(int64); ok { + return fmt.Sprintf("%s", roundDuration(time.Duration(i))) } - return fmt.Sprintf("%s", roundDuration(time.Duration(v.(int64)))) } if f, ok := v.(float64); ok {