1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00

Colorized Progress Bar

This commit is contained in:
Brian Buller 2015-12-17 21:41:02 -06:00
parent 9ca0888000
commit 1c468454f6

View File

@ -12,6 +12,7 @@ type ProgressBar struct {
emptyChar rune emptyChar rune
bordered bool bordered bool
alignment TextAlignment alignment TextAlignment
colorized bool
x, y int x, y int
width, height int width, height int
@ -174,6 +175,16 @@ func (i *ProgressBar) Align(a TextAlignment) *ProgressBar {
return i return i
} }
// SetColorized sets whether the progress bar should be colored
// depending on how full it is:
// 10% - Red
// 50% - Yellow
// 80% - Green
func (i *ProgressBar) SetColorized(c bool) *ProgressBar {
i.colorized = c
return i
}
// HandleKeyPress accepts the termbox event and returns whether it was consumed // HandleKeyPress accepts the termbox event and returns whether it was consumed
func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool {
return false return false
@ -183,11 +194,21 @@ func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool {
func (i *ProgressBar) Draw() { func (i *ProgressBar) Draw() {
// For now, just draw a [#### ] bar // For now, just draw a [#### ] bar
// TODO: make this more advanced // TODO: make this more advanced
useFg := i.fg
if i.colorized {
if i.GetPercent() < 10 {
useFg = termbox.ColorRed
} else if i.GetPercent() < 50 {
useFg = termbox.ColorYellow
} else {
useFg = termbox.ColorGreen
}
}
drawX, drawY := i.x, i.y drawX, drawY := i.x, i.y
fillWidth, fillHeight := i.width-2, i.height fillWidth, fillHeight := i.width-2, i.height
DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg) DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg)
numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total)) numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total))
FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), i.fg, i.bg) FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), useFg, i.bg)
DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg) DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg)
/* /*