Skip to main content

Module util

Module util 

Source

Constants§

CJK_BREAK_REGEX
Regex pattern matching CJK characters for word-wrapping breaks. Matches pi’s cjkBreakRegex script extension pattern.

Functions§

extract_segments
Extract segments from a line for overlay compositing. Returns (before_text, before_width, after_text, after_width). The “before” segment is columns [0, before_end). The “after” segment is columns [after_start, total_width). Matches pi’s extractSegments.
is_cjk_break
Check if a grapheme cluster is CJK (needs its own token for wrapping).
is_whitespace_char
Check if a grapheme cluster is whitespace. Single-char check matching pi’s isWhitespaceChar.
normalize_terminal_output
Normalize a terminal output line by appending a reset + hyperlink-close sequence. This ensures any open ANSI/OSC styles are cleanly terminated. Matches pi’s normalizeTerminalOutput.
slice_by_column
Extract a range of visible columns from a line. Handles ANSI codes and wide chars.
slice_with_width
Slice text by visible columns, returning both the extracted text and its width. Like slice_by_column but also returns the actual visible width of the result. Matches pi’s sliceWithWidth.
truncate_to_width
Truncate text to fit within a maximum visible width, adding ellipsis if needed. Optionally pad with spaces to reach exactly max_width.
visible_width
Calculate the visible width of a string in terminal columns. Strips ANSI escape codes and counts grapheme cluster widths. Uses a thread-local LRU cache for non-ASCII strings (matching pi).
visual_col_to_byte_offset
Convert a visual column position to a byte offset in the given text. Handles ANSI escape codes and wide characters correctly.
wrap_text_with_ansi
Word-wrap text preserving ANSI escape codes. Returns lines where each line is <= width visible chars.