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

Fix wordWrap function so it doesn't drop chars, and fix client.Notice() to automagically split very long lines.

This commit is contained in:
Daniel Oaks 2017-05-09 21:09:44 +10:00
parent 9fe7c143c8
commit d847d55c06
2 changed files with 27 additions and 7 deletions

View File

@ -623,7 +623,14 @@ func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, comm
} }
// Notice sends the client a notice from the server. // Notice sends the client a notice from the server.
//TODO(dan): Make this handle message splitting.
func (client *Client) Notice(text string) { func (client *Client) Notice(text string) {
client.Send(nil, client.server.name, "NOTICE", client.nick, text) limit := 400
if client.capabilities[MaxLine] {
limit = client.server.limits.LineLen.Rest - 110
}
lines := wordWrap(text, limit)
for _, line := range lines {
client.Send(nil, client.server.name, "NOTICE", client.nick, line)
}
} }

View File

@ -886,30 +886,43 @@ func topicHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
return false return false
} }
// wordWrap wraps the given text into a series of lines that don't exceed lineWidth characters.
func wordWrap(text string, lineWidth int) []string { func wordWrap(text string, lineWidth int) []string {
var lines []string var lines []string
var cacheLine, cacheWord string var cacheLine, cacheWord string
for _, char := range text { for _, char := range text {
if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth { if char == '\r' {
continue
} else if char == '\n' {
cacheLine += cacheWord
lines = append(lines, cacheLine)
cacheWord = ""
cacheLine = ""
} else if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
// natural word boundary
cacheLine += cacheWord + string(char) cacheLine += cacheWord + string(char)
cacheWord = "" cacheWord = ""
} else if len(cacheLine)+len(cacheWord)+1 >= lineWidth { } else if lineWidth <= len(cacheLine)+len(cacheWord)+1 {
// time to wrap to next line
if len(cacheLine) < (lineWidth / 2) { if len(cacheLine) < (lineWidth / 2) {
// there must be a really long word or something, just split on word boundary // this word takes up more than half a line... just split in the middle of the word
cacheLine += cacheWord + string(char) cacheLine += cacheWord + string(char)
cacheWord = "" cacheWord = ""
} else {
cacheWord += string(char)
} }
lines = append(lines, cacheLine) lines = append(lines, cacheLine)
cacheLine = "" cacheLine = ""
} else { } else {
// normal character
cacheWord += string(char) cacheWord += string(char)
} }
} }
if len(cacheWord) > 0 { if 0 < len(cacheWord) {
cacheLine += cacheWord cacheLine += cacheWord
} }
if len(cacheLine) > 0 { if 0 < len(cacheLine) {
lines = append(lines, cacheLine) lines = append(lines, cacheLine)
} }