Class: Toys::WrappableString
- Inherits:
-
Object
- Object
- Toys::WrappableString
- Defined in:
- lib/toys/wrappable_string.rb
Overview
A string intended for word-wrapped display.
A WrappableString is an array of string "fragments" representing the atomic units that should not be split when word-wrapping. It should be possible to reconstruct the original string by joining these fragments with whitespace.
Instance Attribute Summary collapse
-
#fragments ⇒ Array<String>
readonly
Returns the string fragments, i.e.
Class Method Summary collapse
-
.make(obj) ⇒ Toys::WrappableString
Make the given object a WrappableString.
-
.make_array(objs) ⇒ Array<Toys::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) ⇒ WrappableString
Returns a new WrappaableString whose content is the concatenation of this WrappableString with another WrappableString.
-
#==(other) ⇒ Boolean
(also: #eql?)
Tests two wrappable strings for equality.
-
#empty? ⇒ Boolean
Returns true if the string is empty (i.e. has no fragments).
-
#hash ⇒ Integer
Returns a hash code for this object.
-
#initialize(string = "") ⇒ WrappableString
constructor
Create a wrapped string.
-
#string ⇒ String
(also: #to_s)
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.
You can pass either:
- A single String, which will be split into fragments by whitespace.
- An array of Strings representing the fragments explicitly.
23 24 25 |
# File 'lib/toys/wrappable_string.rb', line 23 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 string fragments, i.e. the individual "words" for wrapping.
32 33 34 |
# File 'lib/toys/wrappable_string.rb', line 32 def fragments @fragments end |
Class Method Details
.make(obj) ⇒ Toys::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.
146 147 148 |
# File 'lib/toys/wrappable_string.rb', line 146 def self.make(obj) obj.is_a?(WrappableString) ? obj : WrappableString.new(obj) end |
.make_array(objs) ⇒ Array<Toys::WrappableString>
Make the given object an array of WrappableString.
156 157 158 |
# File 'lib/toys/wrappable_string.rb', line 156 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.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/toys/wrappable_string.rb', line 126 def self.wrap_lines(strs, width, width2 = nil) result = Array(strs).map do |s| s = make(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) ⇒ WrappableString
Returns a new WrappaableString whose content is the concatenation of this WrappableString with another WrappableString.
41 42 43 44 |
# File 'lib/toys/wrappable_string.rb', line 41 def +(other) other = WrappableString.new(other) unless other.is_a?(WrappableString) WrappableString.new(fragments + other.fragments) end |
#==(other) ⇒ Boolean Also known as: eql?
Tests two wrappable strings for equality
70 71 72 73 |
# File 'lib/toys/wrappable_string.rb', line 70 def ==(other) return false unless other.is_a?(WrappableString) other.fragments == fragments end |
#empty? ⇒ Boolean
Returns true if the string is empty (i.e. has no fragments)
51 52 53 |
# File 'lib/toys/wrappable_string.rb', line 51 def empty? @fragments.empty? end |
#hash ⇒ Integer
Returns a hash code for this object
80 81 82 |
# File 'lib/toys/wrappable_string.rb', line 80 def hash fragments.hash end |
#string ⇒ String Also known as: to_s
Returns the string without any wrapping
60 61 62 |
# File 'lib/toys/wrappable_string.rb', line 60 def string @fragments.join(" ") end |
#wrap(width, width2 = nil) ⇒ Array<String>
Wraps the string to the given width.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/toys/wrappable_string.rb', line 93 def wrap(width, width2 = nil) lines = [] line = "" line_len = 0 fragments.each do |frag| frag_len = frag.gsub(/\e\[\d+(;\d+)*m/, "").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 |