X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=server.coffee;h=06f6aed56f55f4d9dbe5c90517d6d8f655a07664;hb=37d536e72e3e6fa4de536ad916a0a80119f15509;hp=d66b6cd1c60b8b8d143903189734f688a3b3b91e;hpb=bd8f5b58663dd5870827c1d0dfa4f8eff8f6a5f0;p=watch-my-terminal.git diff --git a/server.coffee b/server.coffee index d66b6cd..06f6aed 100644 --- a/server.coffee +++ b/server.coffee @@ -36,7 +36,7 @@ fs = require('fs') terminal = require('./terminal.coffee') # SETTINGS -app.listen(9293) +app.listen(2218) term = terminal.new(104, 66) sockets = [] @@ -49,7 +49,50 @@ 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 + + enc_color = (prefix, c) -> + if c < 8 + return "#{prefix}#{c}" + if c < 16 + return "#{prefix + 6}#{c - 8}" + return "#{prefix}8;5;#{c}" + + attr_diff = (a, b) -> + xo = a ^ b + parts = [] + if (xo & 0xff) + parts.push enc_color 3, (b & 0xff) + if (xo & 0xff00) + parts.push enc_color 4, ((b & 0xff00) >> 8) + for [bit, code] in [[0x010000, '1'], [0x200000, '3'], [0x020000, '4'], [0x040000, '5'], [0x080000, '7'], [0x100000, '8']] + if (xo & bit) + if (b & bit) + parts.push code + else + parts.push '2' + code + if parts.length + return "\x1b[#{parts.join ';'}m" + else + return '' + + a = 0x07 + state = '' + # FIXME handle alt screen + for y in [0...term.height] + for x in [0...term.width] + if term.attributes[y][x] isnt a + state += attr_diff a, term.attributes[y][x] + a = term.attributes[y][x] + state += term.text[y][x] + if y < term.height - 1 + state += '\n' + state += attr_diff a, term.a + state += "\x1b[#{term.y + 1};#{term.x + 1}H" + unless term.cursor_visible + state += "\x1b[?25l" + unless term.scroll_top is 0 and term.scroll_bottom is term.height - 1 + state += "\x1b[#{term.scroll_top};#{term.scroll_bottom}r" + socket.emit 'init', width: term.width, height: term.height, text: state process.stdin.resume() process.stdin.setEncoding 'utf8'