Move gauge into widget folder
This commit is contained in:
parent
746582638b
commit
371d184755
113
gauge.go
113
gauge.go
@ -1,113 +0,0 @@
|
|||||||
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
||||||
// 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
|
|
||||||
g.Border.Label = "Slim Gauge"
|
|
||||||
g.BarColor = termui.ColorRed
|
|
||||||
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 {
|
|
||||||
Block
|
|
||||||
Percent int
|
|
||||||
BarColor Attribute
|
|
||||||
PercentColor Attribute
|
|
||||||
Label string
|
|
||||||
LabelAlign Align
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewGauge return a new gauge with current theme.
|
|
||||||
func NewGauge() *Gauge {
|
|
||||||
g := &Gauge{
|
|
||||||
Block: *NewBlock(),
|
|
||||||
PercentColor: theme.GaugePercent,
|
|
||||||
BarColor: theme.GaugeBar,
|
|
||||||
Label: "{{percent}}%",
|
|
||||||
LabelAlign: AlignCenter,
|
|
||||||
}
|
|
||||||
|
|
||||||
g.Width = 12
|
|
||||||
g.Height = 5
|
|
||||||
return g
|
|
||||||
}
|
|
||||||
|
|
||||||
// Buffer implements Bufferer interface.
|
|
||||||
func (g *Gauge) Buffer() []Point {
|
|
||||||
ps := g.Block.Buffer()
|
|
||||||
|
|
||||||
// plot bar
|
|
||||||
w := g.Percent * g.innerWidth / 100
|
|
||||||
for i := 0; i < g.innerHeight; i++ {
|
|
||||||
for j := 0; j < w; j++ {
|
|
||||||
p := Point{}
|
|
||||||
p.X = g.innerX + j
|
|
||||||
p.Y = g.innerY + i
|
|
||||||
p.Ch = ' '
|
|
||||||
p.Bg = g.BarColor
|
|
||||||
if p.Bg == ColorDefault {
|
|
||||||
p.Bg |= AttrReverse
|
|
||||||
}
|
|
||||||
ps = append(ps, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
p := Point{}
|
|
||||||
p.X = 1 + pos + i
|
|
||||||
p.Y = pry
|
|
||||||
p.Ch = v
|
|
||||||
p.Fg = g.PercentColor
|
|
||||||
if w+g.innerX > pos+i {
|
|
||||||
p.Bg = g.BarColor
|
|
||||||
if p.Bg == ColorDefault {
|
|
||||||
p.Bg |= AttrReverse
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
p.Bg = g.Block.BgColor
|
|
||||||
}
|
|
||||||
|
|
||||||
ps = append(ps, p)
|
|
||||||
}
|
|
||||||
return g.Block.chopOverflow(ps)
|
|
||||||
}
|
|
104
widget/gauge.go
104
widget/gauge.go
@ -2,10 +2,12 @@
|
|||||||
// Use of this source code is governed by a MIT license that can
|
// Use of this source code is governed by a MIT license that can
|
||||||
// be found in the LICENSE file.
|
// be found in the LICENSE file.
|
||||||
|
|
||||||
package widget
|
package termui
|
||||||
|
|
||||||
import "github.com/gizak/termui"
|
import (
|
||||||
import "strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Gauge is a progress bar like widget.
|
// Gauge is a progress bar like widget.
|
||||||
// A simple example:
|
// A simple example:
|
||||||
@ -18,50 +20,94 @@ 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 {
|
||||||
termui.Block
|
Block
|
||||||
Percent int
|
Percent int
|
||||||
BarColor termui.Attribute
|
BarColor Attribute
|
||||||
PercentColor termui.Attribute
|
PercentColor Attribute
|
||||||
|
Label string
|
||||||
|
LabelAlign Align
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGauge return a new gauge with current theme.
|
// NewGauge return a new gauge with current theme.
|
||||||
func NewGauge() *Gauge {
|
func NewGauge() *Gauge {
|
||||||
g := &Gauge{
|
g := &Gauge{
|
||||||
Block: *termui.NewBlock(),
|
Block: *NewBlock(),
|
||||||
PercentColor: termui.Theme().GaugePercent,
|
PercentColor: theme.GaugePercent,
|
||||||
BarColor: termui.Theme().GaugeBar}
|
BarColor: theme.GaugeBar,
|
||||||
|
Label: "{{percent}}%",
|
||||||
|
LabelAlign: AlignCenter,
|
||||||
|
}
|
||||||
|
|
||||||
g.Width = 12
|
g.Width = 12
|
||||||
g.Height = 3
|
g.Height = 5
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffer implements Bufferer interface.
|
// Buffer implements Bufferer interface.
|
||||||
func (g *Gauge) Buffer() termui.Buffer {
|
func (g *Gauge) Buffer() []Point {
|
||||||
buf := g.Block.Buffer()
|
ps := g.Block.Buffer()
|
||||||
|
|
||||||
inner := g.InnerBounds()
|
|
||||||
w := g.Percent * (inner.Dx() + 1) / 100
|
|
||||||
s := strconv.Itoa(g.Percent) + "%"
|
|
||||||
tx := termui.TextCells(s, g.PercentColor, g.Bg)
|
|
||||||
|
|
||||||
prx := inner.Min.X + (inner.Dx()+1)/2 - 1
|
|
||||||
pry := inner.Min.Y + (inner.Dy()+1)/2
|
|
||||||
|
|
||||||
// plot bar
|
// plot bar
|
||||||
for i := 0; i <= inner.Dy(); i++ {
|
w := g.Percent * g.innerWidth / 100
|
||||||
|
for i := 0; i < g.innerHeight; i++ {
|
||||||
for j := 0; j < w; j++ {
|
for j := 0; j < w; j++ {
|
||||||
c := termui.Cell{' ', g.BarColor, g.BarColor}
|
p := Point{}
|
||||||
buf.Set(inner.Min.X+j, inner.Min.Y+i, c)
|
p.X = g.innerX + j
|
||||||
|
p.Y = g.innerY + i
|
||||||
|
p.Ch = ' '
|
||||||
|
p.Bg = g.BarColor
|
||||||
|
if p.Bg == ColorDefault {
|
||||||
|
p.Bg |= AttrReverse
|
||||||
|
}
|
||||||
|
ps = append(ps, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// plot percentage
|
// plot percentage
|
||||||
for i, v := range tx {
|
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||||||
if w > (inner.Dx()+1)/2-1+i {
|
pry := g.innerY + g.innerHeight/2
|
||||||
v.Bg = g.BarColor
|
rs := str2runes(s)
|
||||||
}
|
var pos int
|
||||||
buf.Set(prx+i, pry, v)
|
switch g.LabelAlign {
|
||||||
|
case AlignLeft:
|
||||||
|
pos = 0
|
||||||
|
|
||||||
|
case AlignCenter:
|
||||||
|
pos = (g.innerWidth - strWidth(s)) / 2
|
||||||
|
|
||||||
|
case AlignRight:
|
||||||
|
pos = g.innerWidth - strWidth(s)
|
||||||
}
|
}
|
||||||
return buf
|
|
||||||
|
for i, v := range rs {
|
||||||
|
p := Point{}
|
||||||
|
p.X = 1 + pos + i
|
||||||
|
p.Y = pry
|
||||||
|
p.Ch = v
|
||||||
|
p.Fg = g.PercentColor
|
||||||
|
if w+g.innerX > pos+i {
|
||||||
|
p.Bg = g.BarColor
|
||||||
|
if p.Bg == ColorDefault {
|
||||||
|
p.Bg |= AttrReverse
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
p.Bg = g.Block.BgColor
|
||||||
|
}
|
||||||
|
|
||||||
|
ps = append(ps, p)
|
||||||
|
}
|
||||||
|
return g.Block.chopOverflow(ps)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user