Przeglądaj źródła

Changed box/bar chars to *.

Steve Thielemann 1 rok temu
rodzic
commit
3573b08fa0
8 zmienionych plików z 164 dodań i 26 usunięć
  1. 2 2
      door/bar.go
  2. 2 2
      door/bar_test.go
  3. 1 1
      door/benchline.sh
  4. 2 2
      door/box.go
  5. 1 1
      door/box_test.go
  6. 4 4
      door/door.go
  7. 39 13
      door/line.go
  8. 113 1
      door/line_test.go

+ 2 - 2
door/bar.go

@@ -47,8 +47,6 @@ type BarCharacters struct {
 	Gradient [4]string
 }
 
-var BARS BarCharacters
-
 var BARS_CP437 = BarCharacters{
 	"\xdb",
 	[2]string{"\xdb", "\xdd"},
@@ -61,6 +59,8 @@ var BARS_UNICODE = BarCharacters{
 	[4]string{"\u2588", "\u2591", "\u2592", "\u2593"},
 }
 
+var BARS *BarCharacters = &BARS_CP437
+
 func (bl *BarLine) CheckRange() {
 	if len(bl.ColorRange) != 0 {
 		// Ok, there is a color range.  Get checking

+ 2 - 2
door/bar_test.go

@@ -45,7 +45,7 @@ func TestBarsUnicode(t *testing.T) {
 	// Init as if we're in Unicode mode
 	Unicode = true
 	CP437 = false
-	BARS = BARS_UNICODE
+	BARS = &BARS_UNICODE
 	testBars(t)
 }
 
@@ -53,7 +53,7 @@ func TestBarsCP437(t *testing.T) {
 	// Init as if we're in CP437 mode
 	Unicode = false
 	CP437 = true
-	BARS = BARS_CP437
+	BARS = &BARS_CP437
 	testBars(t)
 }
 

+ 1 - 1
door/benchline.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-go test -bench=BenchmarkLine -benchmem -memprofile memory.out -cpuprofile cpu.out
+go test -bench=BenchmarkLine -benchmem -memprofilerate=1 -memprofile memory.out -cpuprofile cpu.out
 echo go tool pprof memory.out
 echo go tool pprof cpu.out
 

+ 2 - 2
door/box.go

@@ -18,8 +18,6 @@ type BoxStyle struct {
 // BOXES_CP437 or BOXES_UNICODE.  That'll save us from having
 // to if Unicode when building boxes.  ;)
 
-var BOXES [4]BoxStyle
-
 var BOXES_CP437 = [4]BoxStyle{
 	/*
 		┌──┐
@@ -58,6 +56,8 @@ var BOXES_UNICODE = [4]BoxStyle{
 	BoxStyle{"\u2553", "\u2556", "\u2500", "\u2551", "\u2559", "\u255c", "\u255f", "\u2562"},
 }
 
+var BOXES *[4]BoxStyle = &BOXES_CP437
+
 type Box struct {
 	Width int
 	Style int

+ 1 - 1
door/box_test.go

@@ -31,7 +31,7 @@ func TestBoxUnicode(t *testing.T) {
 func TestBoxes(t *testing.T) {
 	// Normally done in door.init.
 	Unicode = true
-	BOXES = BOXES_UNICODE
+	BOXES = &BOXES_UNICODE
 
 	box := Box{5, 0}
 	got := box.Top()

+ 4 - 4
door/door.go

@@ -396,11 +396,11 @@ func (d *Door) Init(doorname string) {
 
 	d.Detect()
 	if Unicode {
-		BOXES = BOXES_UNICODE
-		BARS = BARS_UNICODE
+		BOXES = &BOXES_UNICODE
+		BARS = &BARS_UNICODE
 	} else {
-		BOXES = BOXES_CP437
-		BARS = BARS_CP437
+		BOXES = &BOXES_CP437
+		BARS = &BARS_CP437
 	}
 }
 

+ 39 - 13
door/line.go

@@ -215,22 +215,48 @@ func RenderUppercase(Upper string, NonUpper string) ColorRender {
 		NonUpperColor = []byte(ColorText(NonUpper))
 	}
 
-	return func(output *bytes.Buffer, text []byte) {
-		var lastColor *[]byte
-		output.Reset()
-		for _, letter := range text {
-			if unicode.IsUpper(rune(letter)) {
-				if lastColor != &UpperColor {
-					output.Write(UpperColor)
-					lastColor = &UpperColor
+	if Unicode {
+		var runeBuffer *bytes.Buffer = &bytes.Buffer{}
+
+		return func(output *bytes.Buffer, text []byte) {
+			var lastColor *[]byte
+			output.Reset()
+			runeBuffer.Reset()
+			runeBuffer.Write(text)
+			for r, _, err := runeBuffer.ReadRune(); err != nil; r, _, err = runeBuffer.ReadRune() {
+				// for _, letter := range text {
+				if unicode.IsUpper(r) {
+					if lastColor != &UpperColor {
+						output.Write(UpperColor)
+						lastColor = &UpperColor
+					}
+				} else {
+					if lastColor != &NonUpperColor {
+						output.Write(NonUpperColor)
+						lastColor = &UpperColor
+					}
 				}
-			} else {
-				if lastColor != &NonUpperColor {
-					output.Write(NonUpperColor)
-					lastColor = &UpperColor
+				output.WriteRune(r)
+			}
+		}
+	} else {
+		return func(output *bytes.Buffer, text []byte) {
+			var lastColor *[]byte
+			output.Reset()
+			for _, letter := range text {
+				if unicode.IsUpper(rune(letter)) {
+					if lastColor != &UpperColor {
+						output.Write(UpperColor)
+						lastColor = &UpperColor
+					}
+				} else {
+					if lastColor != &NonUpperColor {
+						output.Write(NonUpperColor)
+						lastColor = &UpperColor
+					}
 				}
+				output.WriteByte(letter)
 			}
-			output.WriteByte(letter)
 		}
 	}
 }

+ 113 - 1
door/line_test.go

@@ -137,6 +137,18 @@ func BenchmarkLine(b *testing.B) {
 		line.Output()
 	}
 }
+
+func BenchmarkLineUnicode(b *testing.B) {
+	Unicode = true
+
+	for n := 0; n < b.N; n++ {
+		var lineBuff *bytes.Buffer = &bytes.Buffer{}
+		fmt.Fprintf(lineBuff, "Line %d of %d", n, b.N)
+		// lineBuff.WriteString(fmt.Sprintf("Line %d of %d", n, b.N))
+		var line *Line = &Line{Text: lineBuff}
+		line.Output()
+	}
+}
 func BenchmarkLineColor(b *testing.B) {
 	Unicode = false
 	color := ColorText("BRI WHI ON BLUE")
@@ -204,7 +216,7 @@ func BenchmarkLineUpdate(b *testing.B) {
 			u.WriteString(" of ")
 			printd(int64(b.N), u)
 		*/
-		fmt.Fprintf(u, "Line %d of %d", n, b.N)
+		fmt.Fprintf(u, "Line # %d of %d", n, b.N)
 	}
 	line.Update()
 
@@ -214,6 +226,29 @@ func BenchmarkLineUpdate(b *testing.B) {
 	}
 }
 
+func BenchmarkLineUpdateUnicode(b *testing.B) {
+	Unicode = true
+	var line *Line = &Line{}
+	var n int
+
+	line.UpdateF = func(u *bytes.Buffer) {
+		u.Reset()
+		/*
+			u.WriteString("Line ")
+			printd(int64(n), u)
+			u.WriteString(" of ")
+			printd(int64(b.N), u)
+		*/
+		// № \u2116
+		fmt.Fprintf(u, "Line № %d of %d", n, b.N)
+	}
+	line.Update()
+
+	for n = 0; n < b.N; n++ {
+		line.Update()
+		line.Output()
+	}
+}
 func BenchmarkLineRender(b *testing.B) {
 	Unicode = false
 	var rf ColorRender = RenderUppercase("RED", "GREEN")
@@ -226,6 +261,19 @@ func BenchmarkLineRender(b *testing.B) {
 	}
 }
 
+func BenchmarkLineRenderUnicode(b *testing.B) {
+	Unicode = true
+	var rf ColorRender = RenderUppercase("RED", "GREEN")
+	// ₿ /u208f
+	var line *Line = NewLine("ThIs Is CrAzY ₿ TeXt HeRe")
+	line.RenderF = rf
+	var n int
+
+	for n = 0; n < b.N; n++ {
+		line.Output()
+	}
+}
+
 // Benchmarks / profiling
 
 // BenchmarkLineColor-4 2868162 403.9 ns/op 8 B/op 0 allocs/op
@@ -233,7 +281,71 @@ func BenchmarkLineRender(b *testing.B) {
 // No change making Color strings to []byte. (reverted change)
 
 func BenchmarkLineRenderUpdate(b *testing.B) {
+	Unicode = false
+	var Up = ColorText("BLUE")
+	var Down = ColorText("BOLD BLUE")
+	var Num = ColorText("BRI GREEN")
+	var Sym = ColorText("CYAN")
+
+	var render = func(output *bytes.Buffer, text []byte) {
+		output.Reset()
+		var last *string
+		// var r Render = Render{Line: text}
+
+		for _, letter := range text {
+			if unicode.IsUpper(rune(letter)) {
+				if last != &Up {
+					output.WriteString(Up)
+					last = &Up
+				}
+				// r.Append(Up, 1)
+			} else if unicode.IsLower(rune(letter)) {
+				if last != &Down {
+					output.WriteString(Down)
+					last = &Down
+				}
+				// r.Append(Down, 1)
+			} else if unicode.IsDigit(rune(letter)) {
+				if last != &Num {
+					output.WriteString(Num)
+					last = &Num
+				}
+				// r.Append(Num, 1)
+			} else {
+				if last != &Sym {
+					output.WriteString(Sym)
+					last = &Sym
+				}
+				//r.Append(Sym, 1)
+			}
+			output.WriteByte(letter)
+			// output.WriteString(string(letter))
+		}
+		// return output.Bytes()
+		// return r.Result
+	}
+
+	var up int
+	var updater = func(u *bytes.Buffer) {
+		u.Reset()
+		fmt.Fprintf(u, "The Value: %d", up)
+		// u.WriteString("The Value: ")
+		// u.WriteString(strconv.Itoa(up))
+		up++
+		// return fmt.Sprintf("The Value: %d", up)
+	}
+	var line *Line = &Line{UpdateF: updater,
+		RenderF: render,
+		Width:   18}
 
+	for i := 0; i < b.N; i++ {
+		line.Update()
+		line.Output()
+	}
+}
+
+func BenchmarkLineRenderUpdateUnicode(b *testing.B) {
+	Unicode = true
 	var Up = ColorText("BLUE")
 	var Down = ColorText("BOLD BLUE")
 	var Num = ColorText("BRI GREEN")