From 37d536e72e3e6fa4de536ad916a0a80119f15509 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 31 Jan 2013 23:20:06 -0500 Subject: [PATCH] init: encode terminal state as terminal output --- client.coffee | 10 +--------- server.coffee | 45 ++++++++++++++++++++++++++++++++++++++++++++- terminal.coffee | 2 +- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/client.coffee b/client.coffee index 6749a60..e5615af 100644 --- a/client.coffee +++ b/client.coffee @@ -141,12 +141,4 @@ $ -> socket.on 'init', (v) -> term = terminal.new(v.width, v.height) - term.x = v.x - term.y = v.y - term.a = v.a - term.text = v.text - term.attributes = v.attributes - term.cursor_visible = v.cursor_visible - term.scroll_top = v.scroll_top - term.scroll_bottom = v.scroll_bottom - redraw() + update v.text 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' diff --git a/terminal.coffee b/terminal.coffee index 8b7879b..242d246 100644 --- a/terminal.coffee +++ b/terminal.coffee @@ -460,7 +460,7 @@ class Terminal @a = 0 return - # set scrolling region + # set scroll region csi_r: (top, bottom) -> top = -1 + parseInt @fix_esc_arg top, '1' bottom = -1 + parseInt @fix_esc_arg bottom, '10000' -- 1.7.10.4