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
22 @cursor_visible = true
24 @scroll_bottom = height - 1
27 resize: (width, height) ->
28 # FIXME: write a version that retains some of the data
29 # FIXME: clamp variables (eg x, y, saved.*, scrolling region) if getting smaller
39 @attributes[y].push 0x07
41 # pass data from stdout
43 return unless data?.length > 0
44 if @partial.length > 0
45 data = @partial + data
47 parts = data.split(/\x1b\[/)
49 if -1 is @escape_sequence_length parts[parts.length - 1]
50 @partial = parts.pop()
52 for i in [0...parts.length]
56 @update_sequence_then_text parts[i]
60 # clear the line at the top of the scrolling region
62 @text[@scroll_top][i] = ' '
63 @attributes[@scroll_top][i] = 0x07
65 # move (newly cleared) top line to the bottom of the scrolling region
67 @text[0...@scroll_top]..., # up to but not including scroll top
68 @text[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
69 @text[@scroll_top], # top line of scroll region (already cleared)
70 @text[@scroll_bottom + 1 ... @height]... # rest of screen
73 @attributes[0...@scroll_top]..., # up to but not including scroll top
74 @attributes[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
75 @attributes[@scroll_top], # top line of scroll region (already cleared)
76 @attributes[@scroll_bottom + 1 ... @height]... # rest of screen
79 # slide cursor up with rest of text
83 if @y is @scroll_bottom
88 # str has no escape sequences
90 return unless str.length > 0
94 @update_text " ".substr(@x % 8)
99 when '\x08' # backspace
106 when '\x0a', '\x0b' # lf, vertical tab (same thing)
112 @attributes[@y][@x] = @a
116 set_attribute_bits: (mask, value) ->
117 @a = ((@a & ~mask) | value)
119 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
120 # in the default for blank or missing values
121 fix_esc_arg: (value, deef_alt) ->
122 if value? and value != ''
123 while value[0] is '0' and value.length > 1
124 value = value.substr 1
129 # csi_@: rxvt does nothing I can detect
133 lines = parseInt @fix_esc_arg lines, '1'
141 lines = parseInt @fix_esc_arg lines, '1'
149 cols = parseInt @fix_esc_arg cols, '1'
157 cols = parseInt @fix_esc_arg cols, '1'
163 # set cursor position (one based)
164 csi_H: (row, column) ->
165 # handle blank/missing args and convert to 0 base
166 row = -1 + parseInt @fix_esc_arg row, '1'
167 column = -1 + parseInt @fix_esc_arg column, '1'
172 else if column >= @width
184 # clear to screen edge(es)
185 csi_J: (direction) ->
186 switch @fix_esc_arg direction, '0'
187 when '0' # erase down
188 # rest of current line
191 for row in [@y...@height]
192 for i in [0...@width]
194 @attributes[row][i] = @a
196 # beginning of current line
200 for i in [0...@width]
202 @attributes[row][i] = @a
203 when '2' # erase everything
204 for row in [0...@height]
205 for i in [0...@width]
207 @attributes[row][i] = @a
209 console.log "confusing arg for csi_J: #{direction}"
212 # clear (some or all of) current line
213 csi_K: (direction) ->
214 switch @fix_esc_arg direction, '0'
215 when '0' # erase to right
216 for i in [@x...@width]
218 @attributes[@y][i] = @a
219 when '1' # erase to left
220 # @x can equal @width (after printing to right-most column)
227 @attributes[@y][i] = @a
228 when '2' # erase whole line
229 for i in [0...@width]
231 @attributes[@y][i] = @a
233 console.log "confusing arg for csi_K: #{direction}"
240 arg = @fix_esc_arg i, ''
243 @cursor_visible = true
245 if @saved_normal_screen?
246 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
248 @saved_normal_screen = [@x, @y, @text, @attributes]
251 for y in [0...@height]
254 for x in [0...@width]
256 @attributes[y].push 0x07
258 console.log "confusing arg for csiq_h: #{arg}"
265 arg = @fix_esc_arg i, ''
268 @cursor_visible = false
270 if not @saved_normal_screen?
271 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
273 @x = @saved_normal_screen[0]
274 @y = @saved_normal_screen[1]
275 @text = @saved_normal_screen[2]
276 @attributes = @saved_normal_screen[3]
277 @saved_normal_screen = null
279 console.log "confusing arg for csiq_l: #{arg}"
282 # set color, bold, underline, etc
286 args.push @fix_esc_arg i, '0'
288 while args.length > 0
291 # remove all style/color
297 @set_attribute_bits 0x010000, 0x010000
298 when '3' # italic (rare)
299 @set_attribute_bits 0x200000, 0x200000
301 @set_attribute_bits 0x020000, 0x020000
303 @set_attribute_bits 0x040000, 0x040000
305 @set_attribute_bits 0x080000, 0x080000
306 when '8' # invisible. urivt ignores this
307 @set_attribute_bits 0x100000, 0x100000
309 # disable style attributes
310 when '21' # not bold (rare)
311 @set_attribute_bits 0x010000, 0
313 @set_attribute_bits 0x010000, 0
314 when '23' # not italic (rare)
315 @set_attribute_bits 0x200000, 0
316 when '24' # not underline
317 @set_attribute_bits 0x020000, 0
318 when '25' # not blink
319 @set_attribute_bits 0x040000, 0
320 when '27' # not inverse
321 @set_attribute_bits 0x080000, 0
322 when '28' # not invisible
323 @set_attribute_bits 0x100000, 0
325 when '100' # reset colors but not other attributes
326 @set_attribute_bits 0xffff, 0x0007
330 @set_attribute_bits 0xff, 0x00
332 @set_attribute_bits 0xff, 0x01
334 @set_attribute_bits 0xff, 0x02
335 when '33' # fg yellow
336 @set_attribute_bits 0xff, 0x03
338 @set_attribute_bits 0xff, 0x04
339 when '35' # fg magenta
340 @set_attribute_bits 0xff, 0x05
342 @set_attribute_bits 0xff, 0x06
343 when '37', '39' # fg white (39 is default)
344 @set_attribute_bits 0xff, 0x07
347 if args.length >= 2 and args[0] is '5'
349 @set_attribute_bits 0xff, (0xff & args.shift())
351 @set_attribute_bits 0x20000, 0x20000
355 @set_attribute_bits 0xff00, 0x0000
357 @set_attribute_bits 0xff00, 0x0100
359 @set_attribute_bits 0xff00, 0x0200
360 when '43' # bg yellow
361 @set_attribute_bits 0xff00, 0x0300
363 @set_attribute_bits 0xff00, 0x0400
364 when '45' # bg magenta
365 @set_attribute_bits 0xff00, 0x0500
367 @set_attribute_bits 0xff00, 0x0600
369 @set_attribute_bits 0xff00, 0x0700
370 when '49' # bg default
371 @set_attribute_bits 0xff00, 0x0000
374 if args.length >= 2 and args[0] is '5'
376 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
378 @set_attribute_bits 0x20000, 0x20000
381 when '90' # fg bright black
382 @set_attribute_bits 0xff, 0x08
383 when '91' # fg bright red
384 @set_attribute_bits 0xff, 0x09
385 when '92' # fg bright green
386 @set_attribute_bits 0xff, 0x0a
387 when '93' # fg bright yellow
388 @set_attribute_bits 0xff, 0x0b
389 when '94' # fg bright blue
390 @set_attribute_bits 0xff, 0x0c
391 when '95' # fg bright magenta
392 @set_attribute_bits 0xff, 0x0d
393 when '96' # fg bright cyan
394 @set_attribute_bits 0xff, 0x0e
395 when '97' # fg bright white
396 @set_attribute_bits 0xff, 0x0f
399 when '100' # bg bright black
400 @set_attribute_bits 0xff, 0x08
401 when '101' # bg bright red
402 @set_attribute_bits 0xff, 0x09
403 when '102' # bg bright green
404 @set_attribute_bits 0xff, 0x0a
405 when '103' # bg bright yellow
406 @set_attribute_bits 0xff, 0x0b
407 when '104' # bg bright blue
408 @set_attribute_bits 0xff, 0x0c
409 when '105' # bg bright magenta
410 @set_attribute_bits 0xff, 0x0d
411 when '106' # bg bright cyan
412 @set_attribute_bits 0xff, 0x0e
413 when '107' # bg bright white
414 @set_attribute_bits 0xff, 0x0f
417 # if we don't recognize the style, go back to default
418 console.log "unrecognized csi_m arg: \"#{arg}\""
422 # set scrolling region
423 csi_r: (top, bottom) ->
424 top = -1 + parseInt @fix_esc_arg top, '1'
425 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
431 @scroll_bottom = bottom
434 # str is the whole escape sequence (minus the esc[ prefix)
435 update_sequence: (str) ->
440 command = @[prefix + str.substr(str.length - 1)]
442 console.log "Unrecognized sequence: ESC[#{str}"
444 args = str.substr(0, str.length - 1).split ';'
445 command.call this, args...
447 update_sequence_then_text: (str) ->
448 len = @escape_sequence_length str
450 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
451 @update_text "ESC[" + str
453 @update_sequence str.substr 0, len
454 @update_text str.substr len
456 escape_sequence_length: (str) ->
457 parts = str.match(/^[0-9;?]{0,25}./)
458 return -1 unless parts?
459 return parts[0].length
461 my_exports.new = (width, height) ->
462 return new Terminal width, height