Browse Source

Updated Bar so cleaner, testable.

It's like box, we have BARS_CP437 and BARS_UNICODE that get
assigned to BARS.  We also test that CP437 chars match the
Unicode ones.
Steve Thielemann 3 years ago
parent
commit
7fde611e7b
3 changed files with 63 additions and 39 deletions
  1. 31 38
      door/bar.go
  2. 30 1
      door/bar_test.go
  3. 2 0
      door/door.go

+ 31 - 38
door/bar.go

@@ -5,6 +5,10 @@ import (
 	"strings"
 )
 
+// TODO:  Break out the progress bar characters into stuctures.
+// Write tests to verify that CP437 matches Unicode.
+// See box_test.  ;)
+
 type BarStyle int8
 
 const (
@@ -36,6 +40,26 @@ type BarLine struct {
 	Line
 }
 
+type BarCharacters struct {
+	solid    string
+	half     [2]string
+	gradient [4]string
+}
+
+var BARS BarCharacters
+
+var BARS_CP437 = BarCharacters{
+	"\xdb",
+	[2]string{"\xdb", "\xdd"},
+	[4]string{"\xdb", "\xb0", "\xb1", "\xb2"},
+}
+
+var BARS_UNICODE = BarCharacters{
+	"\u2588",
+	[2]string{"\u2588", "\u258c"},
+	[4]string{"\u2588", "\u2591", "\u2592", "\u2593"},
+}
+
 func (bl *BarLine) CheckRange() {
 	if len(bl.ColorRange) != 0 {
 		// Ok, there is a color range.  Get checking
@@ -64,11 +88,7 @@ func (bl *BarLine) Output() string {
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent / step_width)
 
-		if Unicode {
-			output += strings.Repeat("\u2588", steps)
-		} else {
-			output += strings.Repeat("\xdb", steps)
-		}
+		output += strings.Repeat(BARS.solid, steps)
 		// This will work, because we aren't trying to len(output) with unicode.
 		output += strings.Repeat(" ", int(bl.Width-steps))
 
@@ -76,17 +96,9 @@ func (bl *BarLine) Output() string {
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent * 2 / step_width)
 
-		if Unicode {
-			output += strings.Repeat("\u2588", steps/2)
-		} else {
-			output += strings.Repeat("\xdb", steps/2)
-		}
+		output += strings.Repeat(BARS.half[0], steps/2)
 		if steps%2 == 1 {
-			if Unicode {
-				output += "\u258c"
-			} else {
-				output += "\xdd"
-			}
+			output += BARS.half[1]
 			steps++
 		}
 		output += strings.Repeat(" ", bl.Width-(steps/2))
@@ -94,31 +106,12 @@ func (bl *BarLine) Output() string {
 	case GRADIENT:
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent * 4 / step_width)
-		if Unicode {
-			output += strings.Repeat("\u2588", steps/4)
-		} else {
-			output += strings.Repeat("\xdb", steps/4)
-		}
+
+		output += strings.Repeat(BARS.gradient[0], steps/4)
 		if steps%4 != 0 {
 			switch steps % 4 {
-			case 1:
-				if Unicode {
-					output += "\u2591"
-				} else {
-					output += "\xb0"
-				}
-			case 2:
-				if Unicode {
-					output += "\u2592"
-				} else {
-					output += "\xb1"
-				}
-			case 3:
-				if Unicode {
-					output += "\u2593"
-				} else {
-					output += "\xb2"
-				}
+			case 1, 2, 3:
+				output += BARS.gradient[steps%4]
 			}
 			for steps%4 != 0 {
 				steps++

+ 30 - 1
door/bar_test.go

@@ -10,11 +10,40 @@ func pctUpdate(pct *int64) func() int64 {
 	}
 }
 
+func TestBarsUnicode(t *testing.T) {
+	// verify that the BARS_CP437 matches BARS_UNICODE
+
+	cp437 := BARS_CP437.solid
+	convert := CP437_to_Unicode(cp437)
+	unicode := BARS_UNICODE.solid
+	if convert != unicode {
+		t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
+	}
+
+	for half := 0; half < 2; half++ {
+		cp437 := BARS_CP437.half[half]
+		convert := CP437_to_Unicode(cp437)
+		unicode := BARS_UNICODE.half[half]
+		if convert != unicode {
+			t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
+		}
+	}
+
+	for grad := 0; grad < 4; grad++ {
+		cp437 := BARS_CP437.gradient[grad]
+		convert := CP437_to_Unicode(cp437)
+		unicode := BARS_UNICODE.gradient[grad]
+		if convert != unicode {
+			t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
+		}
+	}
+}
+
 func TestBars(t *testing.T) {
 
 	// This is normally done by door init, but we're skipping that right now
 	Unicode = true
-	BOXES = BOXES_UNICODE
+	BARS = BARS_UNICODE
 
 	BarColor := ColorText("BLUE")
 

+ 2 - 0
door/door.go

@@ -222,8 +222,10 @@ func (d *Door) Init() {
 	d.detect()
 	if Unicode {
 		BOXES = BOXES_UNICODE
+		BARS = BARS_UNICODE
 	} else {
 		BOXES = BOXES_CP437
+		BARS = BARS_CP437
 	}
 }