JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
init: encode terminal state as terminal output
[watch-my-terminal.git] / terminal.coffee
index 12d8a74..242d246 100644 (file)
@@ -7,6 +7,11 @@ else
        window.terminal = {}
        my_exports = window.terminal
 
+if console?.log?
+       log = -> console.log arguments...
+else
+       log = -> null
+
 class Terminal
        # public:
        constructor: (width, height) ->
@@ -19,10 +24,14 @@ class Terminal
                @a = 0x000007 # cursor attributes
                @partial = ''
                @saved_normal_screen = null
+               @cursor_visible = true
+               @scroll_top = 0
+               @scroll_bottom = height - 1
                @resize width, height
 
        resize: (width, height) ->
                # FIXME: write a version that retains some of the data
+               # FIXME: clamp variables (eg x, y, saved.*, scrolling region) if getting smaller
                @width = width
                @height = height
                @text = []
@@ -32,7 +41,7 @@ class Terminal
                        @attributes[y] = []
                        for x in [0...width]
                                @text[y].push ' '
-                               @attributes[y].push 0
+                               @attributes[y].push 0x07
 
        # pass data from stdout
        update: (data) ->
@@ -40,33 +49,38 @@ class Terminal
                if @partial.length > 0
                        data = @partial + data
                        @partial = ''
