Browse Source

Updated Bar to allow percentage on any style.

Steve Thielemann 3 years ago
parent
commit
375cbc72ce
3 changed files with 59 additions and 49 deletions
  1. 46 37
      door/bar.go
  2. 12 11
      door/bar_test.go
  3. 1 1
      testdoor/testdoor.go

+ 46 - 37
door/bar.go

@@ -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
 }

+ 12 - 11
door/bar_test.go

@@ -54,17 +54,18 @@ func TestBarHalfStep(t *testing.T) {
 	}
 
 	BarColor = ColorText("BLA ON WHI")
-	bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP}
-	BarHalf := map[int]string{0: BarColor + "          ",
-		5:   BarColor + "\u258c         ",
-		6:   BarColor + "\u258c         ",
-		10:  BarColor + "\u2588         ",
-		20:  BarColor + "\u2588\u2588        ",
-		25:  BarColor + "\u2588\u2588\u258c       ",
-		26:  BarColor + "\u2588\u2588\u258c       ",
-		50:  BarColor + "\u2588\u2588\u2588\u2588\u2588     ",
-		75:  BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258c  ",
-		100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
+	bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
+	BarHalf := map[int]string{0: BarColor + "     0%   ",
+		5:   BarColor + "\u258c    5%   ",
+		6:   BarColor + "\u258c    6%   ",
+		10:  BarColor + "\u2588   10%   ",
+		20:  BarColor + "\u2588\u2588  20%   ",
+		25:  BarColor + "\u2588\u2588\u258c 25%   ",
+		26:  BarColor + "\u2588\u2588\u258c 26%   ",
+		50:  BarColor + "\u2588\u2588\u2588 50%   ",
+		75:  BarColor + "\u2588\u2588\u2588 75%   ",
+		90:  BarColor + "\u2588\u2588\u2588 90% \u2588 ",
+		100: BarColor + "\u2588\u2588\u2588 100 \u2588\u2588",
 	}
 
 	for pct, text := range BarHalf {

+ 1 - 1
testdoor/testdoor.go

@@ -297,7 +297,7 @@ func progress_bars(d *door.Door) {
 	d.Write(door.Clrscr)
 
 	bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
-	bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
+	bar2 := door.BarLine{Width: 30, Style: door.SOLID, PercentStyle: door.PERCENT_SPACE}
 
 	bar2.ColorRange = []door.BarRange{
 		{2500, door.ColorText("RED")},