JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Merge branch 'feature/socket-test' into develop
[watch-my-terminal.git] / parse.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
16         resize: (width, height) ->
17                 # FIXME: write a version that retains some of the data
18                 @text = []
19                 @attributes = []
20                 for i in [0...height]
21                         @text[i] = ""
22                         @attributes[i] = new Array(width)
23
24         # pass data from stdout
25         update: (data) ->
26                 return unless data?.length > 0
27                 if @partial.length > 0
28                         data = @partial + data
29                         @partial = ''
30                 parts = data.split(/\x1b\[/)
31                 if parts.length > 1
32                         if -1 is @escape_sequence_length parts[parts.length - 1]
33                                 @partial = parts.pop()
34                 if parts.length > 0
35                         for i in [0...parts.length]
36                                 if i is 0
37                                         @update_text parts[i]
38                                 else
39                                         @update_sequence_then_text parts[i]
40                 return
41
42         # str has no escape sequences
43         update_text: (str) ->
44                 return unless str.length > 0
45                 console.log "text: \"#{str}\"" # FIXME
46
47         # str is the whole escape sequence (minus the esc[ prefix)
48         update_sequence: (str) ->
49                 console.log "sequence: \"#{str}\"" # FIXME
50
51         update_sequence_then_text: (str) ->
52                 len = @escape_sequence_length str
53                 if len is -1
54                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
55                         @update_text "ESC[" + str
56                 else
57                         @update_sequence str.substr 0, len
58                         @update_text str.substr len
59
60         escape_sequence_length: (str) ->
61                 parts = str.match(/^[0-9;?]{0,25}./)
62                 return -1 unless parts?
63                 return parts[0].length