class Toys::WrappableString

A string intended for word-wrapped display.

Attributes

fragments[R]

Returns the string fragments, i.e. the individual “words” for wrapping.

@return [Array<String>]

Public Class Methods

make(obj) click to toggle source

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.

@param obj [Toys::WrappableString,String,Array<String>] @return [Toys::WrappableString]

# File lib/toys/wrappable_string.rb, line 127
def self.make(obj)
  obj.is_a?(WrappableString) ? obj : WrappableString.new(obj)
end
make_array(objs) click to toggle source

Make the given object an array of WrappableString.

@param objs [Array<Toys::WrappableString,String,Array<String>>] @return [Array<Toys::WrappableString>]

# File lib/toys/wrappable_string.rb, line 137
def self.make_array(objs)
  Array(objs).map { |obj| make(obj) }
end
new(string = "") click to toggle source

Create a wrapped string. @param string [String,Array<String>] The string or array of string

fragments
# File lib/toys/wrappable_string.rb, line 13
def initialize(string = "")
  @fragments = string.is_a?(::Array) ? string.map(&:to_s) : string.to_s.split
end
wrap_lines(strs, width, width2 = nil) click to toggle source

Wraps an array of lines to the given width.

@param strs [Array<WrappableString>] Array of strings to wrap. @param width [Integer,nil] Width in characters, or `nil` for infinite. @param width2 [Integer,nil] Width in characters for the second and

subsequent lines, or `nil` to use the same as width.

@return [Array<String>] Wrapped lines

# File lib/toys/wrappable_string.rb, line 107
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

Public Instance Methods

+(other) click to toggle source

Returns a new WrappaableString whose content is the concatenation of this WrappableString with another WrappableString.

@param other [WrappableString] @return [WrappableString]

# File lib/toys/wrappable_string.rb, line 31
def +(other)
  other = WrappableString.new(other) unless other.is_a?(WrappableString)
  WrappableString.new(fragments + other.fragments)
end
==(other) click to toggle source

@private

# File lib/toys/wrappable_string.rb, line 54
def ==(other)
  return false unless other.is_a?(WrappableString)
  other.fragments == fragments
end
Also aliased as: eql?
empty?() click to toggle source

Returns true if the string is empty (i.e. has no fragments) @return [Boolean]

# File lib/toys/wrappable_string.rb, line 40
def empty?
  @fragments.empty?
end
eql?(other)
Alias for: ==
hash() click to toggle source

@private

# File lib/toys/wrappable_string.rb, line 61
def hash
  fragments.hash
end
string() click to toggle source

Returns the string without any wrapping @return [String]

# File lib/toys/wrappable_string.rb, line 48
def string
  @fragments.join(" ")
end
Also aliased as: to_s
to_s()
Alias for: string
wrap(width, width2 = nil) click to toggle source

Wraps the string to the given width.

@param width [Integer,nil] Width in characters, or `nil` for infinite. @param width2 [Integer,nil] Width in characters for the second and

subsequent lines, or `nil` to use the same as width.

@return [Array<String>] Wrapped lines

# File lib/toys/wrappable_string.rb, line 74
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