Skip to content

smelt.text

Tier: UiHost - Requires a terminal UI; calling these from headless mode raises.

Visibility: Public - Stable Lua API intended for user config and plugins.

Visual-width measurement. UiHost-only.

smelt.text.fit

fun(s: string, width: integer, opts: table?): string

Force s to occupy exactly width display cells: truncate when too long (appending opts.suffix, default "…"), pad when too short (with opts.fill, default " "). opts.align is "left" (default), "right", or "center". Use this for fixed-width UI slots - handles multi-byte and wide chars correctly so the result is always exactly width cells wide regardless of content.

smelt.text.format_cost

fun(usd: number): string

Format a USD cost with precision that scales to the magnitude: $0.0042 under one cent, $0.123 under one dollar, $1.23 otherwise. Mirrors the format the prompt bar uses for session cost.

smelt.text.format_duration

fun(seconds: integer): string

Format seconds as a short human-readable duration: 42s, 3m 12s, 1h 5m 0s. Used by the prompt-bar working indicator; useful for any plugin surfacing elapsed time.

smelt.text.format_tokens

fun(n: integer): string

Format a raw token count as 1.2k, 3.4m, or the bare integer for values under 1000. Useful for compact statusline / banner displays.

smelt.text.line_count

fun(s: string): integer

Return the number of lines in s. Counts \n separators and adds one if the last line is unterminated; an empty string returns 0. Matches the line count users see in a renderer that splits on \n without dropping the trailing partial line.

smelt.text.sanitize_utf8

fun(s: string): string

Return s as valid UTF-8, replacing malformed byte sequences with the Unicode replacement character. Useful when a Lua string came from raw bytes; prefer smelt.text.truncate when shortening text.

smelt.text.slugify

fun(s: string): string

Lowercase s, replace non-alphanumeric runs with -, drop empty segments. Same algorithm the title plugin uses for fallback slugs.

smelt.text.truncate

fun(s: string, max_bytes: integer, opts: any?): string

Return a valid UTF-8 string shortened to a byte budget. By default keeps the head: truncate(s, n). Passing a string third argument appends it as a suffix when truncation happens. Passing an opts table enables { keep = "head"|"tail", prefix?, suffix? }; use { keep = "tail" } for recent-message snippets. Lua string slicing is byte-based and can split multi-byte characters; this function snaps to UTF-8 boundaries and also accepts already-invalid Lua byte strings.

smelt.text.truncate_cells

fun(s: string, width: integer, opts: table?): string

Return s shortened to at most width display cells, appending opts.suffix (default "…") when truncation happens. Unlike smelt.text.fit, this does not pad short strings.

smelt.text.width

fun(s: string): integer

Return the visual column count of s. Lua's #s counts bytes; use this for sizing extmark ranges or computing column offsets so multi-byte and wide characters land correctly.

smelt.text.wrap_prefixed

fun(text: string, width: integer, opts: table?): string[]

Hard-wrap text into rows with opts.prefix on the first row and opts.cont_prefix on continuation rows. Wrapping uses terminal-cell width, preserves every character, and returns an array of strings. width = 0 disables wrapping.