class Ark::TextBuilder

Build text progressively, line by line

Public Class Methods

new() click to toggle source

Initialize a TextBuilder instance

# File lib/ark/utility.rb, line 135
def initialize()
  @lines = [[]]
  @line  = 0
end

Public Instance Methods

add(str) click to toggle source

Add str to the last string on the line. This avoids a space between the strings when the text is printed

# File lib/ark/utility.rb, line 151
def add(str)
  @lines[@line][-1] += str.to_s
end
indent(count) click to toggle source

Indent the current line by count columns

# File lib/ark/utility.rb, line 169
def indent(count)
  @lines[@line].unshift(' ' * (count - 1))
end
next(str=nil) click to toggle source

Start a new line. If str is provided, push str onto the new line

# File lib/ark/utility.rb, line 174
def next(str=nil)
  @lines << []
  @line  += 1
  self.push(str) if str
end
print() click to toggle source

Print the constructed text

push(str) click to toggle source

Push a string onto the current line

# File lib/ark/utility.rb, line 141
def push(str)
  if str.is_a?(Array)
    @lines[@line] += str.map(&:to_s)
  else
    @lines[@line] << str.to_s
  end
end
skip(str=nil) click to toggle source

Insert a blank line and start the line after it. If str is given, push str onto the new line.

# File lib/ark/utility.rb, line 182
def skip(str=nil)
  self.next()
  self.next(str)
end
wrap(width: 78, indent: 0, indent_after: false, segments: false) click to toggle source

Wrap the current line to width, with an optional indent. After wrapping, the current line will be the last line wrapped.

# File lib/ark/utility.rb, line 157
def wrap(width: 78, indent: 0, indent_after: false, segments: false)
  if segments
    text = Text.wrap_segments(@lines[@line], width: width, indent: indent, indent_after: indent_after)
  else
    text = Text.wrap(@lines[@line], width: width, indent: indent, indent_after: indent_after)
  end
  @lines.delete_at(@line)
  @line -= 1
  text.split("\n").each {|line| self.next(line) }
end