JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
init: encode terminal state as terminal output
authorJason Woofenden <jason@jasonwoof.com>
Fri, 1 Feb 2013 04:20:06 +0000 (23:20 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Fri, 1 Feb 2013 04:20:06 +0000 (23:20 -0500)
client.coffee
server.coffee
terminal.coffee

index 6749a60..e5615af 100644 (file)
@@ -141,12 +141,4 @@ $ ->
 
        socket.on 'init', (v) ->
                term = terminal.new(v.width, v.height)
 
        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
index b5a6ad5..06f6aed 100644 (file)
@@ -49,7 +49,50 @@ io.sockets.on 'connection', (socket) ->
                                sockets.splice i, 1
                                return
 
                                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'
 
 process.stdin.resume()
 process.stdin.setEncoding 'utf8'
index 8b7879b..242d246 100644 (file)
@@ -460,7 +460,7 @@ class Terminal
                                        @a = 0
                return
 
                                        @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'
        csi_r: (top, bottom) ->
                top = -1 + parseInt @fix_esc_arg top, '1'
                bottom = -1 + parseInt @fix_esc_arg bottom, '10000'