2016-02-11 16:16:52 +02:00
|
|
|
// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
|
2015-06-24 18:40:18 +03:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
package termui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gauge is a progress bar like widget.
|
|
|
|
// A simple example:
|
|
|
|
/*
|
|
|
|
g := termui.NewGauge()
|
|
|
|
g.Percent = 40
|
|
|
|
g.Width = 50
|
|
|
|
g.Height = 3
|
2016-10-28 20:05:01 +03:00
|
|
|
g.BorderLabel = "Slim Gauge"
|
2015-06-24 18:40:18 +03:00
|
|
|
g.BarColor = termui.ColorRed
|
|
|
|
g.PercentColor = termui.ColorBlue
|
|
|
|
*/
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
const ColorUndef Attribute = Attribute(^uint16(0))
|
2015-06-24 18:40:18 +03:00
|
|
|
|
|
|
|
type Gauge struct {
|
|
|
|
Block
|
2016-02-11 16:16:52 +02:00
|
|
|
Percent int
|
|
|
|
BarColor Attribute
|
|
|
|
PercentColor Attribute
|
|
|
|
PercentColorHighlighted Attribute
|
|
|
|
Label string
|
|
|
|
LabelAlign Align
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGauge return a new gauge with current theme.
|
|
|
|
func NewGauge() *Gauge {
|
|
|
|
g := &Gauge{
|
2016-02-11 16:16:52 +02:00
|
|
|
Block: *NewBlock(),
|
|
|
|
PercentColor: ThemeAttr("gauge.percent.fg"),
|
|
|
|
BarColor: ThemeAttr("gauge.bar.bg"),
|
|
|
|
Label: "{{percent}}%",
|
|
|
|
LabelAlign: AlignCenter,
|
|
|
|
PercentColorHighlighted: ColorUndef,
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
g.Width = 12
|
|
|
|
g.Height = 5
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer implements Bufferer interface.
|
2016-02-11 16:16:52 +02:00
|
|
|
func (g *Gauge) Buffer() Buffer {
|
|
|
|
buf := g.Block.Buffer()
|
2015-06-24 18:40:18 +03:00
|
|
|
|
|
|
|
// plot bar
|
2016-02-11 16:16:52 +02:00
|
|
|
w := g.Percent * g.innerArea.Dx() / 100
|
|
|
|
for i := 0; i < g.innerArea.Dy(); i++ {
|
2015-06-24 18:40:18 +03:00
|
|
|
for j := 0; j < w; j++ {
|
2016-02-11 16:16:52 +02:00
|
|
|
c := Cell{}
|
|
|
|
c.Ch = ' '
|
|
|
|
c.Bg = g.BarColor
|
|
|
|
if c.Bg == ColorDefault {
|
|
|
|
c.Bg |= AttrReverse
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
buf.Set(g.innerArea.Min.X+j, g.innerArea.Min.Y+i, c)
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plot percentage
|
|
|
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
2016-02-11 16:16:52 +02:00
|
|
|
pry := g.innerArea.Min.Y + g.innerArea.Dy()/2
|
2015-06-24 18:40:18 +03:00
|
|
|
rs := str2runes(s)
|
|
|
|
var pos int
|
|
|
|
switch g.LabelAlign {
|
|
|
|
case AlignLeft:
|
|
|
|
pos = 0
|
|
|
|
|
|
|
|
case AlignCenter:
|
2016-02-11 16:16:52 +02:00
|
|
|
pos = (g.innerArea.Dx() - strWidth(s)) / 2
|
2015-06-24 18:40:18 +03:00
|
|
|
|
|
|
|
case AlignRight:
|
2016-02-11 16:16:52 +02:00
|
|
|
pos = g.innerArea.Dx() - strWidth(s) - 1
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
pos += g.innerArea.Min.X
|
2015-06-24 18:40:18 +03:00
|
|
|
|
|
|
|
for i, v := range rs {
|
2016-02-11 16:16:52 +02: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-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
if g.PercentColorHighlighted != ColorUndef {
|
|
|
|
c.Fg = g.PercentColorHighlighted
|
|
|
|
}
|
2015-06-24 18:40:18 +03:00
|
|
|
} else {
|
2016-02-11 16:16:52 +02:00
|
|
|
c.Bg = g.Block.Bg
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
buf.Set(1+pos+i, pry, c)
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
return buf
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|