|
@@ -886,30 +886,43 @@ func topicHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
886
|
886
|
return false
|
887
|
887
|
}
|
888
|
888
|
|
|
889
|
+// wordWrap wraps the given text into a series of lines that don't exceed lineWidth characters.
|
889
|
890
|
func wordWrap(text string, lineWidth int) []string {
|
890
|
891
|
var lines []string
|
891
|
892
|
var cacheLine, cacheWord string
|
892
|
893
|
|
893
|
894
|
for _, char := range text {
|
894
|
|
- if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
|
|
895
|
+ if char == '\r' {
|
|
896
|
+ continue
|
|
897
|
+ } else if char == '\n' {
|
|
898
|
+ cacheLine += cacheWord
|
|
899
|
+ lines = append(lines, cacheLine)
|
|
900
|
+ cacheWord = ""
|
|
901
|
+ cacheLine = ""
|
|
902
|
+ } else if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
|
|
903
|
+ // natural word boundary
|
895
|
904
|
cacheLine += cacheWord + string(char)
|
896
|
905
|
cacheWord = ""
|
897
|
|
- } else if len(cacheLine)+len(cacheWord)+1 >= lineWidth {
|
|
906
|
+ } else if lineWidth <= len(cacheLine)+len(cacheWord)+1 {
|
|
907
|
+ // time to wrap to next line
|
898
|
908
|
if len(cacheLine) < (lineWidth / 2) {
|
899
|
|
- // there must be a really long word or something, just split on word boundary
|
|
909
|
+ // this word takes up more than half a line... just split in the middle of the word
|
900
|
910
|
cacheLine += cacheWord + string(char)
|
901
|
911
|
cacheWord = ""
|
|
912
|
+ } else {
|
|
913
|
+ cacheWord += string(char)
|
902
|
914
|
}
|
903
|
915
|
lines = append(lines, cacheLine)
|
904
|
916
|
cacheLine = ""
|
905
|
917
|
} else {
|
|
918
|
+ // normal character
|
906
|
919
|
cacheWord += string(char)
|
907
|
920
|
}
|
908
|
921
|
}
|
909
|
|
- if len(cacheWord) > 0 {
|
|
922
|
+ if 0 < len(cacheWord) {
|
910
|
923
|
cacheLine += cacheWord
|
911
|
924
|
}
|
912
|
|
- if len(cacheLine) > 0 {
|
|
925
|
+ if 0 < len(cacheLine) {
|
913
|
926
|
lines = append(lines, cacheLine)
|
914
|
927
|
}
|
915
|
928
|
|