Bladeren bron

Strings must be the right len for Box.Middle.

Steve Thielemann 3 jaren geleden
bovenliggende
commit
7c619e9661
2 gewijzigde bestanden met toevoegingen van 5 en 2 verwijderingen
  1. 1 1
      door/door.go
  2. 4 1
      testdoor/testdoor.go

+ 1 - 1
door/door.go

@@ -202,7 +202,7 @@ func (b *Box) Middle(text string) string {
 	} else {
 		style = &boxes[b.Style]
 	}
-	return style.side + text + strings.Repeat(" ", b.Width-len(text)) + style.side
+	return style.side + text + style.side
 }
 
 func (b *Box) Bottom() string {

+ 4 - 1
testdoor/testdoor.go

@@ -19,7 +19,10 @@ func main() {
 	d.Write(message)
 	b := door.Box{20, 1}
 	d.Write(b.Top() + door.CRNL)
-	d.Write(b.Middle("SHAZAM!") + door.CRNL)
+	// or %-20s
+	message = fmt.Sprintf("%20s", "SHAZAM!")
+
+	d.Write(b.Middle(message) + door.CRNL)
 	d.Write(b.Bottom() + door.CRNL)
 
 	d.Write("Returning you to the BBS..." + door.CRNL)