2015-11-30 14:21:22 +00:00
|
|
|
package termboxUtil
|
|
|
|
|
|
|
|
import "github.com/nsf/termbox-go"
|
|
|
|
|
|
|
|
// ProgressBar Just contains the data needed to display a progress bar
|
|
|
|
type ProgressBar struct {
|
|
|
|
total int
|
|
|
|
progress int
|
|
|
|
allowOverflow bool
|
|
|
|
allowUnderflow bool
|
|
|
|
fullChar rune
|
|
|
|
emptyChar rune
|
|
|
|
bordered bool
|
|
|
|
alignment TextAlignment
|
2015-12-18 03:41:02 +00:00
|
|
|
colorized bool
|
2015-11-30 14:21:22 +00:00
|
|
|
|
|
|
|
x, y int
|
|
|
|
width, height int
|
|
|
|
bg, fg termbox.Attribute
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateProgressBar Create a progress bar object
|
|
|
|
func CreateProgressBar(tot, x, y int, fg, bg termbox.Attribute) *ProgressBar {
|
|
|
|
i := ProgressBar{total: tot,
|
|
|
|
fullChar: '#', emptyChar: ' ',
|
|
|
|
x: x, y: y, height: 1, width: 10,
|
|
|
|
bordered: true, fg: fg, bg: bg,
|
|
|
|
alignment: AlignLeft,
|
|
|
|
}
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProgress returns the curret progress value
|
|
|
|
func (i *ProgressBar) GetProgress() int {
|
|
|
|
return i.progress
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetProgress sets the current progress of the bar
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetProgress(p int) {
|
2015-11-30 14:21:22 +00:00
|
|
|
if (p <= i.total || i.allowOverflow) || (p >= 0 || i.allowUnderflow) {
|
|
|
|
i.progress = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrProgress increments the current progress of the bar
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) IncrProgress() {
|
2015-11-30 14:21:22 +00:00
|
|
|
if i.progress < i.total || i.allowOverflow {
|
|
|
|
i.progress++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecrProgress decrements the current progress of the bar
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) DecrProgress() {
|
2015-11-30 14:21:22 +00:00
|
|
|
if i.progress > 0 || i.allowUnderflow {
|
|
|
|
i.progress--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPercent returns the percent full of the bar
|
|
|
|
func (i *ProgressBar) GetPercent() int {
|
|
|
|
return int(float64(i.progress) / float64(i.total) * 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableOverflow Tells the progress bar that it can go over the total
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) EnableOverflow() {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.allowOverflow = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableOverflow Tells the progress bar that it can NOT go over the total
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) DisableOverflow() {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.allowOverflow = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableUnderflow Tells the progress bar that it can go below zero
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) EnableUnderflow() {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.allowUnderflow = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableUnderflow Tells the progress bar that it can NOT go below zero
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) DisableUnderflow() {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.allowUnderflow = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFullChar returns the rune used for 'full'
|
|
|
|
func (i *ProgressBar) GetFullChar() rune {
|
|
|
|
return i.fullChar
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFullChar sets the rune used for 'full'
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetFullChar(f rune) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.fullChar = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmptyChar gets the rune used for 'empty'
|
|
|
|
func (i *ProgressBar) GetEmptyChar() rune {
|
|
|
|
return i.emptyChar
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEmptyChar sets the rune used for 'empty'
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetEmptyChar(f rune) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.emptyChar = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetX Return the x position of the Progress Bar
|
|
|
|
func (i *ProgressBar) GetX() int { return i.x }
|
|
|
|
|
|
|
|
// SetX set the x position of the ProgressBar to x
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetX(x int) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.x = x
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetY Return the y position of the ProgressBar
|
|
|
|
func (i *ProgressBar) GetY() int { return i.y }
|
|
|
|
|
|
|
|
// SetY Set the y position of the ProgressBar to y
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetY(y int) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.y = y
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeight returns the height of the progress bar
|
|
|
|
// Defaults to 1 (3 if bordered)
|
|
|
|
func (i *ProgressBar) GetHeight() int {
|
|
|
|
return i.height
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeight Sets the height of the progress bar
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetHeight(h int) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.height = h
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWidth returns the width of the progress bar
|
|
|
|
func (i *ProgressBar) GetWidth() int {
|
|
|
|
return i.width
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWidth Sets the width of the progress bar
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetWidth(w int) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.width = w
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBackground Return the current background color of the modal
|
|
|
|
func (i *ProgressBar) GetBackground() termbox.Attribute { return i.bg }
|
|
|
|
|
|
|
|
// SetBackground Set the current background color to bg
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetBackground(bg termbox.Attribute) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.bg = bg
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetForeground Return the current foreground color
|
|
|
|
func (i *ProgressBar) GetForeground() termbox.Attribute { return i.fg }
|
|
|
|
|
|
|
|
// SetForeground Set the foreground color to fg
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetForeground(fg termbox.Attribute) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.fg = fg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align Tells which direction the progress bar empties
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) Align(a TextAlignment) {
|
2015-11-30 14:21:22 +00:00
|
|
|
i.alignment = a
|
|
|
|
}
|
|
|
|
|
2015-12-18 03:41:02 +00:00
|
|
|
// SetColorized sets whether the progress bar should be colored
|
|
|
|
// depending on how full it is:
|
|
|
|
// 10% - Red
|
|
|
|
// 50% - Yellow
|
|
|
|
// 80% - Green
|
2016-01-17 05:04:01 +00:00
|
|
|
func (i *ProgressBar) SetColorized(c bool) {
|
2015-12-18 03:41:02 +00:00
|
|
|
i.colorized = c
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:21:22 +00:00
|
|
|
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
|
|
|
func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw outputs the input field on the screen
|
|
|
|
func (i *ProgressBar) Draw() {
|
|
|
|
// For now, just draw a [#### ] bar
|
|
|
|
// TODO: make this more advanced
|
2015-12-18 03:41:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 14:21:22 +00:00
|
|
|
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))
|
2015-12-18 03:41:02 +00:00
|
|
|
FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), useFg, i.bg)
|
2015-11-30 14:21:22 +00:00
|
|
|
DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg)
|
|
|
|
|
|
|
|
/*
|
|
|
|
drawX, drawY := i.x, i.y
|
|
|
|
drawWidth, drawHeight := i.width, i.height
|
|
|
|
if i.bordered {
|
|
|
|
if i.height == 1 && i.width > 2 {
|
|
|
|
// Just using [ & ] for the border
|
|
|
|
DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg)
|
|
|
|
DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg)
|
|
|
|
drawX++
|
|
|
|
drawWidth -= 2
|
|
|
|
} else if i.height >= 3 {
|
|
|
|
DrawBorder(drawX, drawY, drawX+i.width, drawY+i.height, i.fg, i.bg)
|
|
|
|
drawX++
|
|
|
|
drawY++
|
|
|
|
drawWidth -= 2
|
|
|
|
drawHeight -= 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out how many chars are full
|
|
|
|
numFull := drawWidth * (i.progress / i.total)
|
|
|
|
switch i.alignment {
|
|
|
|
case AlignRight: // TODO: Fill from right to left
|
|
|
|
case AlignCenter: // TODO: Fill from middle out
|
|
|
|
default: // Fill from left to right
|
|
|
|
FillWithChar(i.fullChar, drawX, drawY, drawX+numFull, drawY+(drawHeight-1), i.fg, i.bg)
|
|
|
|
if numFull < drawWidth {
|
|
|
|
FillWithChar(i.emptyChar, drawX+numFull, drawY, drawX+drawWidth-1, drawY+(drawHeight-1), i.fg, i.bg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|