Text Width (Unicode Width) Calculator
Demonstrates string width calculation in a terminal environment.
Not all characters are created equal. In a TUI, βWidthβ means cell count, not string length. Emoji (π) take 2 cells. Chinese characters (δ½ ) take 2 cells. The tui.text_width helper tells you the visual width of a string.
Features Demonstrated
-
Unicode Width: Rendering ASCII (1 cell), CJK (2 cells), and Emoji (2 cells).
-
Calculation: Comparing
string.lengthvstui.text_width(string).
Hotkeys
-
Up/Down (β/β): Cycle Text Sample
-
q: Quit
Usage
ruby examples/widget_text_width/app.rb
Learning Outcomes
Use this example if you need toβ¦
-
Align text correctly in columns.
-
Truncate strings that are too long for a widget.
-
Build your own custom layout engine.
