Class: Toys::Utils::WrappableString
- Inherits:
-
Object
- Object
- Toys::Utils::WrappableString
- Defined in:
- lib/toys/utils/wrappable_string.rb
Overview
A string intended to be wrapped.
Instance Attribute Summary collapse
-
#fragments ⇒ Array<String>
readonly
Returns the fragments.
Class Method Summary collapse
-
.make(obj) ⇒ Toys::Utils::WrappableString
Make the given object a WrappableString.
-
.make_array(objs) ⇒ Array<Toys::Utils::WrappableString>
Make the given object an array of WrappableString.
-
.wrap_lines(strs, width, width2 = nil) ⇒ Array<String>
Wraps an array of lines to the given width.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concatenates this WrappableString with another WrappableString.
-
#empty? ⇒ String
Returns true if the string is empty (i.e. has no fragments).
-
#initialize(string = "") ⇒ WrappableString
constructor
Create a wrapped string.
-
#to_s ⇒ String
(also: #string)
Returns the string without any wrapping.
-
#wrap(width, width2 = nil) ⇒ Array<String>
Wraps the string to the given width.
Constructor Details
#initialize(string = "") ⇒ WrappableString
Create a wrapped string.
43 44 45 |
# File 'lib/toys/utils/wrappable_string.rb', line 43 def initialize(string = "") @fragments = string.is_a?(::Array) ? string.map(&:to_s) : string.to_s.split end |
Instance Attribute Details
#fragments ⇒ Array<String> (readonly)
Returns the fragments.
51 52 53 |
# File 'lib/toys/utils/wrappable_string.rb', line 51 def fragments @fragments end |
Class Method Details
.make(obj) ⇒ Toys::Utils::WrappableString
Make the given object a WrappableString. If the object is already a WrappableString, return it. Otherwise, treat it as a string or an array of strings and wrap it in a WrappableString.
150 151 152 |
# File 'lib/toys/utils/wrappable_string.rb', line 150 def self.make(obj) obj.is_a?(Utils::WrappableString) ? obj : Utils::WrappableString.new(obj) end |
.make_array(objs) ⇒ Array<Toys::Utils::WrappableString>
Make the given object an array of WrappableString.
160 161 162 |
# File 'lib/toys/utils/wrappable_string.rb', line 160 def self.make_array(objs) Array(objs).map { |obj| make(obj) } end |
.wrap_lines(strs, width, width2 = nil) ⇒ Array<String>
Wraps an array of lines to the given width.
131 132 133 134 135 136 137 138 139 |
# File 'lib/toys/utils/wrappable_string.rb', line 131 def self.wrap_lines(strs, width, width2 = nil) result = Array(strs).map do |s| lines = s.empty? ? [""] : s.wrap(width, width2) width = width2 if width2 lines end.flatten result = [] if result.all?(&:empty?) result end |
Instance Method Details
#+(other) ⇒ Object
Concatenates this WrappableString with another WrappableString
57 58 59 60 |
# File 'lib/toys/utils/wrappable_string.rb', line 57 def +(other) other = WrappableString.new(other) unless other.is_a?(WrappableString) WrappableString.new(fragments + other.fragments) end |
#empty? ⇒ String
Returns true if the string is empty (i.e. has no fragments)
66 67 68 |
# File 'lib/toys/utils/wrappable_string.rb', line 66 def empty? @fragments.empty? end |
#to_s ⇒ String Also known as: string
Returns the string without any wrapping
74 75 76 |
# File 'lib/toys/utils/wrappable_string.rb', line 74 def to_s @fragments.join(" ") end |
#wrap(width, width2 = nil) ⇒ Array<String>
Wraps the string to the given width.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/toys/utils/wrappable_string.rb', line 99 def wrap(width, width2 = nil) lines = [] line = "" line_len = 0 fragments.each do |frag| frag_len = Utils::Terminal.remove_style_escapes(frag).size if line_len.zero? line = frag line_len = frag_len elsif width && line_len + 1 + frag_len > width lines << line line = frag line_len = frag_len width = width2 if width2 else line_len += frag_len + 1 line = "#{line} #{frag}" end end lines << line if line_len.positive? lines end |