Skip to main content

TextWidth class

Namespace: Arlecchino.Rendering.Text · Assembly: Arlecchino.Core

Measures text the way a terminal shows it, in columns rather than in char values. Use these instead of string.Length, PadRight and slicing wherever the result lands on screen.

public static class TextWidth

Methods

MemberSummary
CountClusters(string)How many symbols the text is made of, as the user would count them.
NextClusterEnd(string, int)Where the symbol at an index ends. This is what a forward delete has to remove.
NextClusterLength(string, int)How many char values the next symbol occupies, starting at an index.
Of(string)How many columns the text occupies.
OfCluster(ReadOnlySpan<char>)Width of a single grapheme cluster — one symbol as the user sees it.
OfRune(Rune)Width of a single code point, before combining marks are taken into account.
PadLeft(string, int)Pads the text with spaces on the left, which right-aligns it in that width.
PadRight(string, int)Pads the text with spaces on the right until it fills the given column width.
PreviousClusterStart(string, int)Where the symbol before an index starts. This is what a backspace has to remove: deleting one char would cut an emoji or a letter with a combining mark in half.
SnapToCluster(string, int)Pulls a position back to the start of the symbol it lands in, so an index that came from somewhere else never points into the middle of one.
Truncate(string, int)Cuts the text down to a column width on a symbol boundary, so a wide character or a surrogate pair is never split in half.
TruncateStart(string, int)Cuts the text down to a column width from the other end, keeping the tail rather than the head. That is what a field scrolled to the right shows.
Wrap(string, int)Breaks text into lines that fit a column width, at spaces where there is one and mid-word only for a word wider than the space. Line breaks already in the text are kept.

Methods in detail

CountClusters(string)

public static int CountClusters(string text);

How many symbols the text is made of, as the user would count them.

Parameters

NameTypeDescription
textstringThe text to count.

Returns int — The number of grapheme clusters.

NextClusterEnd(string, int)

public static int NextClusterEnd(string text, int index);

Where the symbol at an index ends. This is what a forward delete has to remove.

Parameters

NameTypeDescription
textstringThe text being walked.
indexintPosition to look forward from.

Returns int — The next boundary, or the length of the text at its end.

NextClusterLength(string, int)

public static int NextClusterLength(string text, int index);

How many char values the next symbol occupies, starting at an index.

Parameters

NameTypeDescription
textstringThe text being walked.
indexintWhere the symbol starts.

Returns int — Length of the cluster in char values.

Of(string)

public static int Of(string text);

How many columns the text occupies.

Parameters

NameTypeDescription
textstringThe text to measure.

Returns int — Width in terminal columns.

OfCluster(ReadOnlySpan<char>)

public static int OfCluster(ReadOnlySpan<char> cluster);

Width of a single grapheme cluster — one symbol as the user sees it.

Parameters

NameTypeDescription
clusterReadOnlySpan<T><char>The cluster, as returned by TextWidth.NextClusterLength.

Returns int — 0, 1 or 2 columns.

OfRune(Rune)

public static int OfRune(Rune rune);

Width of a single code point, before combining marks are taken into account.

Parameters

NameTypeDescription
runeRuneThe code point to measure.

Returns int — 0 for marks and control characters, 2 for wide ranges, 1 otherwise.

PadLeft(string, int)

public static string PadLeft(string text, int width);

Pads the text with spaces on the left, which right-aligns it in that width.

Parameters

NameTypeDescription
textstringThe text to pad.
widthintColumns to fill.

Returns string — The padded text, unchanged when it is already that wide.

PadRight(string, int)

public static string PadRight(string text, int width);

Pads the text with spaces on the right until it fills the given column width.

Parameters

NameTypeDescription
textstringThe text to pad.
widthintColumns to fill.

Returns string — The padded text, unchanged when it is already that wide.

PreviousClusterStart(string, int)

public static int PreviousClusterStart(string text, int index);

Where the symbol before an index starts. This is what a backspace has to remove: deleting one char would cut an emoji or a letter with a combining mark in half.

Parameters

NameTypeDescription
textstringThe text being walked.
indexintPosition to look back from.

Returns int — The boundary before the index, or 0 at the start of the text.

SnapToCluster(string, int)

public static int SnapToCluster(string text, int index);

Pulls a position back to the start of the symbol it lands in, so an index that came from somewhere else never points into the middle of one.

Parameters

NameTypeDescription
textstringThe text being walked.
indexintPosition to snap.

Returns int — The boundary at or before the index.

Truncate(string, int)

public static string Truncate(string text, int maxWidth);

Cuts the text down to a column width on a symbol boundary, so a wide character or a surrogate pair is never split in half.

Parameters

NameTypeDescription
textstringThe text to cut.
maxWidthintColumns available.

Returns string — The longest prefix that fits.

TruncateStart(string, int)

public static string TruncateStart(string text, int maxWidth);

Cuts the text down to a column width from the other end, keeping the tail rather than the head. That is what a field scrolled to the right shows.

Parameters

NameTypeDescription
textstringThe text to cut.
maxWidthintColumns available.

Returns string — The longest suffix that fits.

Wrap(string, int)

public static List<string> Wrap(string text, int width);

Breaks text into lines that fit a column width, at spaces where there is one and mid-word only for a word wider than the space. Line breaks already in the text are kept.

Parameters

NameTypeDescription
textstringThe text to break up.
widthintColumns available; anything below one is treated as one.

Returns List<T><string> — The lines, in order.