Created
August 11, 2020 20:53
-
-
Save ghostsquad/de3db99e304a5e5854d2fa767a76cdae to your computer and use it in GitHub Desktop.
emoji and character alignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"unicode/utf8" | |
"github.com/mattn/go-runewidth" | |
"github.com/rivo/uniseg" | |
) | |
func valueDiagnostics(val string, leftpad, rightpad int) { | |
fmt.Printf("%15s: %d\n", "rune width", runewidth.StringWidth(val)) | |
fmt.Printf("%15s: %d\n", "rune count", utf8.RuneCountInString(val)) | |
fmt.Printf("%15s: %d\n", "len", len(val)) | |
fmt.Printf("%15s: %d\n", "grapheme ct", uniseg.GraphemeClusterCount(val)) | |
fmt.Printf("%15s: %d\n", "req left pad", leftpad) | |
fmt.Printf("%15s: %d\n", "req right pad", rightpad) | |
fmt.Printf("[%*s%sAAA]\n", leftpad, val, strings.Repeat(" ", rightpad)) | |
fmt.Println(valuePaddingPredictor(val)) | |
} | |
// aligns to 5 characters | |
func valuePaddingPredictor(val string) string { | |
runeWidth := runewidth.StringWidth(val) | |
// runeCount := utf8.RuneCountInString(val) | |
stringLen := len(val) | |
leftPad := 3 | |
rightPad := 1 | |
if runeWidth == 1 { | |
leftPad++ | |
} | |
if stringLen > 4 { | |
leftPad++ | |
rightPad++ | |
} | |
return fmt.Sprintf("[%*s%sAAA]", leftPad, val, strings.Repeat(" ", rightPad)) | |
} | |
func main() { | |
characters := []string{ | |
"π", | |
"π§πΎ", | |
"βΉοΈ", | |
"π©", | |
"x", | |
"π", | |
"π", | |
"βοΈ", | |
"β’", | |
"β¨―", | |
"βοΈ", | |
"β", | |
"Ψ", | |
"β", | |
"γ", | |
"ζΌ’", | |
"ν", | |
"ζΌ’", | |
"ζ±", | |
"⻨", | |
"ζΌ’", | |
"β", "β", "β", "β", "β", "β", "β", "β", | |
} | |
for _, c := range characters { | |
fmt.Println(valuePaddingPredictor(c)) | |
} | |
// valueDiagnostics("π", 3, 1) | |
// valueDiagnostics("π§πΎ", 4, 2) | |
// valueDiagnostics("βΉοΈ", 4, 2) | |
// valueDiagnostics("β’", 4, 1) | |
// valueDiagnostics("π©", 3, 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment