fix: support long unicode strings in proposals' names

pull/21/head
Dominique Merle 3 years ago
parent fd70088161
commit dd5d1cbd3a

@ -112,8 +112,11 @@ Example:
Then go into this project directory and run:
go get
go run .
go run . example/example.csv --sort
go build -o mj
./mj
./mj example/example.csv --sort
### Build distributables

@ -6,6 +6,7 @@ That old cider bottle, 6, 2, 5, 10
"The ""Queen"" of Polls", 3, 2, 5, 13
"Sa majesté des mouches ventripotentes", 5, 3, 4, 11
"Le pseudonyme lorem ipsum dolor sit amet et consequitur in girum imus nocte et consumimur igni", 2, 3, 6, 12
"Le pseudonyme lorem ipsum dolo", 2, 3, 6, 12
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
@ -14,11 +15,11 @@ Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Ch@ps, 5, 8, 1, 09
Chips, 5, 8, 0, 10
Chips, 5, 8, 0, 10
Chips, 5, 8, 0, 10
Cider, 3, 2, 5, 13
Æ€&0@À, 5, 8, 0, 10
Cider, 3, 2, 5, 13
Rosé!, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13
Cider, 3, 2, 5, 13

1 candidates reject passable good excellent
6 The "Queen" of Polls 3 2 5 13
7 Sa majesté des mouches ventripotentes 5 3 4 11
8 Le pseudonyme lorem ipsum dolor sit amet et consequitur in girum imus nocte et consumimur igni 2 3 6 12
9 Le pseudonyme lorem ipsum dolo 2 3 6 12
10 Cider 3 2 5 13
11 Cider 3 2 5 13
12 Cider 3 2 5 13
15 Cider 3 2 5 13
16 Cider 3 2 5 13
17 Cider 3 2 5 13
18 Ch@ps 5 8 1 09
19 Chips 5 8 0 10
20 Chips Æ€&0@À 5 8 0 10
Chips 5 8 0 10
Cider 3 2 5 13
21 Cider 3 2 5 13
22 Rosé! 3 2 5 13
23 Cider 3 2 5 13
24 Cider 3 2 5 13
25 Cider 3 2 5 13

@ -23,6 +23,16 @@ type Formatter interface {
) (string, error)
}
// measureStringLength with support for unicode (hopefully)
// Heavy-duty replacement for len(str)
func measureStringLength(str string) int {
count := 0
for range str {
count++
}
return count
}
// truncateString safely truncates a string (hopefully)
// from https://dev.to/takakd/go-safe-truncate-string-9h0
// with some tweaks, like the suffix ; the length includes the suffix
@ -47,3 +57,9 @@ func truncateString(str string, length int, suffix rune) string {
}
return truncated
}
func replaceAtIndex(in string, r rune, i int) string {
out := []rune(in)
out[i] = r
return string(out)
}

@ -45,7 +45,7 @@ func (t *TextFormatter) Format(
amountOfCharactersForProposal := 1
maximumAmountOfCharactersForProposal := 30
for _, proposal := range proposals {
thatProposalLength := len(proposal)
thatProposalLength := measureStringLength(proposal)
if thatProposalLength > amountOfCharactersForProposal {
amountOfCharactersForProposal = thatProposalLength
}
@ -65,7 +65,7 @@ func (t *TextFormatter) Format(
truncateString(proposals[proposalResult.Index], amountOfCharactersForProposal, '…'),
)
remainingWidth := expectedWidth - len(line)
remainingWidth := expectedWidth - measureStringLength(line)
line += makeAsciiMeritProfile(
pollTally.Proposals[proposalResult.Index],
@ -131,9 +131,3 @@ func getCharForGrade(gradeIndex int) string {
gradeIndex = gradeIndex % len(chars)
return chars[gradeIndex : gradeIndex+1]
}
func replaceAtIndex(in string, r rune, i int) string {
out := []rune(in)
out[i] = r
return string(out)
}

Loading…
Cancel
Save