beetlebug▾
/

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:

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

ArgumentTypeDefaultDescription
STRINGstringrequiredThe string to measure.

Options

OptionTypeDefaultDescription
-ambiguousnarrow or widenarrowGives 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 option

Returns

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.

See also