Module: Terminal::Text
- Defined in:
- lib/terminal/text.rb,
lib/terminal/text/char_width.rb
Overview
Text helper functions.
Class Attribute Summary collapse
-
.ambiguous_char_width ⇒ Integer
Value for Text.width of letters whose display width is not precisely defined by the Unicode standard.
Class Method Summary collapse
-
.each_line(*text, limit: nil, bbcode: true, ansi: true, ignore_newline: false) {|String| ... } ⇒ Enumerator?
(also: each)
Iterate each line of given text.
-
.each_line_with_size(*text, limit: nil, bbcode: true, ansi: true, ignore_newline: false) {|String, Integer| ... } ⇒ Enumerator?
(also: each_with_size)
Iterate each line and it's display width of given text.
-
.width(str, bbcode: true) ⇒ Integer
Calculates the display width of the text representation of a given argument.
Class Attribute Details
.ambiguous_char_width ⇒ Integer
Value for width of letters whose display width is not precisely defined by the Unicode standard. Defaults to one (1).
17 18 19 |
# File 'lib/terminal/text.rb', line 17 def ambiguous_char_width @ambiguous_char_width end |
Class Method Details
.each_line(*text, limit: nil, bbcode: true, ansi: true, ignore_newline: false) {|String| ... } ⇒ Enumerator? Also known as: each
Iterate each line of given text.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/terminal/text.rb', line 62 def each_line( *text, limit: nil, bbcode: true, ansi: true, ignore_newline: false, &block ) unless limit snippeds = as_snippeds(text, bbcode, ansi, ignore_newline, Word) return block ? lines(snippeds, &block) : to_enum(:lines, snippeds) end limit = limit.to_i raise(ArgumentError, "invalid limit - #{limit}") if limit < 1 snippeds = as_snippeds(text, bbcode, ansi, ignore_newline, WordEx) return lim_lines(snippeds, limit, &block) if block to_enum(:lim_lines, snippeds, limit) end |
.each_line_with_size(*text, limit: nil, bbcode: true, ansi: true, ignore_newline: false) {|String, Integer| ... } ⇒ Enumerator? Also known as: each_with_size
Iterate each line and it's display width of given text.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/terminal/text.rb', line 92 def each_line_with_size( *text, limit: nil, bbcode: true, ansi: true, ignore_newline: false, &block ) unless limit snippeds = as_snippeds(text, bbcode, ansi, ignore_newline, Word) return block ? pairs(snippeds, &block) : to_enum(:pairs, snippeds) end limit = limit.to_i raise(ArgumentError, "invalid limit - #{limit}") if limit < 1 snippeds = as_snippeds(text, bbcode, ansi, ignore_newline, WordEx) return lim_pairs(snippeds, limit, &block) if block to_enum(:lim_pairs, snippeds, limit) end |
.width(str, bbcode: true) ⇒ Integer
Calculates the display width of the text representation of a given argument. It can optionally ignore embedded BBCode.
The Unicode standard defines the display width for most characters but some are ambiguous. The function uses ambiguous_char_width for each of these characters.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/terminal/text.rb', line 30 def width(str, bbcode: true) str = bbcode ? Ansi.unbbcode(str) : str.to_s return 0 if str.empty? str = str.encode(@encoding) if str.encoding != @encoding width = 0 str.scan(WIDTH_SCANNER) do |sp, gc| next width += char_width(gc) if gc width += 1 if sp end width end |