module Textbubble::String::InstanceMethods

Extra methods for Strings implementing Textbubbles

Public Instance Methods

lbubble(width: 60, fg: :black, bg: :white) click to toggle source

Creates a bubble on the left hand side

# File lib/textbubble.rb, line 26
def lbubble(width: 60, fg: :black, bg: :white)
  lines = wrap_and_split width
  max = lines.map(&:length).max || 0
  output = ''
  0.upto(lines.length - 2) do |i|
    output += ' ' + lines[i].pad(max).color(fg).background(bg) + "\n"
  end if lines.length > 1
  output += '◢'.color(bg) +
            lines[-1].pad(max).color(fg).background(bg) + "\n"
end
pad(max_length) click to toggle source

Add a space and left justify

# File lib/textbubble.rb, line 39
def pad(max_length)
  ' ' + ljust(max_length) + ' '
end
rbubble(margin: 80, width: nil, fg: :black, bg: :white) click to toggle source

Creates a bubble on the right hand side

# File lib/textbubble.rb, line 11
def rbubble(margin: 80, width: nil, fg: :black, bg: :white)
  lines = wrap_and_split(width.nil? ? (margin * 0.6).to_i : width)
  max = lines.map(&:length).max || 0
  output = ''
  pad = (margin - max - 3) > 0 ? margin - max - 3 : 0
  0.upto(lines.length - 2) do |i|
    output +=
      ' ' * pad + lines[i].pad(max).color(fg).background(bg) + "\n"
  end if lines.length > 1
  output += ' ' * pad + lines[-1].pad(max).color(fg).background(bg) +
            '◣'.color(bg) + "\n"
end

Private Instance Methods

wrap(margin) click to toggle source
# File lib/textbubble.rb, line 45
def wrap(margin)
  margin = 40 if margin.nil? || margin < 2
  split("\n").collect do |line|
    if line.length > margin
      line.gsub(/(.{1,#{margin}})(\s+|$)/, "\\1\n").strip \
    else
      line
    end
  end * "\n"
end
wrap_and_split(width) click to toggle source
# File lib/textbubble.rb, line 56
def wrap_and_split(width)
  lines = wrap(width).split "\n"
  lines == [] ? [''] : lines
end