Options allows you to specify the treatment of ambiguous East Asian
characters and ANSI escape sequences. ControlSequences specifies whether to ignore 7-bit ECMA-48 escape sequences
when calculating the display width. When false (default), ANSI escape
sequences are treated as just a series of characters. When true, they are
treated as a single zero-width unit. ControlSequences8Bit specifies whether to ignore 8-bit ECMA-48 escape sequences
when calculating the display width. When false (default), these are treated
as just a series of characters. When true, they are treated as a single
zero-width unit. EastAsianWidth specifies whether to treat ambiguous East Asian characters
as width 1 or 2. When false (default), ambiguous East Asian characters
are treated as width 1. When true, they are width 2. Bytes calculates the display width of a []byte, for the given options, by
iterating over grapheme clusters in the slice and summing their widths. BytesGraphemes returns an iterator over grapheme clusters for the given
[]byte, with the given options.
Iterate using the Next method, and get the width of the current grapheme
using the Width method. Rune calculates the display width of a rune, for the given options.
You should almost certainly use [String] or [Bytes] for most purposes.
The smallest unit of display width is a grapheme cluster, not a rune.
Iterating over runes to measure width is incorrect in many cases. String calculates the display width of a string, for the given options, by
iterating over grapheme clusters in the string and summing their widths. StringGraphemes returns an iterator over grapheme clusters for the given
string, with the given options.
Iterate using the Next method, and get the width of the current grapheme
using the Width method. TruncateBytes truncates a []byte to the given maxWidth, and appends the
given tail if the []byte is truncated.
It ensures the visible width, including the width of the tail, is less than or
equal to maxWidth.
When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that
appear after the truncation point are preserved in the output. This ensures
that escape sequences such as SGR resets are not lost, preventing color
bleed in terminal output.
[Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values
(0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them
during truncation can shift byte boundaries and form unintended visible
characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width
measurement. TruncateString truncates a string to the given maxWidth, and appends the
given tail if the string is truncated.
It ensures the visible width, including the width of the tail, is less than or
equal to maxWidth.
When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that
appear after the truncation point are preserved in the output. This ensures
that escape sequences such as SGR resets are not lost, preventing color
bleed in terminal output.
[Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values
(0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them
during truncation can shift byte boundaries and form unintended visible
characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width
measurement.
func graphemeWidth[T](s T, options Options) int
var DefaultOptions
var github.com/charmbracelet/x/ansi.dwOptions *Options
Package-Level Functions (total 13, in which 7 are exported)
Bytes calculates the display width of a []byte,
by iterating over grapheme clusters in the byte slice
and summing their widths.
BytesGraphemes returns an iterator over grapheme clusters for the given
[]byte.
Iterate using the Next method, and get the width of the current grapheme
using the Width method.
Rune calculates the display width of a rune. You
should almost certainly use [String] or [Bytes] for
most purposes.
The smallest unit of display width is a grapheme
cluster, not a rune. Iterating over runes to measure
width is incorrect in many cases.
String calculates the display width of a string,
by iterating over grapheme clusters in the string
and summing their widths.
StringGraphemes returns an iterator over grapheme clusters for the given
string.
Iterate using the Next method, and get the width of the current grapheme
using the Width method.
TruncateBytes truncates a []byte to the given maxWidth, and appends the
given tail if the []byte is truncated.
It ensures the total width, including the width of the tail, is less than or
equal to maxWidth.
TruncateString truncates a string to the given maxWidth, and appends the
given tail if the string is truncated.
It ensures the total width, including the width of the tail, is less than or
equal to maxWidth.
Type Parameters:
T: ~string | []byte graphemeWidth returns the display width of a grapheme cluster.
The passed string must be a single grapheme cluster.
Type Parameters:
T: ~string | []byte isVS16 checks if the slice matches VS16 (U+FE0F) UTF-8 encoding
(EF B8 8F). It assumes len(s) >= 3.
Type Parameters:
T: ~string | []byte lookup returns the trie value for the first UTF-8 encoding in s and
the width in bytes of this encoding. The size will be 0 if s does not
hold enough bytes to complete the encoding. len(s) must be greater than 0.
lookupValue determines the type of block n and looks up the value for b.
Type Parameters:
T: string | []byte printableASCIILength returns the length of consecutive printable ASCII bytes
starting at the beginning of s.
Package-Level Variables (total 4, in which 1 is exported)
DefaultOptions is the default options for the display width
calculation, which is EastAsianWidth false, ControlSequences false, and
ControlSequences8Bit false.
propertyWidths is a jump table of sorts, instead of a switch
stringWidthIndex: 30 blocks, 1920 entries, 1920 bytes
Block 0 is the zero block.
stringWidthValues: 246 blocks, 15744 entries, 15744 bytes
The third block is the zero block.
Package-Level Constants (total 5, none are exported)