3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

Use a better line-splitting algorithm

This commit is contained in:
Daniel Oaks 2017-01-18 08:05:31 +10:00
parent aad7aaba07
commit cbe49ff3d8
2 changed files with 26 additions and 37 deletions

View File

@ -500,10 +500,10 @@ func (client *Client) destroy() {
// Adds account-tag to the line as well. // Adds account-tag to the line as well.
func (client *Client) SendSplitMsgFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command, target string, message SplitMessage) { func (client *Client) SendSplitMsgFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command, target string, message SplitMessage) {
if client.capabilities[MaxLine] { if client.capabilities[MaxLine] {
client.SendFromClient(msgid, from, tags, from.nickMaskString, command, target, message.ForMaxLine) client.SendFromClient(msgid, from, tags, command, target, message.ForMaxLine)
} else { } else {
for _, str := range message.For512 { for _, str := range message.For512 {
client.SendFromClient(msgid, from, tags, from.nickMaskString, command, target, str) client.SendFromClient(msgid, from, tags, command, target, str)
} }
} }
} }

View File

@ -853,45 +853,34 @@ func topicHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
return false return false
} }
// func wordWrap(text string, lineWidth int) []string {
// var split []string
// var cache, cacheLastWord string
// for _, char := range text {
// if char == " " {
// cache += cacheLastWord + char
// continue
// }
// cacheLastWord += char
// if cache + cacheLastWord ==
// if len(cacheLastWord) >= lineWidth
// }
// }
// taken from https://gist.github.com/kennwhite/306317d81ab4a885a965e25aa835b8ef
func wordWrap(text string, lineWidth int) []string { func wordWrap(text string, lineWidth int) []string {
var split []string var lines []string
words := strings.Fields(text) var cacheLine, cacheWord string
if len(words) == 0 {
return split
}
cache := words[0]
spaceLeft := lineWidth - len(cache)
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
split = append(split, cache)
cache = word
spaceLeft = lineWidth - len(word)
} else {
cache += " " + word
spaceLeft -= 1 + len(word)
}
}
split = append(split, cache)
return split for _, char := range text {
if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
cacheLine += cacheWord + string(char)
cacheWord = ""
} else if len(cacheLine)+len(cacheWord)+1 >= lineWidth {
if len(cacheLine) < (lineWidth / 2) {
// there must be a really long word or something, just split on word boundary
cacheLine += cacheWord + string(char)
cacheWord = ""
}
lines = append(lines, cacheLine)
cacheLine = ""
} else {
cacheWord += string(char)
}
}
if len(cacheWord) > 0 {
cacheLine += cacheWord
}
if len(cacheLine) > 0 {
lines = append(lines, cacheLine)
}
return lines
} }
// SplitMessage represents a message that's been split for sending. // SplitMessage represents a message that's been split for sending.
@ -905,7 +894,7 @@ func (server *Server) splitMessage(original string, origIs512 bool) SplitMessage
newSplit.ForMaxLine = original newSplit.ForMaxLine = original
if !origIs512 && len(original) > 400 { if !origIs512 {
newSplit.For512 = wordWrap(original, 400) newSplit.For512 = wordWrap(original, 400)
} else { } else {
newSplit.For512 = []string{original} newSplit.For512 = []string{original}