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:
parent
aad7aaba07
commit
cbe49ff3d8
@ -500,10 +500,10 @@ func (client *Client) destroy() {
|
||||
// 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) {
|
||||
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 {
|
||||
for _, str := range message.For512 {
|
||||
client.SendFromClient(msgid, from, tags, from.nickMaskString, command, target, str)
|
||||
client.SendFromClient(msgid, from, tags, command, target, str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -853,45 +853,34 @@ func topicHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
||||
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 {
|
||||
var split []string
|
||||
words := strings.Fields(text)
|
||||
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)
|
||||
var lines []string
|
||||
var cacheLine, cacheWord string
|
||||
|
||||
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.
|
||||
@ -905,7 +894,7 @@ func (server *Server) splitMessage(original string, origIs512 bool) SplitMessage
|
||||
|
||||
newSplit.ForMaxLine = original
|
||||
|
||||
if !origIs512 && len(original) > 400 {
|
||||
if !origIs512 {
|
||||
newSplit.For512 = wordWrap(original, 400)
|
||||
} else {
|
||||
newSplit.For512 = []string{original}
|
||||
|
Loading…
Reference in New Issue
Block a user