From 394a14939b00dbbb2a30c8a87ac3333a41c74ef2 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 30 Jan 2013 06:30:21 -0500 Subject: [PATCH] fix and display colors, bold and underline --- index.html | 38 +++++++++++++++++++++++++++++++++++++- server.coffee | 3 +++ terminal.coffee | 29 ++++++++++++++++------------- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 091da51..6fb1473 100644 --- a/index.html +++ b/index.html @@ -8,10 +8,46 @@ $(function() { var $body = $('body'); var socket = io.connect('http://localhost'); + var color_to_css = function(i) { + return 'color: #' + + ((i & 0xe0) ? 'ff' : '00') + + ((i & 0x1c) ? 'ff' : '00') + + ((i & 0x03) ? 'ff' : '00') + + '; '; + } + var stylize = function (txt, style) { + if(txt.length == 0 || style == 0) { + return txt + } + css = '' + if(style & 0x10000) css += 'font-weight: bold; '; + if(style & 0x20000) css += 'text-decoration: underline; '; + if(style & 0x40000) css += 'text-decoration: underline; '; // blink + if(style & 0x80000) css += 'text-decoration: line-through; '; // invisible + if(style & 0x000ff) css += color_to_css(style & 0xff); + if(style & 0x0ff00) css += 'background-' + color_to_css((style & 0xff00) >> 8); + return $('').text(txt); + } socket.on('init', function (v) { $body.children().remove(); for(i in v.text) { - $body.append($('
').text(v.text[i].join(''))); + div = $('
'); + txt = '' + a = 0; + for(j in v.text[i]) { + if(v.attributes[i][j] != a) { + if(txt.length) { + div.append(stylize(txt, a)); + txt = '' + } + a = v.attributes[i][j] + } + txt += v.text[i][j] + } + if(txt.length) { + div.append(stylize(txt, a)); + } + $body.append(div); } }); }); diff --git a/server.coffee b/server.coffee index 3ff1529..1d0422b 100644 --- a/server.coffee +++ b/server.coffee @@ -36,11 +36,14 @@ io.sockets.on 'connection', (socket) -> sockets.splice i, 1 return + socket.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes + process.stdin.resume() process.stdin.setEncoding 'utf8' process.stdin.on 'data', (data) -> term.update data + # FIXME send data, and have client parse it for s in sockets s.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes diff --git a/terminal.coffee b/terminal.coffee index 360fb1a..42732f4 100644 --- a/terminal.coffee +++ b/terminal.coffee @@ -100,28 +100,31 @@ class Terminal csi_m: default: "0", go: -> for i in arguments - switch i - when '0' + fixed = i + while fixed[0] is '0' + fixed = fixed.substr 1 + switch fixed + when '' @set_attribute_bits 0xffffff, 0 when '1' # bold - @set_attribute_bits 0x100, 1 + @set_attribute_bits 0x10000, 0x10000 when '4' # underline - @set_attribute_bits 0x200, 1 + @set_attribute_bits 0x20000, 0x20000 when '5' # blink - @set_attribute_bits 0x400, 1 + @set_attribute_bits 0x40000, 0x40000 when '8' # invisible - @set_attribute_bits 0x800, 1 + @set_attribute_bits 0x80000, 0x80000 when '22' # not bold... according to a page - @set_attribute_bits 0x100, 0 + @set_attribute_bits 0x10000, 0 when '21' # ... though this would make more sense for "not bold" - @set_attribute_bits 0x100, 0 + @set_attribute_bits 0x10000, 0 when '24' # not underline - @set_attribute_bits 0x200, 0 + @set_attribute_bits 0x20000, 0 when '25' # not blink - @set_attribute_bits 0x400, 0 + @set_attribute_bits 0x40000, 0 when '28' # not invisible - @set_attribute_bits 0x800, 0 + @set_attribute_bits 0x80000, 0 when '30' # fg black @set_attribute_bits 0xff, 0 @@ -132,7 +135,7 @@ class Terminal when '33' # fg yellow @set_attribute_bits 0xff, 0xfc when '34' # fg blue - @set_attribute_bits 0xff, 0x02 + @set_attribute_bits 0xff, 0x03 when '35' # fg magenta @set_attribute_bits 0xff, 0xe2 when '36' # fg cyan @@ -149,7 +152,7 @@ class Terminal when '43' # bg yellow @set_attribute_bits 0xff00, 0xfc00 when '44' # bg blue - @set_attribute_bits 0xff00, 0x0200 + @set_attribute_bits 0xff00, 0x0300 when '45' # bg magenta @set_attribute_bits 0xff00, 0xe200 when '46' # bg cyan -- 1.7.10.4