checkpoint: printer prints full poll info
This commit is contained in:
parent
09cd13a2f7
commit
02e96bd528
4 changed files with 34 additions and 100 deletions
|
@ -381,7 +381,7 @@ func (s *ShowExecutor) showPoll(gtsClient *client.Client) error {
|
||||||
return fmt.Errorf("unable to retrieve the poll: %w", err)
|
return fmt.Errorf("unable to retrieve the poll: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
utilities.Display(poll, false, "")
|
s.printer.PrintPoll(poll)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Poll struct {
|
type Poll struct {
|
||||||
|
@ -31,93 +25,3 @@ type PollOption struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
VotesCount int `json:"votes_count"`
|
VotesCount int `json:"votes_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Poll) Display(noColor bool) string {
|
|
||||||
var builder strings.Builder
|
|
||||||
|
|
||||||
indent := " "
|
|
||||||
|
|
||||||
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) +
|
|
||||||
"\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:"))
|
|
||||||
|
|
||||||
for _, vote := range p.OwnVotes {
|
|
||||||
builder.WriteString("\n" + indent + "[" + strconv.Itoa(vote) + "] " + p.Options[vote].Title)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.WriteString(
|
|
||||||
"\n\n" +
|
|
||||||
utilities.HeaderFormat(noColor, "EXPIRED:") +
|
|
||||||
"\n" + indent + strconv.FormatBool(p.Expired),
|
|
||||||
)
|
|
||||||
|
|
||||||
return builder.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 = float64(option.VotesCount) / float64(poll.VotesCount)
|
|
||||||
percentage = int(math.Floor(100 * calculate))
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.WriteString("\n\n" + indent + "[" + strconv.Itoa(ind) + "] " + option.Title)
|
|
||||||
drawPollMeter(writer, noColor, calculate, 80, indent)
|
|
||||||
|
|
||||||
writer.WriteString(
|
|
||||||
"\n" + indent + strconv.Itoa(option.VotesCount) + " votes " +
|
|
||||||
"(" + strconv.Itoa(percentage) + "%)",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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, 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"
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,7 +9,37 @@ import (
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p Printer) pollContent(poll model.Poll) string {
|
func (p Printer) PrintPoll(poll model.Poll) {
|
||||||
|
var builder strings.Builder
|
||||||
|
|
||||||
|
builder.WriteString("\n" + p.headerFormat("POLL ID:"))
|
||||||
|
builder.WriteString("\n" + poll.ID)
|
||||||
|
|
||||||
|
builder.WriteString("\n\n" + p.headerFormat("OPTIONS:"))
|
||||||
|
builder.WriteString(p.pollOptions(poll))
|
||||||
|
|
||||||
|
builder.WriteString("\n\n" + p.headerFormat("MULTIPLE CHOICES ALLOWED:"))
|
||||||
|
builder.WriteString("\n" + strconv.FormatBool(poll.Multiple))
|
||||||
|
|
||||||
|
builder.WriteString("\n\n" + p.headerFormat("YOU VOTED:"))
|
||||||
|
builder.WriteString("\n" + strconv.FormatBool(poll.Voted))
|
||||||
|
|
||||||
|
if len(poll.OwnVotes) > 0 {
|
||||||
|
builder.WriteString("\n\n" + p.headerFormat("YOUR VOTES:"))
|
||||||
|
|
||||||
|
for _, vote := range poll.OwnVotes {
|
||||||
|
builder.WriteString("\n" + "[" + strconv.Itoa(vote) + "] " + poll.Options[vote].Title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.WriteString("\n\n" + p.headerFormat("EXPIRED:"))
|
||||||
|
builder.WriteString("\n" + strconv.FormatBool(poll.Expired))
|
||||||
|
builder.WriteString("\n\n")
|
||||||
|
|
||||||
|
p.print(builder.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Printer) pollOptions(poll model.Poll) string {
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
|
|
||||||
for ind, option := range poll.Options {
|
for ind, option := range poll.Options {
|
||||||
|
|
|
@ -20,7 +20,7 @@ func (p Printer) PrintStatus(status model.Status) {
|
||||||
|
|
||||||
// If a poll exists in a status, write the contents to the builder.
|
// If a poll exists in a status, write the contents to the builder.
|
||||||
if status.Poll != nil {
|
if status.Poll != nil {
|
||||||
builder.WriteString(p.pollContent(*status.Poll))
|
builder.WriteString(p.pollOptions(*status.Poll))
|
||||||
}
|
}
|
||||||
|
|
||||||
// The ID of the status
|
// The ID of the status
|
||||||
|
@ -76,7 +76,7 @@ func (p Printer) PrintStatusList(list model.StatusList) {
|
||||||
builder.WriteString(utilities.WrapLines(utilities.ConvertHTMLToText(status.Content), "\n", p.maxTerminalWidth))
|
builder.WriteString(utilities.WrapLines(utilities.ConvertHTMLToText(status.Content), "\n", p.maxTerminalWidth))
|
||||||
|
|
||||||
if status.Poll != nil {
|
if status.Poll != nil {
|
||||||
builder.WriteString(p.pollContent(*status.Poll))
|
builder.WriteString(p.pollOptions(*status.Poll))
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.WriteString(
|
builder.WriteString(
|
||||||
|
|
Loading…
Reference in a new issue