2015-04-21 21:33:04 +08:00
|
|
|
package main
|
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
// DefaultSize specifies maximum number of items in stack.
|
|
|
|
//
|
|
|
|
// Values should be enough for sparklines on high-res terminals
|
|
|
|
// with minimal font size.
|
|
|
|
const DefaultSize = 1200
|
|
|
|
|
2015-04-25 14:06:45 +08:00
|
|
|
// Stack is a limited FIFO for holding sparkline values.
|
2015-04-21 21:33:04 +08:00
|
|
|
type Stack struct {
|
2016-11-11 06:59:32 +08:00
|
|
|
values []int
|
|
|
|
len int
|
2015-04-21 21:33:04 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 21:49:19 +08:00
|
|
|
// NewStack inits new Stack with default size limit.
|
|
|
|
func NewStack() *Stack {
|
|
|
|
return NewStackWithSize(DefaultSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStackWithSize inits new Stack with size limit.
|
|
|
|
func NewStackWithSize(size int) *Stack {
|
2015-04-21 21:33:04 +08:00
|
|
|
return &Stack{
|
2016-11-11 06:59:32 +08:00
|
|
|
values: make([]int, size),
|
|
|
|
len: size,
|
2015-04-21 21:33:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 14:06:45 +08:00
|
|
|
// Push inserts data to stack, preserving constant length.
|
2016-11-11 05:34:31 +08:00
|
|
|
func (s *Stack) Push(v IntVar) {
|
|
|
|
val := v.Value()
|
2016-11-11 06:59:32 +08:00
|
|
|
s.values = append(s.values, val)
|
|
|
|
if len(s.values) > s.len {
|
2016-11-11 05:34:31 +08:00
|
|
|
// TODO: check if underlying array is growing constantly
|
2016-11-11 06:59:32 +08:00
|
|
|
s.values = s.values[1:]
|
2015-04-21 21:33:04 +08:00
|
|
|
}
|
2015-05-01 04:54:54 +08:00
|
|
|
}
|
2015-05-02 06:21:52 +08:00
|
|
|
|
2016-11-11 06:59:32 +08:00
|
|
|
// Values returns stack values explicitly casted to int.
|
2015-05-02 06:21:52 +08:00
|
|
|
//
|
|
|
|
// Main case is to use with termui.Sparklines.
|
2016-11-11 06:59:32 +08:00
|
|
|
func (s *Stack) Values() []int {
|
|
|
|
return s.values
|
2015-05-02 06:21:52 +08:00
|
|
|
}
|
2016-11-11 05:34:31 +08:00
|
|
|
|
|
|
|
// TODO: implement trim and resize
|