1 # this file is used by the client and server.
3 # work around lack of module system in the browser:
8 my_exports = window.terminal
12 constructor: (width, height) ->
19 @a = 0x000007 # cursor attributes
21 @saved_normal_screen = null
24 resize: (width, height) ->
25 # FIXME: write a version that retains some of the data
37 # pass data from stdout
39 return unless data?.length > 0
40 if @partial.length > 0
41 data = @partial + data
43 parts = data.split(/\x1b\[/)
45 if -1 is @escape_sequence_length parts[parts.length - 1]
46 @partial = parts.pop()
48 for i in [0...parts.length]
52 @update_sequence_then_text parts[i]
60 # move (newly cleared) top line to the bottom
63 tmp = @attributes.shift()
65 # slide cursor up with rest of text
74 # str has no escape sequences
76 return unless str.length > 0
80 @update_text " ".substr(@x % 8)
85 when '\x08' # backspace
92 when '\x0a', '\x0b' # lf, vertical tab (same thing)
98 @attributes[@y][@x] = @a
102 set_attribute_bits: (mask, value) ->
103 @a = ((@a & ~mask) | value)
105 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
106 # in the default for blank or missing values
107 fix_esc_arg: (value, deef_alt) ->
108 if value? and value != ''
109 while value[0] is '0' and value.length > 1
110 value = value.substr 1
115 # set cursor position (one based)
116 csi_H: (row, column) ->
117 # handle blank/missing args and convert to 0 base
118 row = @fix_esc_arg(row, 1) - 1
119 column = @fix_esc_arg(column, 1) - 1
124 else if column >= @width
135 # clear to screen edge(es)
136 csi_J: (direction) ->
137 switch @fix_esc_arg direction, '0'
138 when '0' # erase down
139 # rest of current line
142 for row in [@y...@height]
143 for i in [0...@width]
145 @attributes[row][i] = @a
147 # beginning of current line
151 for i in [0...@width]
153 @attributes[row][i] = @a
154 when '2' # erase everything
155 for row in [0...@height]
156 for i in [0...@width]
158 @attributes[row][i] = @a
160 console.log "confusing arg for csi_J: #{direction}"
163 # clear (some or all of) current line
164 csi_K: (direction) ->
165 switch @fix_esc_arg direction, '0'
166 when '0' # erase to right
167 for i in [@x...@width]
169 @attributes[@y][i] = @a
170 when '1' # erase to left
171 # @x can equal @width (after printing to right-most column)
178 @attributes[@y][i] = @a
179 when '2' # erase whole line
180 for i in [0...@width]
182 @attributes[@y][i] = @a
184 console.log "confusing arg for csi_K: #{direction}"
191 switch @fix_esc_arg i, ''
193 if @saved_normal_screen?
194 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
196 @saved_normal_screen = [@x, @y, @text, @attributes]
199 for y in [0...@height]
202 for x in [0...@width]
204 @attributes[y].push 0
209 switch @fix_esc_arg i, ''
211 if not @saved_normal_screen?
212 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
214 @x = @saved_normal_screen[0]
215 @y = @saved_normal_screen[1]
216 @text = @saved_normal_screen[2]
217 @attributes = @saved_normal_screen[3]
218 @saved_normal_screen = null
220 # set color, bold, underline, etc
224 args.push @fix_esc_arg i, '0'
226 while args.length > 0
229 # remove all style/color
235 @set_attribute_bits 0x010000, 0x010000
236 # FIXME add '3' for italic ('23' to turn it off)
238 @set_attribute_bits 0x020000, 0x020000
240 @set_attribute_bits 0x040000, 0x040000
242 @set_attribute_bits 0x080000, 0x080000
244 @set_attribute_bits 0x100000, 0x100000
246 # disable style attributes
247 when '22' # not bold... according to a page
248 @set_attribute_bits 0x010000, 0
249 when '21' # ... though this would make more sense for "not bold"
250 @set_attribute_bits 0x010000, 0
251 when '24' # not underline
252 @set_attribute_bits 0x020000, 0
253 when '25' # not blink
254 @set_attribute_bits 0x040000, 0
255 when '27' # not inverse
256 @set_attribute_bits 0x080000, 0
257 when '28' # not invisible
258 @set_attribute_bits 0x100000, 0
260 when '100' # reset colors but not other attributes
261 @set_attribute_bits 0xffff, 0x0007
265 @set_attribute_bits 0xff, 0x00
267 @set_attribute_bits 0xff, 0x01
269 @set_attribute_bits 0xff, 0x02
270 when '33' # fg yellow
271 @set_attribute_bits 0xff, 0x03
273 @set_attribute_bits 0xff, 0x04
274 when '35' # fg magenta
275 @set_attribute_bits 0xff, 0x05
277 @set_attribute_bits 0xff, 0x06
278 when '37', '39' # fg white (39 is default)
279 @set_attribute_bits 0xff, 0x07
282 if args.length >= 2 and args[0] is '5'
284 @set_attribute_bits 0xff, (0xff & args.shift())
286 @set_attribute_bits 0x20000, 0x20000
290 @set_attribute_bits 0xff00, 0x0000
292 @set_attribute_bits 0xff00, 0x0100
294 @set_attribute_bits 0xff00, 0x0200
295 when '43' # bg yellow
296 @set_attribute_bits 0xff00, 0x0300
298 @set_attribute_bits 0xff00, 0x0400
299 when '45' # bg magenta
300 @set_attribute_bits 0xff00, 0x0500
302 @set_attribute_bits 0xff00, 0x0600
304 @set_attribute_bits 0xff00, 0x0700
305 when '49' # bg default
306 @set_attribute_bits 0xff00, 0x0000
309 if args.length >= 2 and args[0] is '5'
311 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
313 @set_attribute_bits 0x20000, 0x20000
316 when '90' # fg bright black
317 @set_attribute_bits 0xff, 0x08
318 when '91' # fg bright red
319 @set_attribute_bits 0xff, 0x09
320 when '92' # fg bright green
321 @set_attribute_bits 0xff, 0x0a
322 when '93' # fg bright yellow
323 @set_attribute_bits 0xff, 0x0b
324 when '94' # fg bright blue
325 @set_attribute_bits 0xff, 0x0c
326 when '95' # fg bright magenta
327 @set_attribute_bits 0xff, 0x0d
328 when '96' # fg bright cyan
329 @set_attribute_bits 0xff, 0x0e
330 when '97' # fg bright white
331 @set_attribute_bits 0xff, 0x0f
334 when '100' # bg bright black
335 @set_attribute_bits 0xff, 0x08
336 when '101' # bg bright red
337 @set_attribute_bits 0xff, 0x09
338 when '102' # bg bright green
339 @set_attribute_bits 0xff, 0x0a
340 when '103' # bg bright yellow
341 @set_attribute_bits 0xff, 0x0b
342 when '104' # bg bright blue
343 @set_attribute_bits 0xff, 0x0c
344 when '105' # bg bright magenta
345 @set_attribute_bits 0xff, 0x0d
346 when '106' # bg bright cyan
347 @set_attribute_bits 0xff, 0x0e
348 when '107' # bg bright white
349 @set_attribute_bits 0xff, 0x0f
352 # if we don't recognize the style, go back to default
353 console.log "unrecognized csi_m arg: \"#{arg}\""
357 # str is the whole escape sequence (minus the esc[ prefix)
358 update_sequence: (str) ->
363 command = @[prefix + str.substr(str.length - 1)]
365 console.log "Unrecognized sequence: ESC[#{str}"
367 args = str.substr(0, str.length - 1).split ';'
368 command.call this, args...
370 update_sequence_then_text: (str) ->
371 len = @escape_sequence_length str
373 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
374 @update_text "ESC[" + str
376 @update_sequence str.substr 0, len
377 @update_text str.substr len
379 escape_sequence_length: (str) ->
380 parts = str.match(/^[0-9;?]{0,25}./)
381 return -1 unless parts?
382 return parts[0].length
384 my_exports.new = (width, height) ->
385 return new Terminal width, height