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 (without moving the cursor)
348 chars = parseInt @fix_esc_arg chars, '1'
354 @attributes[@y][x] = @a
358 chars = parseInt @fix_esc_arg chars, '1'
361 if chars > @width - @x
367 while dest - chars >= @x
368 @text[@y][dest] = @text[@y][dest - chars]
369 @attributes[@y][dest] = @attributes[@y][dest - chars]
373 chars = parseInt @fix_esc_arg chars, '1'
376 if chars > @width - @x
380 while dest < @width - chars
381 @text[@y][dest] = @text[@y][dest + chars]
382 @attributes[@y][dest] = @attributes[@y][dest + chars]
384 # clear the space moved out of
388 @attributes[@y][x] = @a
391 # set modes not starting with "?"
394 arg = @fix_esc_arg i, ''
396 when '0' # error (ignored
398 when '2' # KAM -- keyboard action
400 when '4' # put cursor in "INSERT" mode
402 when '12' # turn local echo off (or on?)
404 # TODO when '20' LNM linefeed/newline
406 log "Unimplemented arg for csi_h: #{arg}"
407 # set modes starting with "?"
410 arg = @fix_esc_arg i, ''
416 @cursor_visible = true
418 # x11 normal mouse tracking
421 if @saved_normal_screen?
422 log "ignoring request to switch to the alt screen because we're already on the alt screen"
424 @saved_normal_screen = x: @x, y: @y, text: @text, attributes: @attributes
427 for y in [0...@height]
430 for x in [0...@width]
432 @attributes[y].push 0x07
434 log "Unimplemented arg for csiq_h: #{arg}"
437 # reset (turn off) modes
438 # note that modes starting with ? call csiq_l, not this
441 arg = @fix_esc_arg i, ''
443 when '0' # error (ignored
445 when '2' # KAM -- keyboard action
447 when '4' # put cursor in "REPLACE" mode
449 when '12' # turn local echo on (or off?)
451 # TODO when '20' LNM linefeed/newline
453 # reset (turn off) modes starting with ?
454 # in st source, these are priv=true
458 arg = @fix_esc_arg i, ''
460 when '1' # mode ?1 reset
464 # stop blinking the cursor (ignored)
466 when '25' # mode ?25 reset
467 @cursor_visible = false
468 when '0', '2', '3', '4', '8', '18', '19', '42', '12'
469 # st ignores all of these
471 when '9', '1000', '1002', '1003', '1004', '1006'
472 # mouse reporting and such (ignored)
475 if not @saved_normal_screen?
476 log "ignoring request to switch to the normal screen because we're already on the normal screen"
478 @x = @saved_normal_screen.x
479 @y = @saved_normal_screen.y
480 @text = @saved_normal_screen.text
481 @attributes = @saved_normal_screen.attributes
482 @saved_normal_screen = null
484 log "Unimplemented arg for csiq_l: #{arg}"
487 # set color, bold, underline, etc
491 args.push @fix_esc_arg i, '0'
493 while args.length > 0
496 # remove all style/color
502 @set_attribute_bits 0x010000, 0x010000
503 when '3' # italic (rare)
504 @set_attribute_bits 0x200000, 0x200000
506 @set_attribute_bits 0x020000, 0x020000
508 @set_attribute_bits 0x040000, 0x040000
510 @set_attribute_bits 0x080000, 0x080000
511 when '8' # invisible. urivt ignores this
512 @set_attribute_bits 0x100000, 0x100000
514 # disable style attributes
515 when '21' # not bold (rare)
516 @set_attribute_bits 0x010000, 0
518 @set_attribute_bits 0x010000, 0
519 when '23' # not italic (rare)
520 @set_attribute_bits 0x200000, 0
521 when '24' # not underline
522 @set_attribute_bits 0x020000, 0
523 when '25' # not blink
524 @set_attribute_bits 0x040000, 0
525 when '27' # not inverse
526 @set_attribute_bits 0x080000, 0
527 when '28' # not invisible
528 @set_attribute_bits 0x100000, 0
530 when '100' # reset colors but not other attributes
531 @set_attribute_bits 0xffff, 0x0007
535 @set_attribute_bits 0xff, 0x00
537 @set_attribute_bits 0xff, 0x01
539 @set_attribute_bits 0xff, 0x02
540 when '33' # fg yellow
541 @set_attribute_bits 0xff, 0x03
543 @set_attribute_bits 0xff, 0x04
544 when '35' # fg magenta
545 @set_attribute_bits 0xff, 0x05
547 @set_attribute_bits 0xff, 0x06
548 when '37', '39' # fg white (39 is default)
549 @set_attribute_bits 0xff, 0x07
552 if args.length >= 2 and args[0] is '5'
554 @set_attribute_bits 0xff, (0xff & args.shift())
556 @set_attribute_bits 0x20000, 0x20000
560 @set_attribute_bits 0xff00, 0x0000
562 @set_attribute_bits 0xff00, 0x0100
564 @set_attribute_bits 0xff00, 0x0200
565 when '43' # bg yellow
566 @set_attribute_bits 0xff00, 0x0300
568 @set_attribute_bits 0xff00, 0x0400
569 when '45' # bg magenta
570 @set_attribute_bits 0xff00, 0x0500
572 @set_attribute_bits 0xff00, 0x0600
574 @set_attribute_bits 0xff00, 0x0700
575 when '49' # bg default
576 @set_attribute_bits 0xff00, 0x0000
579 if args.length >= 2 and args[0] is '5'
581 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
583 @set_attribute_bits 0x20000, 0x20000
586 when '90' # fg bright black
587 @set_attribute_bits 0xff, 0x08
588 when '91' # fg bright red
589 @set_attribute_bits 0xff, 0x09
590 when '92' # fg bright green
591 @set_attribute_bits 0xff, 0x0a
592 when '93' # fg bright yellow
593 @set_attribute_bits 0xff, 0x0b
594 when '94' # fg bright blue
595 @set_attribute_bits 0xff, 0x0c
596 when '95' # fg bright magenta
597 @set_attribute_bits 0xff, 0x0d
598 when '96' # fg bright cyan
599 @set_attribute_bits 0xff, 0x0e
600 when '97' # fg bright white
601 @set_attribute_bits 0xff, 0x0f
604 when '100' # bg bright black
605 @set_attribute_bits 0xff00, 0x0800
606 when '101' # bg bright red
607 @set_attribute_bits 0xff00, 0x0900
608 when '102' # bg bright green
609 @set_attribute_bits 0xff00, 0x0a00
610 when '103' # bg bright yellow
611 @set_attribute_bits 0xff00, 0x0b00
612 when '104' # bg bright blue
613 @set_attribute_bits 0xff00, 0x0c00
614 when '105' # bg bright magenta
615 @set_attribute_bits 0xff00, 0x0d00
616 when '106' # bg bright cyan
617 @set_attribute_bits 0xff00, 0x0e00
618 when '107' # bg bright white
619 @set_attribute_bits 0xff00, 0x0f00
622 # if we don't recognize the style, go back to default
623 log "unrecognized csi_m arg: \"#{arg}\""
628 csi_r: (top, bottom) ->
629 top = -1 + parseInt @fix_esc_arg top, '1'
630 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
636 @scroll_bottom = bottom
639 # move cursor up one line, if it's at the top, scroll everything down
648 sem = seq.indexOf ';'
649 if sem < 1 || seq[0] isnt '0'
650 log "Unimplemented esc_] (OS command) sequence: #{seq}"
653 title = seq.substr sem + 1
654 title = title.substr 0, title.length - 1
655 window.document.title = title
657 # "The auxiliary keypad keys will transmit control sequences."
662 @alternate_charset = true
665 @alternate_charset = true
668 @alternate_charset = false
671 @alternate_charset = false
674 @alternate_charset = true
676 # str is the whole escape sequence (minus the esc character)
677 update_sequence: (str) ->
680 method_prefix = 'csiq_'
681 args = str.substr(2, str.length - 3).split ';'
683 method_prefix = 'csi_'
684 args = str.substr(1, str.length - 2).split ';'
685 method_name = method_prefix + str.substr str.length - 1
686 else if str[0] is '(' # character set
687 method_name = 'gzd4_' + str.substr 1, 1
688 args = [str.substr 1]
690 method_name = 'esc_' + str.substr 0, 1
691 args = [str.substr 1]
692 method = @[method_name]
694 log "Unimplemented #{method_name}, code: ESC#{str}"
696 method.apply this, args
698 update_sequence_then_text: (str) ->
699 len = @escape_sequence_length str
701 log "couldn't find escape sequence here: #{str.substr 0, 25}"
702 @update_text "ESC" + str
704 @update_sequence str.substr 0, len
705 @update_text str.substr len
707 escape_sequence_length: (str) ->
709 parts = str.match(/^\[[0-9;?]{0,25}./)
710 return -1 unless parts?
711 return parts[0].length
712 else if str[0] is '('
715 if str[1] is '!' or str[1] is '%'
719 else if str[0] is ']'
720 st = str.indexOf "\u0007"
725 #log "non[: ESC#{str.substr 0, 10}"
731 my_exports.new = (width, height) ->
732 return new Terminal width, height