JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
initial parsing code (finds escape sequences)
authorJason Woofenden <jason@jasonwoof.com>
Wed, 30 Jan 2013 05:02:43 +0000 (00:02 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 30 Jan 2013 05:02:43 +0000 (00:02 -0500)
parse.coffee [new file with mode: 0644]

diff --git a/parse.coffee b/parse.coffee
new file mode 100644 (file)
index 0000000..b3dfed6
--- /dev/null
@@ -0,0 +1,63 @@
+async = require 'async'
+fs = require 'fs'
+
+class Terminal
+       # public:
+       constructor: (width, height) ->
+               @width = 1
+               @height = 1
+               @text = [""]
+               @attributes = [0]
+               @x = 0
+               @y = 0
+               @partial = ''
+               @resize width, height
+
+       resize: (width, height) ->
+               # FIXME: write a version that retains some of the data
+               @text = []
+               @attributes = []
+               for i in [0...height]
+                       @text[i] = ""
+                       @attributes[i] = new Array(width)
+
+       # pass data from stdout
+       update: (data) ->
+               return unless data?.length > 0
+               if @partial.length > 0
+                       data = @partial + data
+                       @partial = ''
+               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]
+               return
+
+       # str has no escape sequences
+       update_text: (str) ->
+               return unless str.length > 0
+               console.log "text: \"#{str}\"" # FIXME
+
+       # str is the whole escape sequence (minus the esc[ prefix)
+       update_sequence: (str) ->
+               console.log "sequence: \"#{str}\"" # FIXME
+
+       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
+               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