1
0
mirror of https://github.com/divan/expvarmon.git synced 2025-04-27 13:48:55 +08:00
expvarmon/stack.go

46 lines
1.0 KiB
Go
Raw Normal View History

2015-04-21 16:33:04 +03:00
package main
2015-05-01 16:49:19 +03: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 09:06:45 +03:00
// Stack is a limited FIFO for holding sparkline values.
2015-04-21 16:33:04 +03:00
type Stack struct {
2016-11-10 23:59:32 +01:00
values []int
len int
2015-04-21 16:33:04 +03:00
}
2015-05-01 16:49:19 +03: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 16:33:04 +03:00
return &Stack{
2016-11-10 23:59:32 +01:00
values: make([]int, size),
len: size,
2015-04-21 16:33:04 +03:00
}
}
2015-04-25 09:06:45 +03:00
// Push inserts data to stack, preserving constant length.
func (s *Stack) Push(v IntVar) {
val := v.Value()
2016-11-10 23:59:32 +01:00
s.values = append(s.values, val)
if len(s.values) > s.len {
// TODO: check if underlying array is growing constantly
2016-11-10 23:59:32 +01:00
s.values = s.values[1:]
2015-04-21 16:33:04 +03:00
}
2015-04-30 23:54:54 +03:00
}
2015-05-02 01:21:52 +03:00
2016-11-10 23:59:32 +01:00
// Values returns stack values explicitly casted to int.
2015-05-02 01:21:52 +03:00
//
// Main case is to use with termui.Sparklines.
2016-11-10 23:59:32 +01:00
func (s *Stack) Values() []int {
return s.values
2015-05-02 01:21:52 +03:00
}
// TODO: implement trim and resize