package cview
import (
"math"
"sync"
"github.com/gdamore/tcell/v2"
)
type ProgressBar struct {
*Box
emptyRune rune
emptyColor tcell .Color
filledRune rune
filledColor tcell .Color
vertical bool
progress int
max int
sync .RWMutex
}
func NewProgressBar () *ProgressBar {
p := &ProgressBar {
Box : NewBox (),
emptyRune : tcell .RuneBlock ,
emptyColor : Styles .PrimitiveBackgroundColor ,
filledRune : tcell .RuneBlock ,
filledColor : Styles .PrimaryTextColor ,
max : 100 ,
}
p .SetBackgroundColor (Styles .PrimitiveBackgroundColor )
return p
}
func (p *ProgressBar ) SetEmptyRune (empty rune ) {
p .Lock ()
defer p .Unlock ()
p .emptyRune = empty
}
func (p *ProgressBar ) SetEmptyColor (empty tcell .Color ) {
p .Lock ()
defer p .Unlock ()
p .emptyColor = empty
}
func (p *ProgressBar ) SetFilledRune (filled rune ) {
p .Lock ()
defer p .Unlock ()
p .filledRune = filled
}
func (p *ProgressBar ) SetFilledColor (filled tcell .Color ) {
p .Lock ()
defer p .Unlock ()
p .filledColor = filled
}
func (p *ProgressBar ) SetVertical (vertical bool ) {
p .Lock ()
defer p .Unlock ()
p .vertical = vertical
}
func (p *ProgressBar ) SetMax (max int ) {
p .Lock ()
defer p .Unlock ()
if max < 1 {
max = 1
}
p .max = max
}
func (p *ProgressBar ) GetMax () int {
p .RLock ()
defer p .RUnlock ()
return p .max
}
func (p *ProgressBar ) AddProgress (progress int ) {
p .Lock ()
defer p .Unlock ()
p .progress += progress
if p .progress < 0 {
p .progress = 0
} else if p .progress > p .max {
p .progress = p .max
}
}
func (p *ProgressBar ) SetProgress (progress int ) {
p .Lock ()
defer p .Unlock ()
p .progress = progress
if p .progress < 0 {
p .progress = 0
} else if p .progress > p .max {
p .progress = p .max
}
}
func (p *ProgressBar ) GetProgress () int {
p .RLock ()
defer p .RUnlock ()
return p .progress
}
func (p *ProgressBar ) Complete () bool {
p .RLock ()
defer p .RUnlock ()
return p .progress >= p .max
}
func (p *ProgressBar ) Draw (screen tcell .Screen ) {
if !p .GetVisible () {
return
}
p .Box .Draw (screen )
p .Lock ()
defer p .Unlock ()
x , y , width , height := p .GetInnerRect ()
barSize := height
maxLength := width
if p .vertical {
barSize = width
maxLength = height
}
barLength := int (math .RoundToEven (float64 (maxLength ) * (float64 (p .progress ) / float64 (p .max ))))
if barLength > maxLength {
barLength = maxLength
}
for i := 0 ; i < barSize ; i ++ {
for j := 0 ; j < barLength ; j ++ {
if p .vertical {
screen .SetContent (x +i , y +(height -1 -j ), p .filledRune , nil , tcell .StyleDefault .Foreground (p .filledColor ).Background (p .backgroundColor ))
} else {
screen .SetContent (x +j , y +i , p .filledRune , nil , tcell .StyleDefault .Foreground (p .filledColor ).Background (p .backgroundColor ))
}
}
for j := barLength ; j < maxLength ; j ++ {
if p .vertical {
screen .SetContent (x +i , y +(height -1 -j ), p .emptyRune , nil , tcell .StyleDefault .Foreground (p .emptyColor ).Background (p .backgroundColor ))
} else {
screen .SetContent (x +j , y +i , p .emptyRune , nil , tcell .StyleDefault .Foreground (p .emptyColor ).Background (p .backgroundColor ))
}
}
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .