JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix and display colors, bold and underline
[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 = []
11                 @x = 0
12                 @y = 0
13                 @a = 0 # cursor attributes
14                 @partial = ''
15                 @resize width, height
16         
17         resize: (width, height) ->
18                 # FIXME: write a version that retains some of the data
19                 @width = width
20                 @height = height
21                 @text = []
22                 @attributes = []
23                 for y in [0...height]
24                         @text[y] = []
25                         @attributes[y] = []
26                         for x in [0...width]
27                                 @text[y].push ' '
28                                 @attributes[y].push 0
29
30         # pass data from stdout
31         update: (data) ->
32                 return unless data?.length > 0
33                 if @partial.length > 0
34                         data = @partial + data
35                         @partial = ''
36                 parts = data.split(/\x1b\[/)
37                 if parts.length > 1
38                         if -1 is @escape_sequence_length parts[parts.length - 1]
39                                 @partial = parts.pop()
40                 if parts.length > 0
41                         for i in [0...parts.length]
42                                 if i is 0
43                                         @update_text parts[i]
44                                 else
45                                         @update_sequence_then_text parts[i]
46                 return
47         
48         clear_rest_of_line: ->
49                 for i in [@x...@width]
50                         @text[@y][i] = ' '
51                         @attributes[@y][i] = @a
52         
53         add_new_line: ->
54                 # clear top line
55                 for i in [0...@width]
56                         @text[0][i] = ' '
57                         @attributes[0][i] = 0
58                 # move (newly cleared) top line to the bottom
59                 tmp = @text.shift()
60                 @text.push(tmp)
61                 tmp = @attributes.shift()
62                 @attributes.push(tmp)
63                 # slide cursor up with rest of text
64                 @y -= 1
65         
66         wrap_to_next_line: ->
67                 if @y is @height - 1
68                         @add_new_line()
69                 @y += 1
70                 @x = 0
71
72         # str has no escape sequences
73         update_text: (str) ->
74                 return unless str.length > 0
75                 for c in str
76                         switch c
77                                 when '\t' # tab
78                                         @update_text "        ".substr(@x % 8)
79                                 when '\x07' # bell
80                                         false
81                                 when '\x0d' # cr
82                                         @x = 0
83                                 when '\x08' # backspace
84                                         if @x > 0
85                                                 @x -= 1
86                                                 @text[@y][@x] = ' '
87                                                 # should this set the attribute too?
88                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
89                                         @wrap_to_next_line()
90                                 else
91                                         @text[@y][@x] = c
92                                         @attributes[@y][@x] = @a
93                                         @x += 1
94                                         if @x is @width
95                                                 @wrap_to_next_line()
96                 return
97         
98         set_attribute_bits: (mask, value) ->
99                 @a = (@a & ~mask) | value
100
101         csi_m: default: "0", go: ->
102                 for i in arguments
103                         fixed = i
104                         while fixed[0] is '0'
105                                 fixed = fixed.substr 1
106                         switch fixed
107                                 when ''
108                                         @set_attribute_bits 0xffffff, 0
109                                 when '1' # bold
110                                         @set_attribute_bits 0x10000, 0x10000
111                                 when '4' # underline
112                                         @set_attribute_bits 0x20000, 0x20000
113                                 when '5' # blink
114                                         @set_attribute_bits 0x40000, 0x40000
115                                 when '8' # invisible
116                                         @set_attribute_bits 0x80000, 0x80000
117                                 
118                                 when '22' # not bold... according to a page
119                                         @set_attribute_bits 0x10000, 0
120                                 when '21' # ... though this would make more sense for "not bold"
121                                         @set_attribute_bits 0x10000, 0
122                                 when '24' # not underline
123                                         @set_attribute_bits 0x20000, 0
124                                 when '25' # not blink
125                                         @set_attribute_bits 0x40000, 0
126                                 when '28' # not invisible
127                                         @set_attribute_bits 0x80000, 0
128
129                                 when '30' # fg black
130                                         @set_attribute_bits 0xff, 0
131                                 when '31' # fg red
132                                         @set_attribute_bits 0xff, 0xe0
133                                 when '32' # fg green
134                                         @set_attribute_bits 0xff, 0x1c
135                                 when '33' # fg yellow
136                                         @set_attribute_bits 0xff, 0xfc
137                                 when '34' # fg blue
138                                         @set_attribute_bits 0xff, 0x03
139                                 when '35' # fg magenta
140                                         @set_attribute_bits 0xff, 0xe2
141                                 when '36' # fg cyan
142                                         @set_attribute_bits 0xff, 0x1f
143                                 when '37', '39' # fg white  (39 is default)
144                                         @set_attribute_bits 0xff, 0xff
145
146                                 when '40' # bg black
147                                         @set_attribute_bits 0xff00, 0
148                                 when '41' # bg red
149                                         @set_attribute_bits 0xff00, 0xe000
150                                 when '42' # bg green
151                                         @set_attribute_bits 0xff00, 0x1c00
152                                 when '43' # bg yellow
153                                         @set_attribute_bits 0xff00, 0xfc00
154                                 when '44' # bg blue
155                                         @set_attribute_bits 0xff00, 0x0300
156                                 when '45' # bg magenta
157                                         @set_attribute_bits 0xff00, 0xe200
158                                 when '46' # bg cyan
159                                         @set_attribute_bits 0xff00, 0x1f00
160                                 when '47', '49' # bg white  (49 is default)
161                                         @set_attribute_bits 0xff00, 0xff
162
163                                 else
164                                         # if we don't recognize the style, go back to default
165                                         @set_attribute_bits 0xffffff, 0
166                 return
167
168         # str is the whole escape sequence (minus the esc[ prefix)
169         update_sequence: (str) ->
170                 command = @["csi_#{str.substr str.length - 1}"]
171                 return unless command?
172                 args = str.substr(0, str.length - 1).split ';'
173                 for i in [0...args.length]
174                         if args[i] is ''
175                                 args[i] = command.default
176                 command.go.call this, args...
177
178         update_sequence_then_text: (str) ->
179                 len = @escape_sequence_length str
180                 if len is -1
181                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
182                         @update_text "ESC[" + str
183                 else
184                         @update_sequence str.substr 0, len
185                         @update_text str.substr len
186
187         escape_sequence_length: (str) ->
188                 parts = str.match(/^[0-9;?]{0,25}./)
189                 return -1 unless parts?
190                 return parts[0].length
191
192 exports.new = (width, height) ->
193         return new Terminal width, height