Skip to content
docsv0.3.0

TuiString

Width-aware string helpers. Everything here measures in terminal CELLS, not bytes or code points, and ignores ANSI sequences — a renderer that measured in bytes would not produce a wrong string, it would corrupt the frame, because the buffer composites at bounds without clamping.

TuiString::clean()

public static function clean(string $text): string

Strips control characters that would corrupt the frame, leaving printable text and ANSI styling intact.

Parameters

Parameters of clean()
NameTypeDescription
$textstring

TuiString::stripAnsi()

public static function stripAnsi(string $text): string

The text with every ANSI escape sequence removed.

Parameters

Parameters of stripAnsi()
NameTypeDescription
$textstring

TuiString::visibleLength()

public static function visibleLength(string $text): int

The width the text occupies in terminal cells, ignoring ANSI sequences and counting wide graphemes as the columns they really take.

Parameters

Parameters of visibleLength()
NameTypeDescription
$textstring

TuiString::truncate()

public static function truncate(string $text, int $width, string $ellipsis = '…'): string

The text cut to fit the width, ending in the ellipsis when something was removed. Text that already fits is returned unchanged.

Parameters

Parameters of truncate()
NameTypeDescription
$textstring
$widthint
$ellipsisstring

TuiString::padEnd()

public static function padEnd(string $text, int $width): string

The text padded with spaces to exactly the given cell width.

Parameters

Parameters of padEnd()
NameTypeDescription
$textstring
$widthint

TuiString::cells()

public static function cells(string $text): array

The text split into one entry per terminal cell.

Parameters

Parameters of cells()
NameTypeDescription
$textstring

TuiString::slice()

public static function slice(string $text, int $width): string

The leading portion of the text that fits the width, without an ellipsis.

Parameters

Parameters of slice()
NameTypeDescription
$textstring
$widthint

TuiString::sliceFrom()

public static function sliceFrom(string $text, int $start): string

The complement of {@see self::slice()}: everything from the given visible-column offset onward, mb-aware.

Parameters

Parameters of sliceFrom()
NameTypeDescription
$textstring
$startint

TuiString::wordwrap()

public static function wordwrap(string $text, int $width, string $break = '
'): string

Mb-aware equivalent of PHP's `wordwrap($text, $width, $break, true)` -- wraps on word boundaries and hard-breaks any single word wider than $width, but measures/cuts by visible columns (via {@see self::visibleLength()}/{@see self::slice()}) instead of bytes, so multibyte content (accents, emoji) wraps at the same width a plain-ASCII string of that length would.

Parameters

Parameters of wordwrap()
NameTypeDescription
$textstring
$widthint
$breakstring