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'