JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
move output to browser (from inspector)
[watch-my-terminal.git] / terminal.coffee
1 async = require 'async'
2 fs = require 'fs'
3
4 class Terminal
5         # public:
6         constructor: (width, height) ->
7                 @width = 1
8                 @height = 1
9                 @text = [""]
10                 @attributes = [0]
11                 @x = 0
12                 @y = 0
13                 @partial = ''
14                 @resize width, height
15                 @text_callbacks = []
16                 @sequence_callbacks = []
17         
18         on: (evt, callback) ->
19                 @["#{evt}_callbacks"].push callback
20
21         resize: (width, height) ->
22                 # FIXME: write a version that retains some of the data
23                 @text = []
24                 @attributes = []
25                 for i in [0...height]
26                         @text[i] = ""
27                         @attributes[i] = new Array(width)
28
29         # pass data from stdout
30         update: (data) ->
31                 return unless data?.length > 0
32                 if @partial.length > 0
33                         data = @partial + data
34                         @partial = ''
35                 parts = data.split(/\x1b\[/)
36                 if parts.length > 1
37                         if -1 is @escape_sequence_length parts[parts.length - 1]
38                                 @partial = parts.pop()
39                 if parts.length > 0
40                         for i in [0...parts.length]
41                                 if i is 0
42                                         @update_text parts[i]
43                                 else
44                                         @update_sequence_then_text parts[i]
45                 return
46
47         # str has no escape sequences
48         update_text: (str) ->
49                 return unless str.length > 0
50                 for c in @text_callbacks
51                         c str
52                 console.log "text: \"#{str}\"" # FIXME
53
54         # str is the whole escape sequence (minus the esc[ prefix)
55         update_sequence: (str) ->
56                 for c in @sequence_callbacks
57                         c str
58                 console.log "sequence: \"#{str}\"" # FIXME
59
60         update_sequence_then_text: (str) ->
61                 len = @escape_sequence_length str
62                 if len is -1
63                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
64                         @update_text "ESC[" + str
65                 else
66                         @update_sequence str.substr 0, len
67                         @update_text str.substr len
68
69         escape_sequence_length: (str) ->
70                 parts = str.match(/^[0-9;?]{0,25}./)
71                 return -1 unless parts?
72                 return parts[0].length
73
74 exports.new = (width, height) ->
75         return new Terminal width, height