From 1c468454f6011c29b62a628c2889f1f815024157 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 17 Dec 2015 21:41:02 -0600 Subject: [PATCH] Colorized Progress Bar --- termbox_progressbar.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/termbox_progressbar.go b/termbox_progressbar.go index afb0ed8..0cbd3e0 100644 --- a/termbox_progressbar.go +++ b/termbox_progressbar.go @@ -12,6 +12,7 @@ type ProgressBar struct { emptyChar rune bordered bool alignment TextAlignment + colorized bool x, y int width, height int @@ -174,6 +175,16 @@ func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { 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 func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { return false @@ -183,11 +194,21 @@ func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { func (i *ProgressBar) Draw() { // For now, just draw a [#### ] bar // 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 fillWidth, fillHeight := i.width-2, i.height DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg) 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) /*