mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.2 KiB
110 lines
2.2 KiB
9 years ago
|
// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
|
||
10 years ago
|
// 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
|
||
8 years ago
|
g.BorderLabel = "Slim Gauge"
|
||
10 years ago
|
g.BarColor = termui.ColorRed
|
||
|
g.PercentColor = termui.ColorBlue
|
||
|
*/
|
||
|
|
||
9 years ago
|
const ColorUndef Attribute = Attribute(^uint16(0))
|
||
10 years ago
|
|
||
|
type Gauge struct {
|
||
|
Block
|
||
9 years ago
|
Percent int
|
||
|
BarColor Attribute
|
||
|
PercentColor Attribute
|
||
|
PercentColorHighlighted Attribute
|
||
|
Label string
|
||
|
LabelAlign Align
|
||
10 years ago
|
}
|
||
|
|
||
|
// NewGauge return a new gauge with current theme.
|
||
|
func NewGauge() *Gauge {
|
||
|
g := &Gauge{
|
||
9 years ago
|
Block: *NewBlock(),
|
||
|
PercentColor: ThemeAttr("gauge.percent.fg"),
|
||
|
BarColor: ThemeAttr("gauge.bar.bg"),
|
||
|
Label: "{{percent}}%",
|
||
|
LabelAlign: AlignCenter,
|
||
|
PercentColorHighlighted: ColorUndef,
|
||
10 years ago
|
}
|
||
|
|
||
|
g.Width = 12
|
||
|
g.Height = 5
|
||
|
return g
|
||
|
}
|
||
|
|
||
|
// Buffer implements Bufferer interface.
|
||
9 years ago
|
func (g *Gauge) Buffer() Buffer {
|
||
|
buf := g.Block.Buffer()
|
||
10 years ago
|
|
||
|
// plot bar
|
||
9 years ago
|
w := g.Percent * g.innerArea.Dx() / 100
|
||
|
for i := 0; i < g.innerArea.Dy(); i++ {
|
||
10 years ago
|
for j := 0; j < w; j++ {
|
||
9 years ago
|
c := Cell{}
|
||
|
c.Ch = ' '
|
||
|
c.Bg = g.BarColor
|
||
|
if c.Bg == ColorDefault {
|
||
|
c.Bg |= AttrReverse
|
||
10 years ago
|
}
|
||
9 years ago
|
buf.Set(g.innerArea.Min.X+j, g.innerArea.Min.Y+i, c)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// plot percentage
|
||
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||
9 years ago
|
pry := g.innerArea.Min.Y + g.innerArea.Dy()/2
|
||
10 years ago
|
rs := str2runes(s)
|
||
|
var pos int
|
||
|
switch g.LabelAlign {
|
||
|
case AlignLeft:
|
||
|
pos = 0
|
||
|
|
||
|
case AlignCenter:
|
||
9 years ago
|
pos = (g.innerArea.Dx() - strWidth(s)) / 2
|
||
10 years ago
|
|
||
|
case AlignRight:
|
||
9 years ago
|
pos = g.innerArea.Dx() - strWidth(s) - 1
|
||
10 years ago
|
}
|
||
9 years ago
|
pos += g.innerArea.Min.X
|
||
10 years ago
|
|
||
|
for i, v := range rs {
|
||
9 years ago
|
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
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if g.PercentColorHighlighted != ColorUndef {
|
||
|
c.Fg = g.PercentColorHighlighted
|
||
|
}
|
||
10 years ago
|
} else {
|
||
9 years ago
|
c.Bg = g.Block.Bg
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
buf.Set(1+pos+i, pry, c)
|
||
10 years ago
|
}
|
||
9 years ago
|
return buf
|
||
10 years ago
|
}
|