Module: Railbow::TextUtils
- Included in:
- Formatters::Base, Railbow::Table::Renderer
- Defined in:
- lib/railbow/text_utils.rb
Instance Method Summary collapse
Instance Method Details
#display_width(str) ⇒ Object
11 12 13 |
# File 'lib/railbow/text_utils.rb', line 11 def display_width(str) Unicode::DisplayWidth.of(str.to_s) end |
#strip_ansi(str) ⇒ Object
7 8 9 |
# File 'lib/railbow/text_utils.rb', line 7 def strip_ansi(str) str.to_s.gsub(/\e\[[0-9;]*m/, "") end |
#truncate_str(str, max_width) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/railbow/text_utils.rb', line 15 def truncate_str(str, max_width) return str if display_width(strip_ansi(str)) <= max_width plain = strip_ansi(str) truncated = +"" width = 0 plain.each_char do |ch| ch_width = display_width(ch) break if width + ch_width > max_width - 3 truncated << ch width += ch_width end "#{truncated}..." end |