Merge pull request #35 from Matt3o12/gauge-label
Gauge custom percentage label
This commit is contained in:
commit
c7e9e645d4
@ -1,4 +1,4 @@
|
|||||||
# termui [](https://travis-ci.org/gizak/termui) [](https://godoc.org/github.com/gizak/termui)
|
# termui [](https://travis-ci.org/gizak/termui) [](https://godoc.org/github.com/gizak/termui)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -47,7 +47,16 @@ func main() {
|
|||||||
g1.Border.FgColor = termui.ColorWhite
|
g1.Border.FgColor = termui.ColorWhite
|
||||||
g1.Border.LabelFgColor = termui.ColorMagenta
|
g1.Border.LabelFgColor = termui.ColorMagenta
|
||||||
|
|
||||||
termui.Render(g0, g1, g2)
|
g3 := termui.NewGauge()
|
||||||
|
g3.Percent = 50
|
||||||
|
g3.Width = 50
|
||||||
|
g3.Height = 3
|
||||||
|
g3.Y = 11
|
||||||
|
g3.Border.Label = "Gauge with custom label"
|
||||||
|
g3.Label = "{{percent}}% (100MBs free)"
|
||||||
|
g3.LabelAlign = termui.AlignRight
|
||||||
|
|
||||||
|
termui.Render(g0, g1, g2, g3)
|
||||||
|
|
||||||
<-termui.EventCh()
|
<-termui.EventCh()
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 32 KiB |
52
gauge.go
52
gauge.go
@ -4,7 +4,10 @@
|
|||||||
|
|
||||||
package termui
|
package termui
|
||||||
|
|
||||||
import "strconv"
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Gauge is a progress bar like widget.
|
// Gauge is a progress bar like widget.
|
||||||
// A simple example:
|
// A simple example:
|
||||||
@ -17,11 +20,24 @@ import "strconv"
|
|||||||
g.BarColor = termui.ColorRed
|
g.BarColor = termui.ColorRed
|
||||||
g.PercentColor = termui.ColorBlue
|
g.PercentColor = termui.ColorBlue
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Align is the position of the gauge's label.
|
||||||
|
type Align int
|
||||||
|
|
||||||
|
// All supported positions.
|
||||||
|
const (
|
||||||
|
AlignLeft Align = iota
|
||||||
|
AlignCenter
|
||||||
|
AlignRight
|
||||||
|
)
|
||||||
|
|
||||||
type Gauge struct {
|
type Gauge struct {
|
||||||
Block
|
Block
|
||||||
Percent int
|
Percent int
|
||||||
BarColor Attribute
|
BarColor Attribute
|
||||||
PercentColor Attribute
|
PercentColor Attribute
|
||||||
|
Label string
|
||||||
|
LabelAlign Align
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGauge return a new gauge with current theme.
|
// NewGauge return a new gauge with current theme.
|
||||||
@ -29,7 +45,11 @@ func NewGauge() *Gauge {
|
|||||||
g := &Gauge{
|
g := &Gauge{
|
||||||
Block: *NewBlock(),
|
Block: *NewBlock(),
|
||||||
PercentColor: theme.GaugePercent,
|
PercentColor: theme.GaugePercent,
|
||||||
BarColor: theme.GaugeBar}
|
BarColor: theme.GaugeBar,
|
||||||
|
Label: "{{percent}}%",
|
||||||
|
LabelAlign: AlignCenter,
|
||||||
|
}
|
||||||
|
|
||||||
g.Width = 12
|
g.Width = 12
|
||||||
g.Height = 5
|
g.Height = 5
|
||||||
return g
|
return g
|
||||||
@ -39,14 +59,8 @@ func NewGauge() *Gauge {
|
|||||||
func (g *Gauge) Buffer() []Point {
|
func (g *Gauge) Buffer() []Point {
|
||||||
ps := g.Block.Buffer()
|
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
|
// plot bar
|
||||||
|
w := g.Percent * g.innerWidth / 100
|
||||||
for i := 0; i < g.innerHeight; i++ {
|
for i := 0; i < g.innerHeight; i++ {
|
||||||
for j := 0; j < w; j++ {
|
for j := 0; j < w; j++ {
|
||||||
p := Point{}
|
p := Point{}
|
||||||
@ -62,13 +76,28 @@ func (g *Gauge) Buffer() []Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// plot percentage
|
// plot percentage
|
||||||
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||||||
|
pry := g.innerY + g.innerHeight/2
|
||||||
|
rs := str2runes(s)
|
||||||
|
var pos int
|
||||||
|
switch g.LabelAlign {
|
||||||
|
case AlignLeft:
|
||||||
|
pos = 0
|
||||||
|
|
||||||
|
case AlignCenter:
|
||||||
|
pos = (g.innerWidth - strWidth(s)) / 2
|
||||||
|
|
||||||
|
case AlignRight:
|
||||||
|
pos = g.innerWidth - strWidth(s)
|
||||||
|
}
|
||||||
|
|
||||||
for i, v := range rs {
|
for i, v := range rs {
|
||||||
p := Point{}
|
p := Point{}
|
||||||
p.X = prx + i
|
p.X = 1 + pos + i
|
||||||
p.Y = pry
|
p.Y = pry
|
||||||
p.Ch = v
|
p.Ch = v
|
||||||
p.Fg = g.PercentColor
|
p.Fg = g.PercentColor
|
||||||
if w > g.innerWidth/2-1+i {
|
if w > pos+i {
|
||||||
p.Bg = g.BarColor
|
p.Bg = g.BarColor
|
||||||
if p.Bg == ColorDefault {
|
if p.Bg == ColorDefault {
|
||||||
p.Bg |= AttrReverse
|
p.Bg |= AttrReverse
|
||||||
@ -77,6 +106,7 @@ func (g *Gauge) Buffer() []Point {
|
|||||||
} else {
|
} else {
|
||||||
p.Bg = g.Block.BgColor
|
p.Bg = g.Block.BgColor
|
||||||
}
|
}
|
||||||
|
|
||||||
ps = append(ps, p)
|
ps = append(ps, p)
|
||||||
}
|
}
|
||||||
return g.Block.chopOverflow(ps)
|
return g.Block.chopOverflow(ps)
|
||||||
|
Loading…
Reference in New Issue
Block a user