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
11 log = -> console.log arguments...
15 alternate_charset_table = {
59 constructor: (width, height) ->
66 @a = 0x000007 # cursor attributes
68 @saved_normal_screen = null
69 @cursor_visible = true
71 @scroll_bottom = height - 1
74 resize: (width, height) ->
75 # FIXME: write a version that retains some of the data
76 # FIXME: clamp variables (eg x, y, saved.*, scrolling region) if getting smaller
86 @attributes[y].push 0x07
88 # pass data from stdout
90 return unless data?.length > 0
91 if @partial.length > 0
92 data = @partial + data
94 parts = data.split(/\x1b/)
96 if -1 is @escape_sequence_length parts[parts.length - 1]
97 @partial = parts.pop()
98 for i in [0...parts.length]
100 @update_text parts[i]
102 @update_sequence_then_text parts[i]
106 # clear the line at the top of the scrolling region
107 for i in [0...@width]
108 @text[@scroll_top][i] = ' '
109 @attributes[@scroll_top][i] = 0x07
113 a[0...@scroll_top]..., # up to but not including scroll top
114 a[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
115 a[@scroll_top], # top line of scroll region (already cleared)
116 a[@scroll_bottom + 1 ... @height]... # rest of screen
118 @text = rearrange @text
119 @attributes = rearrange @attributes
121 # slide cursor up with rest of text
124 wrap_to_next_line: ->
125 if @y is @scroll_bottom
130 # str has no escape sequences
131 update_text: (str) ->
132 return unless str.length > 0
136 @update_text " ".substr(@x % 8)
141 when '\x08' # backspace
148 when '\x0a', '\x0b' # lf, vertical tab (same thing)
151 @alternate_charset = true
153 @alternate_charset = false
157 if @alternate_charset
158 if alternate_charset_table[c]?
159 c = alternate_charset_table[c]
161 @attributes[@y][@x] = @a
165 set_attribute_bits: (mask, value) ->
166 @a = ((@a & ~mask) | value)
168 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
169 # in the default for blank or missing values
170 fix_esc_arg: (value, deef_alt) ->
171 if value? and value != ''
172 while value[0] is '0' and value.length > 1
173 value = value.substr 1
178 # csi_@: rxvt does nothing I can detect
180 # "The auxiliary keypad keys will send ASCII codes corresponding to the
181 # characters engraved on their keys." (numlock?)
186 lines = parseInt @fix_esc_arg lines, '1'
194 lines = parseInt @fix_esc_arg lines, '1'
202 cols = parseInt @fix_esc_arg cols, '1'
210 cols = parseInt @fix_esc_arg cols, '1'
218 row = -1 + parseInt @fix_esc_arg row, '1'
224 col = -1 + parseInt @fix_esc_arg col, '1'
225 @set_column_clamped col
228 # set cursor row (zero based)
229 set_row_clamped: (row) ->
236 # set cursor column (zero based)
237 set_column_clamped: (column) ->
244 # set cursor position (one based)
245 csi_H: (row, column) ->
246 # handle blank/missing args and convert to 0 base
247 row = -1 + parseInt @fix_esc_arg row, '1'
248 column = -1 + parseInt @fix_esc_arg column, '1'
250 @set_column_clamped column
253 # clear to screen edge(es)
254 csi_J: (direction) ->
255 switch @fix_esc_arg direction, '0'
256 when '0' # erase down
257 # rest of current line
260 for row in [@y...@height]
261 for i in [0...@width]
263 @attributes[row][i] = @a
265 # beginning of current line
269 for i in [0...@width]
271 @attributes[row][i] = @a
272 when '2' # erase everything
273 for row in [0...@height]
274 for i in [0...@width]
276 @attributes[row][i] = @a
278 log "confusing arg for csi_J: #{direction}"
281 # clear (some or all of) current line
282 csi_K: (direction) ->
283 switch @fix_esc_arg direction, '0'
284 when '0' # erase to right
285 for i in [@x...@width]
287 @attributes[@y][i] = @a
288 when '1' # erase to left
289 # @x can equal @width (after printing to right-most column)
296 @attributes[@y][i] = @a
297 when '2' # erase whole line
298 for i in [0...@width]
300 @attributes[@y][i] = @a
302 log "confusing arg for csi_K: #{direction}"
305 # move lines downwards (arg is how far)
307 lines = parseInt @fix_esc_arg lines, '1'
311 a[0...@y]..., # keep everything above cursor
312 a[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
313 a[@y..@scroll_bottom - lines]..., # lines that are moving down
314 a[@scroll_bottom + 1 ... @height]... # rest of screen
316 @text = rearrange @text
317 @attributes = rearrange @attributes
319 # clear the lines we scrolled off (and put back in as "new")
320 for y in [@y...@y+lines]
321 for x in [0...@width]
323 @attributes[y][x] = 0x07
325 # move lines upwards (arg is how far)
326 # this obliterates the line under the cursor and arg-1 following it
328 lines = parseInt @fix_esc_arg lines, '1'
332 a[0 ... @y]..., # keep everything above cursor
333 a[@y + lines .. @scroll_bottom]..., # lines we're moving up
334 a[@y ... @y + lines]..., # recycle these
335 a[@scroll_bottom + 1 ... @height]... # keep the rest
337 @text = rearrange @text
338 @attributes = rearrange @attributes
340 # clear the lines we're recycling
341 for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
342 for x in [0...@width]
344 @attributes[y][x] = 0x07
346 # delete chars (I think this shouldn't move the cursor)
348 chars = parseInt @fix_esc_arg chars, '1'
354 @attributes[@y][x] = @a
358 # set modes starting with "?"
362 arg = @fix_esc_arg i, ''
368 @cursor_visible = true
370 # x11 normal mouse tracking
373 if @saved_normal_screen?
374 log "ignoring request to switch to the alt screen because we're already on the alt screen"
376 @saved_normal_screen = x: @x, y: @y, text: @text, attributes: @attributes
379 for y in [0...@height]
382 for x in [0...@width]
384 @attributes[y].push 0x07
386 log "Unimplemented arg for csiq_h: #{arg}"
389 # reset (turn off) modes
390 # note that modes starting with ? call csiq_l, not this
393 arg = @fix_esc_arg i, ''
399 # KAM -- keyboard action
402 # put cursor in "REPLACE" mode
405 # turn local echo on (or off?)
407 # TODO when '20' LNM linefeed/newline
409 # reset (turn off) modes starting with ?
410 # in st source, these are priv=true
414 arg = @fix_esc_arg i, ''
416 when '1' # mode ?1 reset
420 # stop blinking the cursor (ignored)
422 when '25' # mode ?25 reset
423 @cursor_visible = false
424 when '0', '2', '3', '4', '8', '18', '19', '42', '12'
425 # st ignores all of these
427 when '9', '1000', '1002', '1003', '1004', '1006'
428 # mouse reporting and such (ignored)
431 if not @saved_normal_screen?
432 log "ignoring request to switch to the normal screen because we're already on the normal screen"
434 @x = @saved_normal_screen.x
435 @y = @saved_normal_screen.y
436 @text = @saved_normal_screen.text
437 @attributes = @saved_normal_screen.attributes
438 @saved_normal_screen = null
440 log "Unimplemented arg for csiq_l: #{arg}"
443 # set color, bold, underline, etc
447 args.push @fix_esc_arg i, '0'
449 while args.length > 0
452 # remove all style/color
458 @set_attribute_bits 0x010000, 0x010000
459 when '3' # italic (rare)
460 @set_attribute_bits 0x200000, 0x200000
462 @set_attribute_bits 0x020000, 0x020000
464 @set_attribute_bits 0x040000, 0x040000
466 @set_attribute_bits 0x080000, 0x080000
467 when '8' # invisible. urivt ignores this
468 @set_attribute_bits 0x100000, 0x100000
470 # disable style attributes
471 when '21' # not bold (rare)
472 @set_attribute_bits 0x010000, 0
474 @set_attribute_bits 0x010000, 0
475 when '23' # not italic (rare)
476 @set_attribute_bits 0x200000, 0
477 when '24' # not underline
478 @set_attribute_bits 0x020000, 0
479 when '25' # not blink
480 @set_attribute_bits 0x040000, 0
481 when '27' # not inverse
482 @set_attribute_bits 0x080000, 0
483 when '28' # not invisible
484 @set_attribute_bits 0x100000, 0
486 when '100' # reset colors but not other attributes
487 @set_attribute_bits 0xffff, 0x0007
491 @set_attribute_bits 0xff, 0x00
493 @set_attribute_bits 0xff, 0x01
495 @set_attribute_bits 0xff, 0x02
496 when '33' # fg yellow
497 @set_attribute_bits 0xff, 0x03
499 @set_attribute_bits 0xff, 0x04
500 when '35' # fg magenta
501 @set_attribute_bits 0xff, 0x05
503 @set_attribute_bits 0xff, 0x06
504 when '37', '39' # fg white (39 is default)
505 @set_attribute_bits 0xff, 0x07
508 if args.length >= 2 and args[0] is '5'
510 @set_attribute_bits 0xff, (0xff & args.shift())
512 @set_attribute_bits 0x20000, 0x20000
516 @set_attribute_bits 0xff00, 0x0000
518 @set_attribute_bits 0xff00, 0x0100
520 @set_attribute_bits 0xff00, 0x0200
521 when '43' # bg yellow
522 @set_attribute_bits 0xff00, 0x0300
524 @set_attribute_bits 0xff00, 0x0400
525 when '45' # bg magenta
526 @set_attribute_bits 0xff00, 0x0500
528 @set_attribute_bits 0xff00, 0x0600
530 @set_attribute_bits 0xff00, 0x0700
531 when '49' # bg default
532 @set_attribute_bits 0xff00, 0x0000
535 if args.length >= 2 and args[0] is '5'
537 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
539 @set_attribute_bits 0x20000, 0x20000
542 when '90' # fg bright black
543 @set_attribute_bits 0xff, 0x08
544 when '91' # fg bright red
545 @set_attribute_bits 0xff, 0x09
546 when '92' # fg bright green
547 @set_attribute_bits 0xff, 0x0a
548 when '93' # fg bright yellow
549 @set_attribute_bits 0xff, 0x0b
550 when '94' # fg bright blue
551 @set_attribute_bits 0xff, 0x0c
552 when '95' # fg bright magenta
553 @set_attribute_bits 0xff, 0x0d
554 when '96' # fg bright cyan
555 @set_attribute_bits 0xff, 0x0e
556 when '97' # fg bright white
557 @set_attribute_bits 0xff, 0x0f
560 when '100' # bg bright black
561 @set_attribute_bits 0xff00, 0x0800
562 when '101' # bg bright red
563 @set_attribute_bits 0xff00, 0x0900
564 when '102' # bg bright green
565 @set_attribute_bits 0xff00, 0x0a00
566 when '103' # bg bright yellow
567 @set_attribute_bits 0xff00, 0x0b00
568 when '104' # bg bright blue
569 @set_attribute_bits 0xff00, 0x0c00
570 when '105' # bg bright magenta
571 @set_attribute_bits 0xff00, 0x0d00
572 when '106' # bg bright cyan
573 @set_attribute_bits 0xff00, 0x0e00
574 when '107' # bg bright white
575 @set_attribute_bits 0xff00, 0x0f00
578 # if we don't recognize the style, go back to default
579 log "unrecognized csi_m arg: \"#{arg}\""
584 csi_r: (top, bottom) ->
585 top = -1 + parseInt @fix_esc_arg top, '1'
586 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
592 @scroll_bottom = bottom
595 # move cursor up one line, if it's at the top, scroll everything down
604 sem = seq.indexOf ';'
605 if sem < 1 || seq[0] isnt '0'
606 log "Unimplemented esc_] (OS command) sequence: #{seq}"
609 title = seq.substr sem + 1
610 title = title.substr 0, title.length - 1
611 window.document.title = title
613 # "The auxiliary keypad keys will transmit control sequences."
618 @alternate_charset = true
621 @alternate_charset = true
624 @alternate_charset = false
627 @alternate_charset = false
630 @alternate_charset = true
632 # str is the whole escape sequence (minus the esc character)
633 update_sequence: (str) ->
636 method_prefix = 'csiq_'
637 args = str.substr(2, str.length - 3).split ';'
639 method_prefix = 'csi_'
640 args = str.substr(1, str.length - 2).split ';'
641 method_name = method_prefix + str.substr str.length - 1
642 else if str[0] is '(' # character set
643 method_name = 'gzd4_' + str.substr 1, 1
644 args = [str.substr 1]
646 method_name = 'esc_' + str.substr 0, 1
647 args = [str.substr 1]
648 method = @[method_name]
650 log "Unimplemented #{method_name}, code: ESC#{str}"
652 method.apply this, args
654 update_sequence_then_text: (str) ->
655 len = @escape_sequence_length str
657 log "couldn't find escape sequence here: #{str.substr 0, 25}"
658 @update_text "ESC" + str
660 @update_sequence str.substr 0, len
661 @update_text str.substr len
663 escape_sequence_length: (str) ->
665 parts = str.match(/^\[[0-9;?]{0,25}./)
666 return -1 unless parts?
667 return parts[0].length
668 else if str[0] is '('
671 if str[1] is '!' or str[1] is '%'
675 else if str[0] is ']'
676 st = str.indexOf "\u0007"
681 #log "non[: ESC#{str.substr 0, 10}"
687 my_exports.new = (width, height) ->
688 return new Terminal width, height