Module: Txt2img::StringUtil

Included in:
Txt
Defined in:
lib/txt2img/string_util.rb

Instance Method Summary collapse

Instance Method Details

#size(string) ⇒ Object



6
7
8
# File 'lib/txt2img/string_util.rb', line 6

def size(string)
  string.display_width
end

#split_by_width(str, width) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/txt2img/string_util.rb', line 10

def split_by_width(str, width)
  chars = str.chars.to_a

  current_length = 0
  split_index    = 0

  chars.each_with_index do |c, i|
    char_width = self.size(c)
    break if current_length + char_width > width
    split_index = i + 1
    current_length += char_width
  end

  [chars[0, split_index].join, chars[split_index..-1].join]
end

#wrap_by_width(str, width) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/txt2img/string_util.rb', line 26

def wrap_by_width(str, width)
  lines = []

  while !str.empty? do
    head, tail = split_by_width(str, width)
    lines << head
    str = tail
  end

  lines.join("\n")
end