From a5e1c4ac7904019234967dd8fa91554eba72ce80 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 12 Jun 2024 14:25:34 +0100 Subject: [PATCH] draw poll meter with no colour when required --- internal/model/poll.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/internal/model/poll.go b/internal/model/poll.go index 19cb921..b1c9bec 100644 --- a/internal/model/poll.go +++ b/internal/model/poll.go @@ -80,7 +80,7 @@ func displayPollContent(writer io.StringWriter, poll Poll, noColor bool, indent } writer.WriteString("\n\n" + indent + "[" + strconv.Itoa(ind) + "] " + option.Title) - drawPollMeter(writer, calculate, 80, indent) + drawPollMeter(writer, noColor, calculate, 80, indent) writer.WriteString( "\n" + indent + strconv.Itoa(option.VotesCount) + " votes " + @@ -96,15 +96,24 @@ func displayPollContent(writer io.StringWriter, poll Poll, noColor bool, indent ) } -func drawPollMeter(writer io.StringWriter, calculated float64, limit int, indent string) { - colouredBlocks := int(math.Floor(float64(limit) * calculated)) - backgroundBlocks := limit - colouredBlocks +func drawPollMeter(writer io.StringWriter, noColor bool, calculated float64, limit int, indent string) { + numVoteBlocks := int(math.Floor(float64(limit) * calculated)) + numBackgroundBlocks := limit - numVoteBlocks blockChar := "\u2501" + voteBlockColor := "\033[32;1m" + backgroundBlockColor := "\033[90m" - writer.WriteString( - "\n" + - indent + - "\033[32m" + strings.Repeat(blockChar, colouredBlocks) + "\033[0m" + - strings.Repeat(blockChar, backgroundBlocks), - ) + if noColor { + voteBlockColor = "\033[0m" + + if numVoteBlocks == 0 { + numVoteBlocks = 1 + } + } + + writer.WriteString("\n" + indent + voteBlockColor + strings.Repeat(blockChar, numVoteBlocks) + "\033[0m") + + if !noColor { + writer.WriteString(backgroundBlockColor + strings.Repeat(blockChar, numBackgroundBlocks) + "\033[0m") + } }