beamtea_util (beamtea v0.1.2)

View Source

Small shared helpers for beamtea bubbles.

Summary

Functions

The display width of a single code point (0, 1 or 2).

Format milliseconds as MM:SS (or HH:MM:SS past an hour).

Format milliseconds as MM:SS.CS (centiseconds) — for stopwatches.

Right-pad an iodata string with spaces to Width code points (never truncates).

Right-pad to Width *visible* columns, ignoring ANSI escapes in the width calculation (so coloured text lines up correctly).

Remove ANSI escape sequences (SGR colours, cursor moves, …) from a string, leaving only the visible characters.

Truncate an iodata string to at most Max *display columns*, adding an ellipsis when cut. Wide characters count as 2, so the result never exceeds Max columns even when it splits around emoji/CJK.

The number of *display columns* a string occupies, ignoring ANSI escapes. Uses a wcwidth (via prim_tty:npwcwidth/1), so wide characters (emoji, CJK) count as 2 and combining marks as 0 — this is what keeps borders and padding aligned around wide glyphs.

Functions

char_width(C)

-spec char_width(char()) -> 0..2.

The display width of a single code point (0, 1 or 2).

format_clock(Ms)

-spec format_clock(integer()) -> iodata().

Format milliseconds as MM:SS (or HH:MM:SS past an hour).

format_stopwatch(Ms)

-spec format_stopwatch(integer()) -> iodata().

Format milliseconds as MM:SS.CS (centiseconds) — for stopwatches.

pad_right(IoData, Width)

-spec pad_right(iodata(), non_neg_integer()) -> iodata().

Right-pad an iodata string with spaces to Width code points (never truncates).

pad_visible(IoData, Width)

-spec pad_visible(unicode:chardata(), non_neg_integer()) -> iodata().

Right-pad to Width *visible* columns, ignoring ANSI escapes in the width calculation (so coloured text lines up correctly).

strip_ansi(IoData)

-spec strip_ansi(unicode:chardata()) -> binary().

Remove ANSI escape sequences (SGR colours, cursor moves, …) from a string, leaving only the visible characters.

truncate(IoData, Max)

-spec truncate(iodata(), non_neg_integer()) -> binary().

Truncate an iodata string to at most Max *display columns*, adding an ellipsis when cut. Wide characters count as 2, so the result never exceeds Max columns even when it splits around emoji/CJK.

visible_width(IoData)

-spec visible_width(unicode:chardata()) -> non_neg_integer().

The number of *display columns* a string occupies, ignoring ANSI escapes. Uses a wcwidth (via prim_tty:npwcwidth/1), so wide characters (emoji, CJK) count as 2 and combining marks as 0 — this is what keeps borders and padding aligned around wide glyphs.