Module: Charming::UI::Width

Defined in:
lib/charming/ui/width.rb

Overview

Width is a namespace for measuring and normalising the visual width of strings that may contain ANSI escape sequences. It delegates to ‘Unicode::DisplayWidth` while automatically stripping formatting codes so layout primitives can calculate exact character positions.

Constant Summary collapse

ANSI_PATTERN =
/\e\[[0-9;]*m/

Class Method Summary collapse

Class Method Details

.measure(value) ⇒ Object



15
16
17
# File 'lib/charming/ui/width.rb', line 15

def measure(value)
  Unicode::DisplayWidth.of(strip_ansi(value.to_s))
end

.strip_ansi(value) ⇒ Object



19
20
21
# File 'lib/charming/ui/width.rb', line 19

def strip_ansi(value)
  value.to_s.gsub(ANSI_PATTERN, "")
end