X-Git-Url: https://jasonwoof.com/gitweb/?p=watch-my-terminal.git;a=blobdiff_plain;f=server.coffee;fp=server.coffee;h=06f6aed56f55f4d9dbe5c90517d6d8f655a07664;hp=b5a6ad5e4dff09430c44a252bfdb8b24e69be6a8;hb=37d536e72e3e6fa4de536ad916a0a80119f15509;hpb=238684c497185ba7d18860770dc24096c172562b diff --git a/server.coffee b/server.coffee index b5a6ad5..06f6aed 100644 --- a/server.coffee +++ b/server.coffee @@ -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, cursor_visible: term.cursor_visible, scroll_top: term.scroll_top, scroll_bottom: term.scroll_bottom + + 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'