2015-03-20 14:21:50 -06:00
|
|
|
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
package termui
|
|
|
|
|
|
|
|
import "strconv"
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Gauge is a progress bar like widget.
|
|
|
|
// A simple example:
|
|
|
|
/*
|
|
|
|
g := termui.NewGauge()
|
|
|
|
g.Percent = 40
|
|
|
|
g.Width = 50
|
|
|
|
g.Height = 3
|
|
|
|
g.Border.Label = "Slim Gauge"
|
|
|
|
g.BarColor = termui.ColorRed
|
|
|
|
g.PercentColor = termui.ColorBlue
|
|
|
|
*/
|
2015-02-03 18:56:49 -07:00
|
|
|
type Gauge struct {
|
|
|
|
Block
|
|
|
|
Percent int
|
|
|
|
BarColor Attribute
|
|
|
|
PercentColor Attribute
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// NewGauge return a new gauge with current theme.
|
2015-02-03 18:56:49 -07:00
|
|
|
func NewGauge() *Gauge {
|
2015-03-11 14:15:59 -06:00
|
|
|
g := &Gauge{
|
|
|
|
Block: *NewBlock(),
|
|
|
|
PercentColor: theme.GaugePercent,
|
|
|
|
BarColor: theme.GaugeBar}
|
2015-02-03 18:56:49 -07:00
|
|
|
g.Width = 12
|
|
|
|
g.Height = 5
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Buffer implements Bufferer interface.
|
2015-02-03 18:56:49 -07:00
|
|
|
func (g *Gauge) Buffer() []Point {
|
|
|
|
ps := g.Block.Buffer()
|
|
|
|
|
|
|
|
w := g.Percent * g.innerWidth / 100
|
|
|
|
s := strconv.Itoa(g.Percent) + "%"
|
|
|
|
rs := str2runes(s)
|
|
|
|
|
|
|
|
prx := g.innerX + g.innerWidth/2 - 1
|
|
|
|
pry := g.innerY + g.innerHeight/2
|
|
|
|
|
|
|
|
// plot bar
|
|
|
|
for i := 0; i < g.innerHeight; i++ {
|
|
|
|
for j := 0; j < w; j++ {
|
|
|
|
p := Point{}
|
|
|
|
p.X = g.innerX + j
|
|
|
|
p.Y = g.innerY + i
|
2015-03-03 11:28:09 -07:00
|
|
|
p.Ch = ' '
|
|
|
|
p.Bg = g.BarColor
|
2015-03-11 14:15:59 -06:00
|
|
|
if p.Bg == ColorDefault {
|
|
|
|
p.Bg |= AttrReverse
|
|
|
|
}
|
2015-02-03 18:56:49 -07:00
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot percentage
|
|
|
|
for i, v := range rs {
|
|
|
|
p := Point{}
|
|
|
|
p.X = prx + i
|
|
|
|
p.Y = pry
|
2015-03-03 11:28:09 -07:00
|
|
|
p.Ch = v
|
|
|
|
p.Fg = g.PercentColor
|
2015-02-03 18:56:49 -07:00
|
|
|
if w > g.innerWidth/2-1+i {
|
2015-03-03 11:28:09 -07:00
|
|
|
p.Bg = g.BarColor
|
2015-03-11 14:15:59 -06:00
|
|
|
if p.Bg == ColorDefault {
|
|
|
|
p.Bg |= AttrReverse
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
} else {
|
2015-03-03 11:28:09 -07:00
|
|
|
p.Bg = g.Block.BgColor
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
2015-04-09 14:40:49 -06:00
|
|
|
return g.Block.chopOverflow(ps)
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|