|
@@ -5,13 +5,19 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-type BarStyle int
|
|
|
+type BarStyle int8
|
|
|
|
|
|
const (
|
|
|
SOLID BarStyle = iota
|
|
|
HALF_STEP
|
|
|
GRADIENT
|
|
|
- PERCENTAGE
|
|
|
+)
|
|
|
+
|
|
|
+type Percent_Style int8
|
|
|
+
|
|
|
+const (
|
|
|
+ PERCENT_NONE Percent_Style = iota
|
|
|
+ PERCENT
|
|
|
PERCENT_SPACE
|
|
|
)
|
|
|
|
|
@@ -21,11 +27,12 @@ type BarRange struct {
|
|
|
}
|
|
|
|
|
|
type BarLine struct {
|
|
|
- Width int
|
|
|
- Style BarStyle
|
|
|
- Percent int64 // percentage * 100
|
|
|
- ColorRange []BarRange
|
|
|
- UpdateP func() int64
|
|
|
+ Width int
|
|
|
+ Style BarStyle
|
|
|
+ Percent int64 // percentage * 100
|
|
|
+ PercentStyle Percent_Style
|
|
|
+ ColorRange []BarRange
|
|
|
+ UpdateP func() int64
|
|
|
Line
|
|
|
}
|
|
|
|
|
@@ -53,7 +60,7 @@ func (bl *BarLine) Output() string {
|
|
|
bl.CheckRange()
|
|
|
|
|
|
switch bl.Style {
|
|
|
- case SOLID, PERCENTAGE, PERCENT_SPACE:
|
|
|
+ case SOLID:
|
|
|
step_width = int64(100 * 100 / bl.Width)
|
|
|
var steps int = int(bl.Percent / step_width)
|
|
|
|
|
@@ -65,35 +72,6 @@ func (bl *BarLine) Output() string {
|
|
|
// This will work, because we aren't trying to len(output) with unicode.
|
|
|
output += strings.Repeat(" ", int(bl.Width-steps))
|
|
|
|
|
|
- if bl.Style == PERCENTAGE || bl.Style == PERCENT_SPACE {
|
|
|
- percent := fmt.Sprintf("%d", bl.Percent/100)
|
|
|
-
|
|
|
- var pos int = bl.Width/2 - 1
|
|
|
- if percent != "100" {
|
|
|
- percent += "%"
|
|
|
- if len(percent) < 3 {
|
|
|
- percent = " " + percent
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if bl.Style == PERCENT_SPACE {
|
|
|
- percent = " " + percent + " "
|
|
|
- pos--
|
|
|
- }
|
|
|
- // to process/slice the string (with unicode) do this:
|
|
|
- // convert to []rune, slice that, convert back to string
|
|
|
- //
|
|
|
- // sliceable := []rune(output)
|
|
|
- // newString := string(sliceable[:5]) + " new content " + (sliceable[10:])
|
|
|
- // fmt.Printf("%d %d [%s] %d [%s]\n", bl.Width, pos, percent, len(output), output)
|
|
|
-
|
|
|
- if Unicode {
|
|
|
- runes := []rune(output)
|
|
|
- output = string(runes[:pos]) + percent + string(runes[pos+len(percent):])
|
|
|
- } else {
|
|
|
- output = output[:pos] + percent + output[pos+len(percent):]
|
|
|
- }
|
|
|
- }
|
|
|
case HALF_STEP:
|
|
|
step_width = int64(100 * 100 / bl.Width)
|
|
|
var steps int = int(bl.Percent * 2 / step_width)
|
|
@@ -149,5 +127,36 @@ func (bl *BarLine) Output() string {
|
|
|
output += strings.Repeat(" ", bl.Width-(steps/4))
|
|
|
}
|
|
|
|
|
|
+ if bl.PercentStyle != PERCENT_NONE {
|
|
|
+ percent := fmt.Sprintf("%d", bl.Percent/100)
|
|
|
+
|
|
|
+ var pos int = bl.Width/2 - 1
|
|
|
+ if percent != "100" {
|
|
|
+ percent += "%"
|
|
|
+ if len(percent) < 3 {
|
|
|
+ percent = " " + percent
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if bl.PercentStyle == PERCENT_SPACE {
|
|
|
+ percent = " " + percent + " "
|
|
|
+ pos--
|
|
|
+ }
|
|
|
+
|
|
|
+ // to process/slice the string (with unicode) do this:
|
|
|
+ // convert to []rune, slice that, convert back to string
|
|
|
+ //
|
|
|
+ // sliceable := []rune(output)
|
|
|
+ // newString := string(sliceable[:5]) + " new content " + (sliceable[10:])
|
|
|
+ // fmt.Printf("%d %d [%s] %d [%s]\n", bl.Width, pos, percent, len(output), output)
|
|
|
+
|
|
|
+ if Unicode {
|
|
|
+ runes := []rune(output)
|
|
|
+ output = string(runes[:pos]) + percent + string(runes[pos+len(percent):])
|
|
|
+ } else {
|
|
|
+ output = output[:pos] + percent + output[pos+len(percent):]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return bl.DefaultColor + output
|
|
|
}
|