| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"strings" |
| 7 |
"unicode/utf8" |
| 8 |
|
| 9 |
"github.com/clipperhouse/displaywidth" |
| 10 |
) |
| 11 |
|
| 12 |
func main() { |
| 13 |
if len(os.Args) > 1 && os.Args[1] == "--help" { |
| 14 |
fmt.Println("Usage: terminal-test") |
| 15 |
fmt.Println("Tests the actual terminal display width of regional indicator symbols") |
| 16 |
fmt.Println("by using visual alignment tests.") |
| 17 |
fmt.Println() |
| 18 |
fmt.Println("Run from a terminal to see visual alignment results.") |
| 19 |
os.Exit(0) |
| 20 |
} |
| 21 |
|
| 22 |
fmt.Println("=== Terminal Regional Indicator Width Test ===") |
| 23 |
fmt.Printf("TERM: %s\n", os.Getenv("TERM")) |
| 24 |
fmt.Println() |
| 25 |
|
| 26 |
|
| 27 |
singleRI := "π¦E6 |
| 28 |
pairRI := "πΊflag |
| 29 |
regularEmoji := "π" |
| 30 |
ascii := "a" |
| 31 |
cjk := "δΈ" |
| 32 |
|
| 33 |
testCases := []struct { |
| 34 |
name string |
| 35 |
char string |
| 36 |
width int |
| 37 |
}{ |
| 38 |
{"Single Regional Indicator", singleRI, displaywidth.String(singleRI)}, |
| 39 |
{"Regional Indicator Pair (flag)", pairRI, displaywidth.String(pairRI)}, |
| 40 |
{"Regular Emoji", regularEmoji, displaywidth.String(regularEmoji)}, |
| 41 |
{"ASCII", ascii, displaywidth.String(ascii)}, |
| 42 |
{"CJK", cjk, displaywidth.String(cjk)}, |
| 43 |
} |
| 44 |
|
| 45 |
fmt.Println("Package calculated widths:") |
| 46 |
for _, tc := range testCases { |
| 47 |
fmt.Printf(" %s (%s): %d columns\n", tc.name, tc.char, tc.width) |
| 48 |
} |
| 49 |
fmt.Println() |
| 50 |
|
| 51 |
|
| 52 |
fmt.Println("=== Visual Alignment Tests ===") |
| 53 |
fmt.Println("Check if the markers align correctly with the characters.") |
| 54 |
fmt.Println("If aligned: terminal width matches package calculation.") |
| 55 |
fmt.Println("If misaligned: terminal rendering differs from package.") |
| 56 |
fmt.Println() |
| 57 |
|
| 58 |
for _, tc := range testCases { |
| 59 |
visualTest(tc.char, tc.name, tc.width) |
| 60 |
fmt.Println() |
| 61 |
} |
| 62 |
|
| 63 |
fmt.Println("=== Summary ===") |
| 64 |
fmt.Println("Compare the visual alignment above.") |
| 65 |
fmt.Println("The '^' marker shows the START of the character.") |
| 66 |
fmt.Println("The 'x' marker shows the expected END position (start + width).") |
| 67 |
fmt.Println("If characters align with the markers, the package calculation is correct.") |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func visualTest(char string, label string, expectedWidth int) { |
| 72 |
fmt.Printf("--- %s: %s ---\n", label, char) |
| 73 |
|
| 74 |
|
| 75 |
marker := "0123456789" |
| 76 |
testLine := marker + char + marker |
| 77 |
fmt.Println(testLine) |
| 78 |
|
| 79 |
|
| 80 |
caretStart := len(marker) |
| 81 |
caretLine := strings.Repeat(" ", caretStart) + "^ (start)" + strings.Repeat(" ", len(marker)-6) |
| 82 |
fmt.Println(caretLine) |
| 83 |
|
| 84 |
|
| 85 |
expectedEnd := len(marker) + expectedWidth |
| 86 |
if expectedEnd <= len(testLine) { |
| 87 |
expectedLine := strings.Repeat(" ", expectedEnd) + "x (expected end, width=" + fmt.Sprintf("%d", expectedWidth) + ")" |
| 88 |
fmt.Println(expectedLine) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
runeCount := utf8.RuneCountInString(char) |
| 93 |
fmt.Printf(" UTF-8 bytes: %d | Runes: %d | Package width: %d\n", len(char), runeCount, expectedWidth) |
| 94 |
} |