Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - duration string var type #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"strings"
"sync"
"time"

"github.com/antonholmquist/jason"
)
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 3 additions & 1 deletion ui_multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions var.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
KindMemory
KindDuration
KindString
KindDurationString
)

// ToSlice converts "dot-separated" notation into the "slice of strings".
Expand Down Expand Up @@ -76,6 +77,8 @@ func (v VarName) Kind() VarKind {
return KindDuration
case "str":
return KindString
case "durationstr":
return KindDurationString
}
return KindDefault
}
Expand All @@ -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 {
Expand Down