Skip to content

Commit

Permalink
feat: display the merit profiles as ASCII art
Browse files Browse the repository at this point in the history
  • Loading branch information
domi41 committed Oct 19, 2021
1 parent 1175720 commit 9c50248
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,13 @@ multiply them beforehand by a big factor like 1 000 000 000.
os.Exit(3)
}

desiredWidth, widthErr := strconv.Atoi(cmd.Flags().Lookup("width").Value.String())
if widthErr != nil || desiredWidth < 0 {
desiredWidth = 79
}
options := &formatter.Options{
Sorted: bool(cmd.Flags().Lookup("sort").Changed),
Width: desiredWidth,
}

out, formatterErr := outputFormatter.Format(
Expand Down Expand Up @@ -233,6 +238,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.majority-judgment-cli.yaml)")
//rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.Flags().StringP("format", "f", "text", "desired format of the output")
rootCmd.Flags().StringP("width", "w", "79", "desired width, in characters")
//rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
//rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")

Expand Down
1 change: 1 addition & 0 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/mieuxvoter/majority-judgment-library-go/judgment"

type Options struct {
Sorted bool
Width int
}

type Formatter interface {
Expand Down
58 changes: 54 additions & 4 deletions formatter/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package formatter
import (
"fmt"
"github.com/mieuxvoter/majority-judgment-library-go/judgment"
"math"
"strconv"
"strings"
)
Expand All @@ -18,6 +19,11 @@ func (t *TextFormatter) Format(
) (string, error) {
out := ""

expectedWidth := options.Width
if 0 >= expectedWidth {
expectedWidth = 79
}

proposalsResults := result.Proposals
if options.Sorted {
proposalsResults = result.ProposalsSorted
Expand All @@ -44,17 +50,25 @@ func (t *TextFormatter) Format(
}

for _, proposalResult := range proposalsResults {
out += fmt.Sprintf(
line := ""
line += fmt.Sprintf(
"#%0"+strconv.Itoa(amountOfDigitsForRank)+"d ",
proposalResult.Rank,
)
out += fmt.Sprintf(
" %*s",
line += fmt.Sprintf(
" %*s ",
amountOfCharactersForProposal,
proposals[proposalResult.Index],
)

out += "\n"
remainingWidth := expectedWidth - len(line)

line += makeAsciiMeritProfile(
pollTally.Proposals[proposalResult.Index],
remainingWidth,
)

out += line + "\n"
}

return strings.TrimSpace(out), nil
Expand All @@ -68,3 +82,39 @@ func countDigits(i int) (count int) {

return
}

func makeAsciiMeritProfile(
tally *judgment.ProposalTally,
width int,
) (ascii string) {
if width < 3 {
width = 3
}
amountOfJudges := float64(tally.CountJudgments())
for gradeIndex, gradeTallyInt := range tally.Tally {
gradeTally := float64(gradeTallyInt)
gradeRune := strconv.Itoa(gradeIndex)
ascii += strings.Repeat(
gradeRune,
int(math.Round(float64(width)*gradeTally/amountOfJudges)),
)
}

for len(ascii) < width {
ascii += ascii[len(ascii)-1:]
}

for len(ascii) > width {
ascii = ascii[0 : len(ascii)-1]
}

ascii = replaceAtIndex(ascii, '|', width/2)

return
}

func replaceAtIndex(in string, r rune, i int) string {
out := []rune(in)
out[i] = r
return string(out)
}

0 comments on commit 9c50248

Please sign in to comment.