2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-20 14:21:50 -06:00
|
|
|
// 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
|
|
|
|
|
2015-04-21 08:43:09 -06:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
2015-02-03 18:56:49 -07:00
|
|
|
|
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
|
2016-10-14 05:48:50 -06:00
|
|
|
g.BorderLabel = "Slim Gauge"
|
2015-03-24 15:16:43 -06:00
|
|
|
g.BarColor = termui.ColorRed
|
|
|
|
g.PercentColor = termui.ColorBlue
|
|
|
|
*/
|
2015-04-21 09:41:52 -06:00
|
|
|
|
2015-10-22 09:17:20 -06:00
|
|
|
const ColorUndef Attribute = Attribute(^uint16(0))
|
2015-04-22 14:04:30 -06:00
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
type Gauge struct {
|
|
|
|
Block
|
2015-04-22 14:04:30 -06:00
|
|
|
Percent int
|
|
|
|
BarColor Attribute
|
|
|
|
PercentColor Attribute
|
|
|
|
PercentColorHighlighted Attribute
|
|
|
|
Label string
|
|
|
|
LabelAlign Align
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
|
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{
|
2015-04-22 14:04:30 -06:00
|
|
|
Block: *NewBlock(),
|
2015-10-22 09:17:20 -06:00
|
|
|
PercentColor: ThemeAttr("gauge.percent.fg"),
|
|
|
|
BarColor: ThemeAttr("gauge.bar.bg"),
|
2015-04-22 14:04:30 -06:00
|
|
|
Label: "{{percent}}%",
|
|
|
|
LabelAlign: AlignCenter,
|
2015-10-22 09:17:20 -06:00
|
|
|
PercentColorHighlighted: ColorUndef,
|
2015-04-21 08:43:09 -06:00
|
|
|
}
|
|
|
|
|
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-10-07 12:25:59 -06:00
|
|
|
func (g *Gauge) Buffer() Buffer {
|
|
|
|
buf := g.Block.Buffer()
|
2015-02-03 18:56:49 -07:00
|
|
|
|
|
|
|
// plot bar
|
2015-10-07 12:25:59 -06:00
|
|
|
w := g.Percent * g.innerArea.Dx() / 100
|
|
|
|
for i := 0; i < g.innerArea.Dy(); i++ {
|
2015-02-03 18:56:49 -07:00
|
|
|
for j := 0; j < w; j++ {
|
2015-10-07 12:25:59 -06:00
|
|
|
c := Cell{}
|
|
|
|
c.Ch = ' '
|
|
|
|
c.Bg = g.BarColor
|
|
|
|
if c.Bg == ColorDefault {
|
|
|
|
c.Bg |= AttrReverse
|
2015-03-11 14:15:59 -06:00
|
|
|
}
|
2015-10-07 12:25:59 -06:00
|
|
|
buf.Set(g.innerArea.Min.X+j, g.innerArea.Min.Y+i, c)
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot percentage
|
2015-04-21 08:43:09 -06:00
|
|
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
2015-10-07 12:25:59 -06:00
|
|
|
pry := g.innerArea.Min.Y + g.innerArea.Dy()/2
|
2015-04-21 08:43:09 -06:00
|
|
|
rs := str2runes(s)
|
2015-04-21 09:41:52 -06:00
|
|
|
var pos int
|
2015-04-21 10:07:48 -06:00
|
|
|
switch g.LabelAlign {
|
2015-04-21 09:41:52 -06:00
|
|
|
case AlignLeft:
|
|
|
|
pos = 0
|
|
|
|
|
|
|
|
case AlignCenter:
|
2015-10-07 12:25:59 -06:00
|
|
|
pos = (g.innerArea.Dx() - strWidth(s)) / 2
|
2015-04-21 09:41:52 -06:00
|
|
|
|
|
|
|
case AlignRight:
|
2015-10-10 09:20:44 -06:00
|
|
|
pos = g.innerArea.Dx() - strWidth(s) - 1
|
2015-04-21 09:41:52 -06:00
|
|
|
}
|
2015-10-10 09:20:44 -06:00
|
|
|
pos += g.innerArea.Min.X
|
2015-04-21 09:41:52 -06:00
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
for i, v := range rs {
|
2015-10-07 12:25:59 -06:00
|
|
|
c := Cell{
|
|
|
|
Ch: v,
|
|
|
|
Fg: g.PercentColor,
|
|
|
|
}
|
|
|
|
|
|
|
|
if w+g.innerArea.Min.X > pos+i {
|
|
|
|
c.Bg = g.BarColor
|
|
|
|
if c.Bg == ColorDefault {
|
|
|
|
c.Bg |= AttrReverse
|
2015-03-11 14:15:59 -06:00
|
|
|
}
|
|
|
|
|
2015-10-22 09:17:20 -06:00
|
|
|
if g.PercentColorHighlighted != ColorUndef {
|
|
|
|
c.Fg = g.PercentColorHighlighted
|
2015-04-22 14:04:30 -06:00
|
|
|
}
|
2015-02-03 18:56:49 -07:00
|
|
|
} else {
|
2015-10-07 12:25:59 -06:00
|
|
|
c.Bg = g.Block.Bg
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
2015-04-21 08:43:09 -06:00
|
|
|
|
2015-10-07 12:25:59 -06:00
|
|
|
buf.Set(1+pos+i, pry, c)
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
2015-10-07 12:25:59 -06:00
|
|
|
return buf
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|