Skip to content

Commit 48f5eee

Browse files
committed
move message splitting to own method
1 parent 01e5716 commit 48f5eee

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

lib/cinch/target.rb

+25-23
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,10 @@ def send(text, notice = false)
3636
split_start = @bot.config.message_split_start || ""
3737
split_end = @bot.config.message_split_end || ""
3838
command = notice ? "NOTICE" : "PRIVMSG"
39-
max_bytesize = 510 - ":#{@bot.mask} #{command} #{@name} :".bytesize
40-
max_bytesize_without_end = max_bytesize - split_end.bytesize
39+
prefix = ":#{@bot.mask} #{command} #{@name} :"
4140

4241
text.lines.map(&:chomp).each do |line|
43-
if line.bytesize <= max_bytesize
44-
@bot.irc.send("#{command} #@name :#{line}")
45-
next
46-
end
47-
48-
splitted = []
49-
acc = 0
50-
acc_rune_sizes = line.each_char.map {|ch|
51-
acc += ch.bytesize
52-
}
53-
while line.bytesize > max_bytesize_without_end
54-
max_rune = acc_rune_sizes.rindex {|bs| bs <= max_bytesize_without_end}
55-
max_rune ||= 0
56-
r = line.rindex(/\s/, max_rune) || max_rune
57-
if r == 0
58-
r = 1
59-
end
60-
splitted << (line[0...r] + split_end.tr(" ", "\u00A0"))
61-
line = split_start.tr(" ", "\u00A0") + line[r..-1].lstrip
62-
end
63-
splitted << line
42+
splitted = split_message(line, prefix, split_start, split_end)
6443

6544
splitted[0, (@bot.config.max_messages || splitted.size)].each do |string|
6645
string.tr!("\u00A0", " ") # clean string from any non-breaking spaces
@@ -188,5 +167,28 @@ def <=>(other)
188167
nil
189168
end
190169
end
170+
171+
private
172+
def split_message(msg, prefix, split_start, split_end)
173+
max_bytesize = 510 - prefix.bytesize
174+
max_bytesize_without_end = max_bytesize - split_end.bytesize
175+
176+
if msg.bytesize <= max_bytesize
177+
return [msg]
178+
end
179+
180+
splitted = []
181+
acc = 0
182+
acc_rune_sizes = msg.each_char.map {|ch|
183+
acc += ch.bytesize
184+
}
185+
while msg.bytesize > max_bytesize_without_end
186+
max_rune = acc_rune_sizes.rindex {|bs| bs <= max_bytesize_without_end} || 0
187+
r = [msg.rindex(/\s/, max_rune) || max_rune, 1].max
188+
splitted << (msg[0...r] + split_end.tr(" ", "\u00A0"))
189+
msg = split_start.tr(" ", "\u00A0") + msg[r..-1].lstrip
190+
end
191+
splitted << msg
192+
end
191193
end
192194
end

0 commit comments

Comments
 (0)