From 04ebc4e5bd98d0cb850491f3c0ddb6194c0106ac Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 11 Jun 2024 23:31:55 +0100 Subject: [PATCH] checkpoint: draw the poll meter --- internal/model/poll.go | 59 ++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/internal/model/poll.go b/internal/model/poll.go index aa1c40d..19cb921 100644 --- a/internal/model/poll.go +++ b/internal/model/poll.go @@ -33,11 +33,22 @@ func (p Poll) Display(noColor bool) string { indent := " " - builder.WriteString(utilities.HeaderFormat(noColor, "POLL ID:") + "\n" + indent + p.ID) - builder.WriteString("\n\n" + utilities.HeaderFormat(noColor, "OPTIONS:")) + builder.WriteString( + utilities.HeaderFormat(noColor, "POLL ID:") + + "\n" + indent + p.ID + + "\n\n" + utilities.HeaderFormat(noColor, "OPTIONS:"), + ) + displayPollContent(&builder, p, noColor, indent) - builder.WriteString("\n\n" + utilities.HeaderFormat(noColor, "MULTIPLE CHOICES ALLOWED:") + "\n" + indent + strconv.FormatBool(p.Multiple)) - builder.WriteString("\n\n" + utilities.HeaderFormat(noColor, "YOU VOTED:") + "\n" + indent + strconv.FormatBool(p.Voted)) + + builder.WriteString( + "\n\n" + + utilities.HeaderFormat(noColor, "MULTIPLE CHOICES ALLOWED:") + + "\n" + indent + strconv.FormatBool(p.Multiple) + + "\n\n" + + utilities.HeaderFormat(noColor, "YOU VOTED:") + + "\n" + indent + strconv.FormatBool(p.Voted), + ) if len(p.OwnVotes) > 0 { builder.WriteString("\n\n" + utilities.HeaderFormat(noColor, "YOUR VOTES:")) @@ -47,7 +58,11 @@ func (p Poll) Display(noColor bool) string { } } - builder.WriteString("\n\n" + utilities.HeaderFormat(noColor, "EXPIRED:") + "\n" + indent + strconv.FormatBool(p.Expired)) + builder.WriteString( + "\n\n" + + utilities.HeaderFormat(noColor, "EXPIRED:") + + "\n" + indent + strconv.FormatBool(p.Expired), + ) return builder.String() } @@ -55,23 +70,41 @@ func (p Poll) Display(noColor bool) string { func displayPollContent(writer io.StringWriter, poll Poll, noColor bool, indent string) { for ind, option := range poll.Options { var percentage int + var calculate float64 if poll.VotesCount == 0 { percentage = 0 } else { - calculate := math.Floor(100 * (float64(option.VotesCount) / float64(poll.VotesCount))) - percentage = int(calculate) + calculate = float64(option.VotesCount) / float64(poll.VotesCount) + percentage = int(math.Floor(100 * calculate)) } + writer.WriteString("\n\n" + indent + "[" + strconv.Itoa(ind) + "] " + option.Title) + drawPollMeter(writer, calculate, 80, indent) + writer.WriteString( - "\n\n" + indent + "[" + strconv.Itoa(ind) + "] " + option.Title + - "\n" + indent + strings.Repeat("\u2584", 80) + - "\n" + indent + strconv.Itoa(option.VotesCount) + " votes " + + "\n" + indent + strconv.Itoa(option.VotesCount) + " votes " + "(" + strconv.Itoa(percentage) + "%)", ) } - writer.WriteString("\n\n" + indent + utilities.FieldFormat(noColor, "Total votes:") + " " + strconv.Itoa(poll.VotesCount)) - writer.WriteString("\n" + indent + utilities.FieldFormat(noColor, "Poll ID:") + " " + poll.ID) - writer.WriteString("\n" + indent + utilities.FieldFormat(noColor, "Poll is open until:") + " " + utilities.FormatTime(poll.ExpiredAt)) + writer.WriteString( + "\n\n" + + indent + utilities.FieldFormat(noColor, "Total votes:") + " " + strconv.Itoa(poll.VotesCount) + + "\n" + indent + utilities.FieldFormat(noColor, "Poll ID:") + " " + poll.ID + + "\n" + indent + utilities.FieldFormat(noColor, "Poll is open until:") + " " + utilities.FormatTime(poll.ExpiredAt), + ) +} + +func drawPollMeter(writer io.StringWriter, calculated float64, limit int, indent string) { + colouredBlocks := int(math.Floor(float64(limit) * calculated)) + backgroundBlocks := limit - colouredBlocks + blockChar := "\u2501" + + writer.WriteString( + "\n" + + indent + + "\033[32m" + strings.Repeat(blockChar, colouredBlocks) + "\033[0m" + + strings.Repeat(blockChar, backgroundBlocks), + ) }