Skip to content

sliceToWidth

sliceToWidth(text, maxWidth): string

Defined in: packages/core/src/utils/displayWidth.ts:221

The longest LEADING run of whole clusters whose total width is at most maxWidth — i.e. keep the head, lose the tail. Total (never throws): a non-positive maxWidth yields ''.

A cluster is kept only if it fits ENTIRELY, so the result can come back one column short of maxWidth when the next cluster is two columns wide. That is the point: half a wide glyph cannot be drawn, and spending the column anyway is how text over-runs its budget.

The clusters are produced one at a time and the walk stops at the budget, so the cost is the length of the RESULT rather than the length of the input — the input here is a whole tool-output line, which is unbounded.

string

number

string