ConsoleForge 0.3.0
Elm-architecture TUI framework for .NET 8
Loading...
Searching...
No Matches
ConsoleForge.Layout.TextUtils Class Reference

Terminal-aware text utilities: visual column width, truncation, and padding that correctly handle multi-byte Unicode characters and wide glyphs (CJK ideographs, full-width forms, emoji) which occupy 2 terminal columns. More...

Static Public Member Functions

static string FitToWidth (string text, int targetWidth)
 Right-pads or truncates text so its visual width equals exactly targetWidth terminal columns.
static int MeasureWrapped (string text, int width, out int widestLine)
 How many lines WrapToWidth would produce, and how wide the widest of them would be, without building any of them.
static int RuneDisplayWidth (Rune rune)
 Returns the number of terminal columns a single Rune occupies:

  • 0 — combining marks, control and format characters (including ZWJ and the variation selectors).

static string TruncateToWidth (string text, int maxWidth)
 Truncates text so its visual width does not exceed maxWidth terminal columns.
static int VisualWidth (ReadOnlySpan< char > text)
 Returns the number of terminal columns occupied by the span.
static int VisualWidth (string text)
 Returns the number of terminal columns occupied by text .
static List< string > WrapToWidth (string text, int width)
 Splits text into lines of at most width terminal columns.

Static Public Attributes

const int VariationSelector16 = 0xFE0F
 U+FE0F VARIATION SELECTOR-16.

Detailed Description

Terminal-aware text utilities: visual column width, truncation, and padding that correctly handle multi-byte Unicode characters and wide glyphs (CJK ideographs, full-width forms, emoji) which occupy 2 terminal columns.

Performance — All methods use string.IsAscii (SIMD-vectorized in .NET 8) as the primary fast-path gate. For pure-ASCII strings (the common case for widget labels and UI text) the hot path degenerates to a single vectorized scan plus O(1) arithmetic — no Rune enumeration, no StringBuilder allocation.

Member Function Documentation

◆ FitToWidth()

string ConsoleForge.Layout.TextUtils.FitToWidth ( string text,
int targetWidth )
static

Right-pads or truncates text so its visual width equals exactly targetWidth terminal columns.

Wide characters that would overflow by exactly 1 column have a space substituted.

◆ MeasureWrapped()

int ConsoleForge.Layout.TextUtils.MeasureWrapped ( string text,
int width,
out int widestLine )
static

How many lines WrapToWidth would produce, and how wide the widest of them would be, without building any of them.

This exists for IMeasurable.Measure, which runs during layout on every frame and only needs the shape of the wrap, not its text. Materialising the lines there cost 320 bytes per TextBlock per frame for strings that were discarded and rebuilt by Render moments later.

It must stay in agreement with WrapToWidth; the two are pinned together by tests over a corpus of strings and widths.

Parameters
textThe text that would be wrapped.
widthColumn budget per line.
widestLineReceives the visual width of the widest wrapped line.
Returns
The number of wrapped lines.

◆ RuneDisplayWidth()

int ConsoleForge.Layout.TextUtils.RuneDisplayWidth ( Rune rune)
static

Returns the number of terminal columns a single Rune occupies:

  • 0 — combining marks, control and format characters (including ZWJ and the variation selectors).

These attach to the preceding character rather than advancing the cursor. 2 — East_Asian_Width Wide or Fullwidth: CJK, Hangul, fullwidth forms, and emoji that have default emoji presentation. 1 — everything else, including pictographs with default text presentation such as U+1F39E FILM FRAMES or U+2699 GEAR. Those widen to 2 columns only when followed by VariationSelector16, which callers handle at string level via WidthWalker.

Allocation-free. Printable ASCII and Latin-1 return without touching the table.

◆ TruncateToWidth()

string ConsoleForge.Layout.TextUtils.TruncateToWidth ( string text,
int maxWidth )
static

Truncates text so its visual width does not exceed maxWidth terminal columns.

Returns the original string reference unchanged when it already fits.

◆ VisualWidth()

int ConsoleForge.Layout.TextUtils.VisualWidth ( string text)
static

Returns the number of terminal columns occupied by text .

Pure ASCII strings return text.Length via a single SIMD scan. Wide characters (CJK, emoji, full-width) count as 2 columns.

◆ WrapToWidth()

List< string > ConsoleForge.Layout.TextUtils.WrapToWidth ( string text,
int width )
static

Splits text into lines of at most width terminal columns.

Hard newlines in the source always produce a line break.

Member Data Documentation

◆ VariationSelector16

const int ConsoleForge.Layout.TextUtils.VariationSelector16 = 0xFE0F
static

U+FE0F VARIATION SELECTOR-16.

Occupies no column of its own, but forces the preceding character into emoji presentation, which terminals render 2 columns wide. See VisualWidth(string).


The documentation for this class was generated from the following file:
  • src/ConsoleForge/Layout/TextUtils.cs