ztcl::text width
Gives the count of columns of a string in a terminal or a monospace font.
ztcl::text width ?-ambiguous narrow|wide? STRING
Description
Each grapheme cluster has the largest width of its code points, plus the width of each spacing mark and each conjunct consonant in it:
- marks, format characters except U+00AD, controls such as
\t, default ignorables, and the medial and final jamo of Hangul are 0 columns; - the East Asian Wide and Fullwidth characters, and emoji with emoji presentation, are 2 columns;
- the East Asian Ambiguous characters, such as
±and the Greek letters, are 1 column, or 2 with-ambiguous wide; - each other code point is 1 column.
U+FE0F after an emoji makes the cluster 2 columns, and a flag of two regional indicators is 2 columns. The widths equal the cursor moves of the terminal of ztcl::vt with grapheme clusters on (mode 2027).
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| STRING | string | required | The string to measure. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-ambiguous | narrow or wide | narrow | Gives the width of the East Asian Ambiguous characters. |
-ambiguous WIDTH
Gives the width of the East Asian Ambiguous characters.
narrow counts each East Asian Ambiguous character as 1 column, and wide as 2, as a terminal set for East Asian text does. The name of the option has no short form.
Values: narrow, wide.
% ztcl::text width -ambiguous wide "±"
2
% ztcl::text width -ambiguous narrow "±"
1% ztcl::text width -ambiguous big a
bad value "big": must be narrow or wide
-errorcode TCL LOOKUP INDEX value
% ztcl::text width -amb wide a
bad option "-amb": must be -ambiguous
-errorcode TCL LOOKUP INDEX optionReturns
An integer.
Examples
Each example of this page runs after package require ztcl::text.
% ztcl::text width "ab中"
4
% ztcl::text width "❤️ é"
4
% ztcl::text width "\U1F1FA\U1F1F8"
2
% ztcl::text width "a\tb"
2
% ztcl::text width ""
0
Errors
% ztcl::text width
wrong # args: should be "ztcl::text width ?-ambiguous narrow|wide? string"
-errorcode TCL WRONGARGS
% ztcl::text width -x a
wrong # args: should be "ztcl::text width ?-ambiguous narrow|wide? string"
-errorcode TCL WRONGARGS
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1
- Compute cost
- One compute unit for each 4,096 bytes of the string.