From 52a45f8c73269d2b331dd9c90077487d72264c7c Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Fri, 30 Sep 2011 06:28:26 +0100 Subject: [PATCH] Better colour handling code No longer bugs out when multiple colours are used without a terminating \x03 byte between them. Should fix Issue #38. --- js/front.js | 128 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 45 deletions(-) diff --git a/js/front.js b/js/front.js index 47d410c..2934088 100644 --- a/js/front.js +++ b/js/front.js @@ -451,52 +451,90 @@ kiwi.front = { } // colour - re = /\x03([0-9][0-5]?)(,([0-9][0-5]?))?(.*?)\x03/g; - - msg = msg.replace(re, function (str, p1, p2, p3, p4) { - var fg, bg, - col = function (num) { - switch (parseInt(num, 10)) { - case 0: - return '#FFFFFF'; - case 1: - return '#000000'; - case 2: - return '#000080'; - case 3: - return '#008000'; - case 4: - return '#FF0000'; - case 5: - return '#800040'; - case 6: - return '#800080'; - case 7: - return '#FF8040'; - case 8: - return '#FFFF00'; - case 9: - return '#80FF00'; - case 10: - return '#008080'; - case 11: - return '#00FFFF'; - case 12: - return '#0000FF'; - case 13: - return '#FF55FF'; - case 14: - return '#808080'; - case 15: - return '#C0C0C0'; - default: - return null; + msg = (function (msg) { + var replace, colourMatch, col, i, match, to, endCol, fg, bg, str; + replace = ''; + colourMatch = function (str) { + var re = /^\x03([0-9][0-5]?)(,([0-9][0-5]?))?/; + return re.exec(str); + }; + col = function (num) { + switch (parseInt(num, 10)) { + case 0: + return '#FFFFFF'; + case 1: + return '#000000'; + case 2: + return '#000080'; + case 3: + return '#008000'; + case 4: + return '#FF0000'; + case 5: + return '#800040'; + case 6: + return '#800080'; + case 7: + return '#FF8040'; + case 8: + return '#FFFF00'; + case 9: + return '#80FF00'; + case 10: + return '#008080'; + case 11: + return '#00FFFF'; + case 12: + return '#0000FF'; + case 13: + return '#FF55FF'; + case 14: + return '#808080'; + case 15: + return '#C0C0C0'; + default: + return null; + } + }; + if (msg.indexOf('\x03') !== -1) { + i = msg.indexOf('\x03'); + replace = msg.substr(0, i); + while (i < msg.length) { + match = colourMatch(msg.substr(i, 6)); + if (match) { + //console.log(match); + // Next colour code + to = msg.indexOf('\x03', i + 1); + endCol = msg.indexOf(String.fromCharCode(15), i + 1); + if (endCol !== -1) { + if (to === -1) { + to = endCol; + } else { + to = ((to < endCol) ? to : endCol); + } + } + if (to === -1) { + to = msg.length; + } + //console.log(i, to); + fg = col(match[1]); + bg = col(match[3]); + str = msg.substring(i + 1 + match[1].length + ((bg !== null) ? match[2].length + 1 : 0), to); + //console.log(str); + replace += '' + str + ''; + i = to; + } else { + if ((msg[i] !== '\x03') && (msg[i] !== String.fromCharCode(15))) { + replace += msg[i]; + } + i++; } - }; - fg = col(p1); - bg = col(p3); - return '' + p4 + ''; - }); + } + return replace; + } + return msg; + }(msg)); + return msg; } -- 2.25.1