package printer import ( "math" "strconv" "strings" "codeflow.dananglin.me.uk/apollo/enbas/internal/model" "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" ) func (p Printer) pollContent(poll model.Poll) string { var builder strings.Builder for ind, option := range poll.Options { var ( votage float64 percentage int ) if poll.VotesCount == 0 { percentage = 0 } else { votage = float64(option.VotesCount) / float64(poll.VotesCount) percentage = int(math.Floor(100 * votage)) } builder.WriteString("\n\n" + "[" + strconv.Itoa(ind) + "] " + option.Title) builder.WriteString(p.pollMeter(votage)) builder.WriteString("\n" + strconv.Itoa(option.VotesCount) + " votes " + "(" + strconv.Itoa(percentage) + "%)") } builder.WriteString("\n\n" + p.fieldFormat("Total votes:") + " " + strconv.Itoa(poll.VotesCount)) builder.WriteString("\n" + p.fieldFormat("Poll ID:") + " " + poll.ID) builder.WriteString("\n" + p.fieldFormat("Poll is open until:") + " " + utilities.FormatTime(poll.ExpiredAt)) return builder.String() } func (p Printer) pollMeter(votage float64) string { numVoteBlocks := int(math.Floor(float64(p.maxTerminalWidth) * votage)) numBackgroundBlocks := p.maxTerminalWidth - numVoteBlocks voteBlockColor := p.theme.boldgreen backgroundBlockColor := p.theme.grey if p.noColor { voteBlockColor = p.theme.reset if numVoteBlocks == 0 { numVoteBlocks = 1 } } meter := "\n" + voteBlockColor + strings.Repeat(p.pollMeterSymbol, numVoteBlocks) + p.theme.reset if !p.noColor { meter += backgroundBlockColor + strings.Repeat(p.pollMeterSymbol, numBackgroundBlocks) + p.theme.reset } return meter }