module Opal::VisualWidth

Overview

Utilities for computing string display width in terminal columns, accounting for ANSI escapes, East Asian wide characters, and Unicode emojis.

Defined in:

opal/style/visual_width.cr

Constant Summary

ANSI_REGEX = /\e\[[0-9;?]*[a-zA-Z]|\e\].*?(?:\a|\e\\)/

Class Method Summary

Class Method Detail

def self.char_width(char : Char) : Int32 #

Returns the visual column width of a character when printed on a terminal.


[View source]
def self.strip_ansi(str : String) : String #

Removes all ANSI escape codes from the string.


[View source]
def self.truncate(str : String, max_width : Int32, ellipsis : String = "...") : String #

Truncates a string to fit within max_width terminal columns, appending ellipsis if needed.


[View source]
def self.width(str : String) : Int32 #

Returns the total visual column width of a string.


[View source]