xmtop/termui/list.go

43 lines
760 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package termui
2018-02-19 15:51:55 +08:00
import (
"fmt"
)
2018-02-19 15:25:02 +08:00
// BarChart creates multiple bars in a widget:
type List struct {
*Block
2018-02-21 16:46:11 +08:00
TextColor Color
2018-02-19 15:25:02 +08:00
Data []int
DataLabels []string
Threshold int
}
// NewBarChart returns a new *BarChart with current theme.
func NewList() *List {
return &List{
Block: NewBlock(),
TextColor: Theme.Fg,
}
}
// Buffer implements Bufferer interface.
func (bc *List) Buffer() *Buffer {
buf := bc.Block.Buffer()
for y, text := range bc.DataLabels {
if y+1 > bc.Y {
break
}
2018-02-22 03:15:53 +08:00
fg := Theme.TempLow
2018-02-19 15:25:02 +08:00
if bc.Data[y] >= bc.Threshold {
2018-02-22 03:15:53 +08:00
fg = Theme.TempHigh
2018-02-19 15:25:02 +08:00
}
r := MaxString(text, (bc.X - 4))
2018-02-22 03:15:53 +08:00
buf.SetString(1, y+1, r, Theme.Fg, bc.Bg)
buf.SetString(bc.X-2, y+1, fmt.Sprintf("%dC", bc.Data[y]), fg, bc.Bg)
2018-02-19 15:25:02 +08:00
}
return buf
}