2018-02-19 15:25:02 +08:00
|
|
|
package termui
|
|
|
|
|
2018-02-19 15:51:55 +08:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
2018-02-19 15:25:02 +08:00
|
|
|
|
|
|
|
// Gauge is a progress bar like widget.
|
|
|
|
type Gauge struct {
|
|
|
|
*Block
|
2018-02-25 13:35:57 +08:00
|
|
|
Percent int
|
|
|
|
GaugeColor Color
|
|
|
|
Description string
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGauge return a new gauge with current theme.
|
|
|
|
func NewGauge() *Gauge {
|
|
|
|
return &Gauge{
|
2018-02-25 13:35:57 +08:00
|
|
|
Block: NewBlock(),
|
|
|
|
GaugeColor: Theme.GaugeColor,
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer implements Bufferer interface.
|
2018-03-28 05:27:23 +08:00
|
|
|
func (self *Gauge) Buffer() *Buffer {
|
|
|
|
buf := self.Block.Buffer()
|
2018-02-19 15:25:02 +08:00
|
|
|
|
|
|
|
// plot bar
|
2018-03-28 05:27:23 +08:00
|
|
|
width := self.Percent * self.X / 100
|
|
|
|
for y := 1; y <= self.Y; y++ {
|
2018-02-19 15:25:02 +08:00
|
|
|
for x := 1; x <= width; x++ {
|
2018-03-28 05:27:23 +08:00
|
|
|
buf.SetCell(x, y, Cell{' ', self.GaugeColor, self.GaugeColor})
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot percentage
|
2018-03-28 05:27:23 +08:00
|
|
|
s := strconv.Itoa(self.Percent) + "%" + self.Description
|
|
|
|
s = MaxString(s, self.X)
|
|
|
|
y := (self.Y + 1) / 2
|
|
|
|
x := ((self.X - len(s)) + 1) / 2
|
2018-02-19 15:25:02 +08:00
|
|
|
for i, char := range s {
|
2018-03-28 05:27:23 +08:00
|
|
|
bg := self.Bg
|
|
|
|
fg := self.Fg
|
2018-02-19 15:25:02 +08:00
|
|
|
if x+i < width {
|
2018-03-28 05:27:23 +08:00
|
|
|
fg = self.GaugeColor
|
2018-02-19 15:25:02 +08:00
|
|
|
bg = AttrReverse
|
|
|
|
}
|
2018-02-21 18:24:36 +08:00
|
|
|
buf.SetCell(1+x+i, y, Cell{char, fg, bg})
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|