8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/svn_campfire_notifier/wrap.rb', line 8
def self.wrap_chars(chars, max_size=80)
space_char = " ".chars.first
if chars.length > max_size and chars.include?(space_char)
if last_whitespace_index = chars[0...max_size].rindex(space_char)
left = chars[0...last_whitespace_index]
right = wrap_chars(chars[last_whitespace_index+1..-1], max_size)
left.empty? ? right : left + " \n".chars + right
elsif last_whitespace_index = chars[max_size..-1].index(space_char)
last_whitespace_index += max_size
left = wrap_chars(chars[0...last_whitespace_index], max_size)
right = wrap_chars(chars[last_whitespace_index+1..-1], max_size)
left.empty? ? right : left + " \n".chars + right
else
raise "We lost characters from the string, did it changes inbetween?"
end
else
chars
end
end
|