Added support for label align in gauge
s.
This commit is contained in:
parent
3968b02432
commit
137d2a7eb2
@ -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)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -54,6 +54,7 @@ func main() {
|
|||||||
g3.Y = 11
|
g3.Y = 11
|
||||||
g3.Border.Label = "Gauge with custom label"
|
g3.Border.Label = "Gauge with custom label"
|
||||||
g3.Label = "{{percent}}% (100MBs free)"
|
g3.Label = "{{percent}}% (100MBs free)"
|
||||||
|
g3.Align = termui.AlignRight
|
||||||
|
|
||||||
termui.Render(g0, g1, g2, g3)
|
termui.Render(g0, g1, g2, g3)
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 32 KiB |
30
gauge.go
30
gauge.go
@ -20,12 +20,24 @@ import (
|
|||||||
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
|
Label string
|
||||||
|
Align Align
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGauge return a new gauge with current theme.
|
// NewGauge return a new gauge with current theme.
|
||||||
@ -35,6 +47,7 @@ func NewGauge() *Gauge {
|
|||||||
PercentColor: theme.GaugePercent,
|
PercentColor: theme.GaugePercent,
|
||||||
BarColor: theme.GaugeBar,
|
BarColor: theme.GaugeBar,
|
||||||
Label: "{{percent}}%",
|
Label: "{{percent}}%",
|
||||||
|
Align: AlignCenter,
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Width = 12
|
g.Width = 12
|
||||||
@ -64,16 +77,27 @@ func (g *Gauge) Buffer() []Point {
|
|||||||
|
|
||||||
// plot percentage
|
// plot percentage
|
||||||
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||||||
prx := g.innerX + (g.innerWidth-strWidth(s))/2
|
|
||||||
pry := g.innerY + g.innerHeight/2
|
pry := g.innerY + g.innerHeight/2
|
||||||
rs := str2runes(s)
|
rs := str2runes(s)
|
||||||
|
var pos int
|
||||||
|
switch g.Align {
|
||||||
|
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-strWidth(s))/2+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
|
||||||
|
Loading…
Reference in New Issue
Block a user