Module: Textbubble::String::InstanceMethods

Defined in:
lib/textbubble.rb

Overview

Extra methods for Strings implementing Textbubbles

Instance Method Summary collapse

Instance Method Details

#lbubble(width: 60, fg: :black, bg: :white) ⇒ Object

Creates a bubble on the left hand side



26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object

Add a space and left justify



39
40
41
# File 'lib/textbubble.rb', line 39

def pad(max_length)
  ' ' + ljust(max_length) + ' '
end

#rbubble(margin: 80, width: nil, fg: :black, bg: :white) ⇒ Object

Creates a bubble on the right hand side



11
12
13
14
15
16
17
18
19
20
21
22
# 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