-               parts = data.split(/\x1b\[/)
+               parts = data.split(/\x1b/)
                if parts.length > 1
                        if -1 is @escape_sequence_length parts[parts.length - 1]
                                @partial = parts.pop()
-               if parts.length > 0
-                       for i in [0...parts.length]
-                               if i is 0
-                                       @update_text parts[i]
-                               else
-                                       @update_sequence_then_text parts[i]
+               for i in [0...parts.length]
+                       if i is 0
+                               @update_text parts[i]
+                       else
+                               @update_sequence_then_text parts[i]
                return
 
        add_new_line: ->
-               # clear top line
+               # clear the line at the top of the scrolling region
                for i in [0...@width]
-                       @text[0][i] = ' '
-                       @attributes[0][i] = 0
-               # move (newly cleared) top line to the bottom
-               tmp = @text.shift()
-               @text.push(tmp)
-               tmp = @attributes.shift()
-               @attributes.push(tmp)
+                       @text[@scroll_top][i] = ' '
+                       @attributes[@scroll_top][i] = 0x07
+
+               rearrange = (a) =>
+                       return [
+                               a[0...@scroll_top]..., # up to but not including scroll top
+                               a[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
+                               a[@scroll_top], # top line of scroll region (already cleared)
+                               a[@scroll_bottom + 1 ... @height]... # rest of screen
+                       ]
+               @text = rearrange @text
+               @attributes = rearrange @attributes
+
                # slide cursor up with rest of text
                @y -= 1
 
        wrap_to_next_line: ->
-               if @y is @height - 1
+               if @y is @scroll_bottom
                        @add_new_line()
                @y += 1
                @x = 0
@@ -120,6 +134,7 @@ class Terminal
                @y -= lines
                if @y < 0
                        @y = 0
+               return
 
        # move cursor down
        csi_B: (lines) ->
@@ -127,6 +142,7 @@ class Terminal
                @y += lines
                if @y >= @height
                        @y = @height - 1
+               return
 
        # move cursor right
        csi_C: (cols) ->
@@ -134,6 +150,7 @@ class Terminal
                @x += cols
                if @x >= @width
                        @x = @width - 1
+               return
 
        # move cursor left
        csi_D: (cols) ->
@@ -141,6 +158,7 @@ class Terminal
                @x -= cols
                if @x < 0
                        @x = 0
+               return
 
        # set cursor position (one based)
        csi_H: (row, column) ->
@@ -161,6 +179,7 @@ class Terminal
                #move the cursor
                @x = column
                @y = row
+               return
 
        # clear to screen edge(es)
        csi_J: (direction) ->
@@ -187,7 +206,7 @@ class Terminal
                                                @text[row][i] = ' '
                                                @attributes[row][i] = @a
                        else
-                               console.log "confusing arg for csi_J: #{direction}"
+                               log "confusing arg for csi_J: #{direction}"
                return
 
        # clear (some or all of) current line
@@ -211,18 +230,61 @@ class Terminal
                                        @text[@y][i] = ' '
                                        @attributes[@y][i] = @a
                        else
-                               console.log "confusing arg for csi_K: #{direction}"
+                               log "confusing arg for csi_K: #{direction}"
                return
-       
+
+       # move lines downwards (arg is how far)
+       csi_L: (lines) ->
+               lines = parseInt @fix_esc_arg lines, '1'
+
+               rearrange = (a) =>
+                       return [
+                               a[0...@y]..., # keep everything above cursor
+                               a[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
+                               a[@y..@scroll_bottom - lines]..., # lines that are moving down
+                               a[@scroll_bottom + 1 ... @height]... # rest of screen
+                       ]
+               @text = rearrange @text
+               @attributes = rearrange @attributes
+
+               # clear the lines we scrolled off (and put back in as "new")
+               for y in [@y...@y+lines]
+                       for x in [0...@width]
+                               @text[y][x] = ' '
+                               @attributes[y][x] = 0x07
+
+       # move lines upwards (arg is how far)
+       # this obliterates the line under the cursor and arg-1 following it
+       csi_M: (lines) ->
+               lines = parseInt @fix_esc_arg lines, '1'
+
+               rearrange = (a) =>
+                       return [
+                               a[0 ... @y]..., # keep everything above cursor
+                               a[@y + lines .. @scroll_bottom]..., # lines we're moving up
+                               a[@y ... @y + lines]..., # recycle these
+                               a[@scroll_bottom + 1 ... @height]... # keep the rest
+                       ]
+               @text = rearrange @text
+               @attributes = rearrange @attributes
+
+               # clear the lines we're recycling
+               for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
+                       for x in [0...@width]
+                               @text[y][x] = ' '
+                               @attributes[y][x] = 0x07
+
        # misc
        csiq_h: ->
                args = []
                for i in arguments
                        arg = @fix_esc_arg i, ''
                        switch arg
+                               when '25'
+                                       @cursor_visible = true
                                when '1049'
                                        if @saved_normal_screen?
-                                               console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
+                                               log "ignoring request to switch to the alt screen because we're already on the alt screen"
                                                return
                                        @saved_normal_screen = [@x, @y, @text, @attributes]
                                        @text = []
@@ -232,18 +294,22 @@ class Terminal
                                                @attributes[y] = []
                                                for x in [0...@width]
                                                        @text[y].push ' '
-                                                       @attributes[y].push 0
+                                                       @attributes[y].push 0x07
                                else
-                                       console.log "confusing arg for csiq_h: #{arg}"
+                                       log "confusing arg for csiq_h: #{arg}"
+               return
+
        # unmisc
        csiq_l: ->
                args = []
                for i in arguments
                        arg = @fix_esc_arg i, ''
                        switch arg
+                               when '25'
+                                       @cursor_visible = false
                                when '1049'
                                        if not @saved_normal_screen?
-                                               console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
+                                               log "ignoring request to switch to the normal screen because we're already on the normal screen"
                                                return
                                        @x = @saved_normal_screen[0]
                                        @y = @saved_normal_screen[1]
@@ -251,7 +317,8 @@ class Terminal
                                        @attributes = @saved_normal_screen[3]
                                        @saved_normal_screen = null
                                else
-                                       console.log "confusing arg for csiq_l: #{arg}"
+                                       log "confusing arg for csiq_l: #{arg}"
+               return
 
        # set color, bold, underline, etc
        csi_m: ->
@@ -389,36 +456,66 @@ class Terminal
 
                                else
                                        # if we don't recognize the style, go back to default
-                                       console.log "unrecognized csi_m arg: \"#{arg}\""
+                                       log "unrecognized csi_m arg: \"#{arg}\""
                                        @a = 0
                return
 
+       # set scroll region
+       csi_r: (top, bottom) ->
+               top = -1 + parseInt @fix_esc_arg top, '1'
+               bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
+               if top < 0
+                       top = 0
+               if bottom >= @height
+                       bottom = @height - 1
+               @scroll_top = top
+               @scroll_bottom = bottom
+               return
+
+       # move cursor up one line, if it's at the top, scroll everything down
+       esc_M: ->
+               if @y > 0
+                       @csi_A '1'
+               else
+                       @csi_L '1'
+
        # str is the whole escape sequence (minus the esc[ prefix)
        update_sequence: (str) ->
-               prefix = 'csi_'
-               if str[0] is '?'
-                       prefix = 'csiq_'
-                       str = str.substr 1
+               if str[0] is '['
+                       prefix = 'csi_'
+                       if str[1] is '?'
+                               prefix = 'csiq_'
+                               str = str.substr 1
+                       args = str.substr(1, str.length - 2).split ';'
+               else
+                       prefix = 'esc_'
+                       args = [str.substr(0, str.length - 1)]
                command = @[prefix + str.substr(str.length - 1)]
                if not command?
-                       console.log "Unrecognized sequence: ESC[#{str}"
+                       log "Unrecognized sequence: ESC[#{str}"
                        return
-               args = str.substr(0, str.length - 1).split ';'
                command.call this, args...
 
        update_sequence_then_text: (str) ->
                len = @escape_sequence_length str
                if len is -1
-                       console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
-                       @update_text "ESC[" + str
+                       log "couldn't find escape sequence here: #{str.substr 0, 25}"
+                       @update_text "ESC" + str
                else
                        @update_sequence str.substr 0, len
                        @update_text str.substr len
 
        escape_sequence_length: (str) ->
-               parts = str.match(/^[0-9;?]{0,25}./)
-               return -1 unless parts?
-               return parts[0].length
+               if str[0] is '['
+                       parts = str.match(/^\[[0-9;?]{0,25}./)
+                       return -1 unless parts?
+                       return parts[0].length
+               else
+                       log "non[: ESC#{str.substr 0, 10}"
+                       if str.length >= 1
+                               return 1
+                       else
+                               return -1
 
 my_exports.new = (width, height) ->
        return new Terminal width, height