Class: TTY::Text::Wrapping
- Inherits:
-
Object
- Object
- TTY::Text::Wrapping
- Includes:
- Unicode
- Defined in:
- lib/tty/text/wrapping.rb
Overview
A class responsible for text wrapping operations
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(text, *args) ⇒ Wrapping
constructor
private
Initialize a Wrapping.
-
#wrap ⇒ Object
private
Wrap a text into lines no longer than length.
Methods included from Unicode
#as_unicode, #clean_utf8, #utf8?
Constructor Details
#new(text, value) ⇒ Wrapping #new(text, value, options) ⇒ Wrapping
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a Wrapping
34 35 36 37 38 39 40 41 |
# File 'lib/tty/text/wrapping.rb', line 34 def initialize(text, *args) = Utils.(args) @text = text @length = .fetch(:length) { DEFAULT_WIDTH } @indent = .fetch(:indent) { 0 } @padding = .fetch(:padding) { [] } @length = args[0] unless args.empty? end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
13 14 15 |
# File 'lib/tty/text/wrapping.rb', line 13 def indent @indent end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
11 12 13 |
# File 'lib/tty/text/wrapping.rb', line 11 def length @length end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
15 16 17 |
# File 'lib/tty/text/wrapping.rb', line 15 def padding @padding end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
9 10 11 |
# File 'lib/tty/text/wrapping.rb', line 9 def text @text end |
Instance Method Details
#wrap ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wrap a text into lines no longer than length
48 49 50 51 52 53 54 55 56 |
# File 'lib/tty/text/wrapping.rb', line 48 def wrap return text unless length && length > 0 as_unicode do text.split(NEWLINE, -1).map do |line| pad_line(indent_line(wrap_line(line))) end * NEWLINE end end |