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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/terminal/text.rb', line 58 def each_line( *text, limit: nil, bbcode: true, ansi: true, ignore_newline: false, &block ) if limit limit = limit.to_i raise(ArgumentError, "invalid limit - #{limit}") if limit < 1 if block_given? lines_in(text, bbcode, ansi, ignore_newline, limit, &block) else to_enum(:lines_in, text, bbcode, ansi, ignore_newline, limit) end elsif block_given? lines(text, bbcode, ansi, ignore_newline, &block) else to_enum(:lines, text, bbcode, ansi, ignore_newline) end 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/terminal/text.rb', line 88 def each_line_with_size( *text, limit: nil, bbcode: true, ansi: true, ignore_newline: false, &block ) if limit limit = limit.to_i raise(ArgumentError, "invalid limit - #{limit}") if limit < 1 if block_given? pairs_in(text, bbcode, ansi, ignore_newline, limit, &block) else to_enum(:pairs_in, text, bbcode, ansi, ignore_newline, limit) end elsif block_given? pairs(text, bbcode, ansi, ignore_newline, &block) else to_enum(:pairs, text, bbcode, ansi, ignore_newline) end 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(ENC) if str.encoding != ENC width = 0 str.scan(WIDTH_SCANNER) do |sp, gc| next width += char_width(gc) if gc width += 1 if sp end width end |