From 9bc33fa76eb376b0769daea4d133febf1a13a0c2 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sat, 27 Jul 2013 10:43:41 -0400 Subject: [PATCH] Adds color parsing Special thanks to @eirikb for developing this module in weechat.js --- js/websockets.js | 112 ++++++++++++++++++++++++++++++++++++++++++++++- websockets.html | 6 ++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index f009018..182c6e8 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -1,10 +1,116 @@ var weechat = angular.module('weechat', []); -weechat.factory('connection', ['$rootScope', function($scope) { +weechat.factory('colors', [function($scope) { + + // http://weechat.org/files/doc/devel/weechat_dev.en.html#color_codes_in_strings + var part, fg, bg, attrs, colors = ['', 'black', 'dark gray', 'dark red', 'light red', 'dark green', 'light green', 'brown', 'yellow', 'dark blue', 'light blue', 'dark magenta', 'light magenta', 'dark cyan', 'light cyan', 'gray', 'white']; + + function setAttrs() { + while (part.match(/^[\*\/\_\|]/)) { + attrs.push(part.charAt(0)); + part = part.slice(1); + } + } + + function getColor() { + var c; + if (part.match(/^@/)) { + c = part.slice(1, 5); + part = part.slice(5); + } else { + c = part.slice(0, 2); + part = part.slice(2); + } + return c; + } + + var prefixes = { + '\x19': function() { + if (part.match(/^F/)) { + part = part.slice(1); + setAttrs(); + fg = getColor(); + } else if (part.match(/^B/)) { + part = part.slice(1); + setAttrs(); + bg = getColor(); + } else { + setAttrs(); + fg = getColor(); + if (part.match(/^,/)) { + part = part.slice(1); + bg = getColor(); + } + } + }, + '\x1A': function() { + // Don't know what to do + }, + '\x1B': function() { + attrs = []; + }, + '\x1C': function() { + fg = ''; + bg = ''; + } + }; + + function parse(text) { + if (!text) { + return text; + } + var f, parts = text.split(/(\x19|\x1A|\x1B|\x1C)/); + if (parts.length === 1) return [{ + text: parts[0] + }]; + attrs = []; + + return parts.map(function(p) { + var res, tmp = prefixes[p.charAt(0)]; + if (f) { + part = p; + f(); + res = { + text: part, + fg: colors[parseInt(fg, 10)], + bg: colors[parseInt(bg, 10)], + attrs: attrs + }; + if (!res.fg) res.fg = fg; + if (!res.bg) res.bg = bg; + } + f = tmp; + return res; + }).filter(function(p) { + return p; + }); + }; + + + + + + + return { + + setAttrs: setAttrs, + getColor: getColor, + parse: parse, + parts: ['', 'black', 'dark gray', 'dark red', 'light red', 'dark green', 'light green', 'brown', 'yellow', 'dark blue', 'light blue', 'dark magenta', 'light magenta', 'dark cyan', 'light cyan', 'gray', 'white'] + } + + + + +}]); + + +weechat.factory('connection', ['$rootScope', 'colors', function($scope, colors) { protocol = new Protocol(); var websocket = null; var doSend = function(message) { + msgs = message.replace(/[\r\n]+$/g, "").split("\n"); for (var i = 0; i < msgs.length; i++) { console.log('=' + msgs[i] + '='); @@ -19,6 +125,7 @@ weechat.factory('connection', ['$rootScope', function($scope) { websocket.onopen = function (evt) { if (proto == "weechat") { doSend("init compression=off\nversion\n"); + doSend("sync\n"); } else { doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n"); } @@ -61,7 +168,8 @@ weechat.factory('connection', ['$rootScope', function($scope) { var handleBufferLineAdded = function(message) { - var buffer_line = message['objects'][0]['content'][0]['message']; + var buffer_line = colors.parse(message['objects'][0]['content'][0]['message']); + $scope.buffer.push(buffer_line); } diff --git a/websockets.html b/websockets.html index e2fd958..ff88220 100644 --- a/websockets.html +++ b/websockets.html @@ -38,7 +38,11 @@
- {{ bufferline }} +

+

+ {{ part.text }} +
+