Table of Contents

Class UnicodeWidth

Namespace
SharpConsoleUI.Helpers
Assembly
SharpConsoleUI.dll

Provides display width information for Unicode characters. Delegates to the Wcwidth library for accurate width calculation including zero-width characters (combining marks, variation selectors, ZWJ). Spacing Combining Marks (Unicode category Mc) are corrected to width 1, as they occupy visual space in terminals despite Wcwidth marking them zero-width. Adapts to terminal capabilities: uses Unicode 15.0 width tables unless the terminal is detected to support Unicode 16.0 widths (probed at startup).

public static class UnicodeWidth
Inheritance
UnicodeWidth
Inherited Members

Methods

CharOffsetToColumn(string, int)

Returns the display column at the given character index (UTF-16 offset). Equivalent to GetStringWidth(s.Substring(0, charOffset)) but without allocating.

public static int CharOffsetToColumn(string s, int charOffset)

Parameters

s string
charOffset int

Returns

int

ColumnToCharOffset(string, int)

Returns the character index (UTF-16 offset) at or before the given display column. A column that lands inside a wide char resolves to that char's start. Clamps to s.Length. Mirrors GetStringWidth(string) so the two never disagree.

public static int ColumnToCharOffset(string s, int column)

Parameters

s string
column int

Returns

int

GetCharWidth(char)

Returns the display width of a character in terminal columns (0, 1, or 2). Spacing Combining Marks (Mc) are corrected to width 1.

public static int GetCharWidth(char c)

Parameters

c char

Returns

int

GetRuneWidth(Rune)

Returns the display width of a Rune in terminal columns (0, 1, or 2). Spacing Combining Marks (Mc) are corrected to width 1.

public static int GetRuneWidth(Rune r)

Parameters

r Rune

Returns

int

GetStringWidth(string)

Returns the total display width of a string, summing per-Rune widths. Zero-width characters contribute 0 to the total. VS16 (U+FE0F) after a widenable emoji adds 1 column (widening from 1 to 2).

public static int GetStringWidth(string s)

Parameters

s string

Returns

int

IsVS16(Rune)

Returns true if the rune is Variation Selector 16 (U+FE0F), which widens certain emoji from 1 to 2 terminal columns.

public static bool IsVS16(Rune r)

Parameters

r Rune

Returns

bool

IsVs16Widened(Rune)

Returns true if VS16 (U+FE0F) after this rune widens it from 1 to 2 columns. Uses the Wcwidth library's Vs16Table for accurate lookup. Returns false if the terminal does not support VS16 widening (detected at startup via TerminalCapabilities.Probe).

public static bool IsVs16Widened(Rune baseRune)

Parameters

baseRune Rune

Returns

bool

IsWide(char)

Returns true if the character occupies 2 terminal columns.

public static bool IsWide(char c)

Parameters

c char

Returns

bool

IsWideRune(Rune)

Returns true if the Rune occupies 2 terminal columns.

public static bool IsWideRune(Rune r)

Parameters

r Rune

Returns

bool

IsZeroWidth(Rune)

Returns true if the Rune occupies 0 terminal columns (combining mark, etc.).

public static bool IsZeroWidth(Rune r)

Parameters

r Rune

Returns

bool

TakeColumns(string, int, int)

Width-aware slice primitive for wrapping: starting at character index startChar, consumes whole runes while the total stays within maxColumns. Returns the END character index and the display columns actually consumed. Guarantees forward progress: a single rune wider than maxColumns is still taken whole (so wrap loops can never stall).

public static (int endChar, int width) TakeColumns(string s, int startChar, int maxColumns)

Parameters

s string
startChar int
maxColumns int

Returns

(int absoluteLeft, int absoluteTop)