diff --git a/stack.go b/stack.go index 1b471de..dd1898b 100644 --- a/stack.go +++ b/stack.go @@ -8,8 +8,8 @@ const DefaultSize = 1200 // Stack is a limited FIFO for holding sparkline values. type Stack struct { - Values []int - Len int + values []int + len int } // NewStack inits new Stack with default size limit. @@ -20,26 +20,26 @@ func NewStack() *Stack { // NewStackWithSize inits new Stack with size limit. func NewStackWithSize(size int) *Stack { return &Stack{ - Values: make([]int, size), - Len: size, + values: make([]int, size), + len: size, } } // Push inserts data to stack, preserving constant length. func (s *Stack) Push(v IntVar) { val := v.Value() - s.Values = append(s.Values, val) - if len(s.Values) > s.Len { + s.values = append(s.values, val) + if len(s.values) > s.len { // TODO: check if underlying array is growing constantly - s.Values = s.Values[1:] + s.values = s.values[1:] } } -// IntValues returns stack values explicitly casted to int. +// Values returns stack values explicitly casted to int. // // Main case is to use with termui.Sparklines. -func (s *Stack) IntValues() []int { - return s.Values +func (s *Stack) Values() []int { + return s.values } // TODO: implement trim and resize diff --git a/stack_test.go b/stack_test.go index 7c773cb..e15a547 100644 --- a/stack_test.go +++ b/stack_test.go @@ -2,27 +2,13 @@ package main import "testing" -func TestPushWithFloatAndIntValue(t *testing.T) { - s := NewStack() - s.Push(Var(int64(0.0))) // from service.go:guessValue - s.Push(Var(5.0)) - s.Push(Var(float64(15.0))) - if _, ok := s.Max.(float64); !ok { - t.Fatalf("Expected Max to be float64, but it's not") - } - s.Push(Var(int64(25.0))) - if _, ok := s.Max.(int64); !ok { - t.Fatalf("Expected Max to be int64, but it's not") - } -} - func TestStack(t *testing.T) { size := 10 s := NewStackWithSize(size) for i := 0; i < size+5; i++ { - s.Push(i) - l := len(s.Values) + s.Push(&Number{float64(i)}) + l := len(s.values) if l < size { if l != i+1 { @@ -35,33 +21,8 @@ func TestStack(t *testing.T) { } } - if s.Front().(int) != 14 { - t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, s.Front()) - } - - s1 := NewStackWithSize(3) - s1.Push(true) - s1.Push(false) - s1.Push(true) - - ints1 := s1.IntValues() - if len(ints1) != 3 { - t.Fatalf("expecting len of to be %d, but got %d", 3, len(ints1)) - } - if ints1[0] != 1 || ints1[1] != 0 || ints1[2] != 1 { - t.Fatalf("bool values converted to int incorrectly: %v", ints1) - } - - s2 := NewStackWithSize(3) - s2.Push(0.1) - s2.Push(0.5) - s2.Push(0.03) - - ints2 := s2.IntValues() - if len(ints2) != 3 { - t.Fatalf("expecting len to be %d, but got %d", 3, len(ints2)) - } - if ints2[0] != 10 || ints2[1] != 50 || ints2[2] != 3 { - t.Fatalf("float values converted to int incorrectly: %v", ints2) + got := s.Values()[9] + if got != 14 { + t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, got) } } diff --git a/ui_multi.go b/ui_multi.go index 81b8230..7b56eea 100644 --- a/ui_multi.go +++ b/ui_multi.go @@ -120,7 +120,7 @@ func (t *TermUI) Update(data UIData) { data.Stats[name].Update(v) max := data.Stats[name].Max().String() t.Sparkline1.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max) - t.Sparkline1.Lines[i].Data = data.Stacks[name].IntValues() + t.Sparkline1.Lines[i].Data = data.Stacks[name].Values() } if len(data.Vars) == 1 { @@ -134,7 +134,7 @@ func (t *TermUI) Update(data UIData) { data.Stats[name].Update(v) max := data.Stats[name].Max().String() t.Sparkline2.Lines[i].Title = fmt.Sprintf("%s (max: %s)", service.Name, max) - t.Sparkline2.Lines[i].Data = data.Stacks[name].IntValues() + t.Sparkline2.Lines[i].Data = data.Stacks[name].Values() } } diff --git a/ui_single.go b/ui_single.go index 1eb8c93..615a1c2 100644 --- a/ui_single.go +++ b/ui_single.go @@ -106,7 +106,7 @@ func (t *TermUISingle) Update(data UIData) { spl.TitleColor = colorByKind(name.Kind()) spl.LineColor = colorByKind(name.Kind()) - spl.Data = data.Stacks[name].IntValues() + spl.Data = data.Stacks[name].Values() } t.Relayout() diff --git a/var.go b/var.go index cf8c788..f69bb29 100644 --- a/var.go +++ b/var.go @@ -34,8 +34,10 @@ type IntVar interface { Value() int } +// Number is a type for numeric values, obtained from JSON. +// In JSON it's always float64, so there is no straightforward way +// to separate float from int, so let's keep everything as float. type Number struct { - // TODO: add mutex here or level above, in service? val float64 } @@ -58,6 +60,7 @@ func (v *Number) Value() int { return int(v.val) } +// Memory represents memory information in bytes. type Memory struct { bytes int64 } @@ -80,6 +83,7 @@ func (v *Memory) Value() int { return int(v.bytes) } +// Duration represents duration data (in ns) type Duration struct { dur time.Duration } @@ -105,6 +109,7 @@ func (v *Duration) Value() int { return int(v.dur) } +// Strings represents string data. type String struct { str string }