1 async = require 'async'
6 constructor: (width, height) ->
16 @sequence_callbacks = []
18 on: (evt, callback) ->
19 @["#{evt}_callbacks"].push callback
21 resize: (width, height) ->
22 # FIXME: write a version that retains some of the data
27 @attributes[i] = new Array(width)
29 # pass data from stdout
31 return unless data?.length > 0
32 if @partial.length > 0
33 data = @partial + data
35 parts = data.split(/\x1b\[/)
37 if -1 is @escape_sequence_length parts[parts.length - 1]
38 @partial = parts.pop()
40 for i in [0...parts.length]
44 @update_sequence_then_text parts[i]
47 # str has no escape sequences
49 return unless str.length > 0
50 for c in @text_callbacks
52 console.log "text: \"#{str}\"" # FIXME
54 # str is the whole escape sequence (minus the esc[ prefix)
55 update_sequence: (str) ->
56 for c in @sequence_callbacks
58 console.log "sequence: \"#{str}\"" # FIXME
60 update_sequence_then_text: (str) ->
61 len = @escape_sequence_length str
63 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
64 @update_text "ESC[" + str
66 @update_sequence str.substr 0, len
67 @update_text str.substr len
69 escape_sequence_length: (str) ->
70 parts = str.match(/^[0-9;?]{0,25}./)
71 return -1 unless parts?
72 return parts[0].length
74 exports.new = (width, height) ->
75 return new Terminal width, height