From 1a6a6438922f6b47f3588c79978a88f94ec31646 Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Thu, 1 Dec 2022 23:13:03 +1100 Subject: [PATCH] Use printf for printing the text bubble MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's easier for my feeble mind to read, and fixes the "negative repeat" error that currently happens randomly ``` ╰─>$ fortune | pokesay -width 20 /----------------------\ panic: strings: negative Repeat count ``` This occurs because of the way the padding was calculated, and happens when the length of the current line and the width are equal This was to deal the the space that println inserts between each argument. Using printf instead removes the need for this workaround and fixes the bug too --- pokesay.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pokesay.go b/pokesay.go index baed5698..ac8d97fb 100644 --- a/pokesay.go +++ b/pokesay.go @@ -51,9 +51,9 @@ func randomInt(n int) int { func printSpeechBubbleLine(line string, width int) { if len(line) > width { - fmt.Println("|", line) + fmt.Printf("| %s\n", line) } else { - fmt.Println("|", line, strings.Repeat(" ", width-len(line)-1), "|") + fmt.Printf("| %s%s |\n", line, strings.Repeat(" ", width-len(line))) } }