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...
17 constructor: (width, height) ->
24 @a = 0x000007 # cursor attributes
26 @saved_normal_screen = null
27 @cursor_visible = true
29 @scroll_bottom = height - 1
32 resize: (width, height) ->
33 # FIXME: write a version that retains some of the data
34 # FIXME: clamp variables (eg x, y, saved.*, scrolling region) if getting smaller
44 @attributes[y].push 0x07
46 # pass data from stdout
48 return unless data?.length > 0
49 if @partial.length > 0
50 data = @partial + data
52 parts = data.split(/\x1b/)
54 if -1 is @escape_sequence_length parts[parts.length - 1]
55 @partial = parts.pop()
56 for i in [0...parts.length]
60 @update_sequence_then_text parts[i]
64 # clear the line at the top of the scrolling region
66 @text[@scroll_top][i] = ' '
67 @attributes[@scroll_top][i] = 0x07
71 a[0...@scroll_top]..., # up to but not including scroll top
72 a[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
73 a[@scroll_top], # top line of scroll region (already cleared)
74 a[@scroll_bottom + 1 ... @height]... # rest of screen
76 @text = rearrange @text
77 @attributes = rearrange @attributes
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 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 log "confusing arg for csi_K: #{direction}"
236 # move lines downwards (arg is how far)
238 lines = parseInt @fix_esc_arg lines, '1'
242 a[0...@y]..., # keep everything above cursor
243 a[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
244 a[@y..@scroll_bottom - lines]..., # lines that are moving down
245 a[@scroll_bottom + 1 ... @height]... # rest of screen
247 @text = rearrange @text
248 @attributes = rearrange @attributes
250 # clear the lines we scrolled off (and put back in as "new")
251 for y in [@y...@y+lines]
252 for x in [0...@width]
254 @attributes[y][x] = 0x07
256 # move lines upwards (arg is how far)
257 # this obliterates the line under the cursor and arg-1 following it
259 lines = parseInt @fix_esc_arg lines, '1'
263 a[0 ... @y]..., # keep everything above cursor
264 a[@y + lines .. @scroll_bottom]..., # lines we're moving up
265 a[@y ... @y + lines]..., # recycle these
266 a[@scroll_bottom + 1 ... @height]... # keep the rest
268 @text = rearrange @text
269 @attributes = rearrange @attributes
271 # clear the lines we're recycling
272 for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
273 for x in [0...@width]
275 @attributes[y][x] = 0x07
281 arg = @fix_esc_arg i, ''
284 @cursor_visible = true
286 if @saved_normal_screen?
287 log "ignoring request to switch to the alt screen because we're already on the alt screen"
289 @saved_normal_screen = x: @x, y: @y, text: @text, attributes: @attributes
292 for y in [0...@height]
295 for x in [0...@width]
297 @attributes[y].push 0x07
299 log "confusing arg for csiq_h: #{arg}"
306 arg = @fix_esc_arg i, ''
309 @cursor_visible = false
311 if not @saved_normal_screen?
312 log "ignoring request to switch to the normal screen because we're already on the normal screen"
314 @x = @saved_normal_screen.x
315 @y = @saved_normal_screen.y
316 @text = @saved_normal_screen.text
317 @attributes = @saved_normal_screen.attributes
318 @saved_normal_screen = null
320 log "confusing arg for csiq_l: #{arg}"
323 # set color, bold, underline, etc
327 args.push @fix_esc_arg i, '0'
329 while args.length > 0
332 # remove all style/color
338 @set_attribute_bits 0x010000, 0x010000
339 when '3' # italic (rare)
340 @set_attribute_bits 0x200000, 0x200000
342 @set_attribute_bits 0x020000, 0x020000
344 @set_attribute_bits 0x040000, 0x040000
346 @set_attribute_bits 0x080000, 0x080000
347 when '8' # invisible. urivt ignores this
348 @set_attribute_bits 0x100000, 0x100000
350 # disable style attributes
351 when '21' # not bold (rare)
352 @set_attribute_bits 0x010000, 0
354 @set_attribute_bits 0x010000, 0
355 when '23' # not italic (rare)
356 @set_attribute_bits 0x200000, 0
357 when '24' # not underline
358 @set_attribute_bits 0x020000, 0
359 when '25' # not blink
360 @set_attribute_bits 0x040000, 0
361 when '27' # not inverse
362 @set_attribute_bits 0x080000, 0
363 when '28' # not invisible
364 @set_attribute_bits 0x100000, 0
366 when '100' # reset colors but not other attributes
367 @set_attribute_bits 0xffff, 0x0007
371 @set_attribute_bits 0xff, 0x00
373 @set_attribute_bits 0xff, 0x01
375 @set_attribute_bits 0xff, 0x02
376 when '33' # fg yellow
377 @set_attribute_bits 0xff, 0x03
379 @set_attribute_bits 0xff, 0x04
380 when '35' # fg magenta
381 @set_attribute_bits 0xff, 0x05
383 @set_attribute_bits 0xff, 0x06
384 when '37', '39' # fg white (39 is default)
385 @set_attribute_bits 0xff, 0x07
388 if args.length >= 2 and args[0] is '5'
390 @set_attribute_bits 0xff, (0xff & args.shift())
392 @set_attribute_bits 0x20000, 0x20000
396 @set_attribute_bits 0xff00, 0x0000
398 @set_attribute_bits 0xff00, 0x0100
400 @set_attribute_bits 0xff00, 0x0200
401 when '43' # bg yellow
402 @set_attribute_bits 0xff00, 0x0300
404 @set_attribute_bits 0xff00, 0x0400
405 when '45' # bg magenta
406 @set_attribute_bits 0xff00, 0x0500
408 @set_attribute_bits 0xff00, 0x0600
410 @set_attribute_bits 0xff00, 0x0700
411 when '49' # bg default
412 @set_attribute_bits 0xff00, 0x0000
415 if args.length >= 2 and args[0] is '5'
417 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
419 @set_attribute_bits 0x20000, 0x20000
422 when '90' # fg bright black
423 @set_attribute_bits 0xff, 0x08
424 when '91' # fg bright red
425 @set_attribute_bits 0xff, 0x09
426 when '92' # fg bright green
427 @set_attribute_bits 0xff, 0x0a
428 when '93' # fg bright yellow
429 @set_attribute_bits 0xff, 0x0b
430 when '94' # fg bright blue
431 @set_attribute_bits 0xff, 0x0c
432 when '95' # fg bright magenta
433 @set_attribute_bits 0xff, 0x0d
434 when '96' # fg bright cyan
435 @set_attribute_bits 0xff, 0x0e
436 when '97' # fg bright white
437 @set_attribute_bits 0xff, 0x0f
440 when '100' # bg bright black
441 @set_attribute_bits 0xff, 0x08
442 when '101' # bg bright red
443 @set_attribute_bits 0xff, 0x09
444 when '102' # bg bright green
445 @set_attribute_bits 0xff, 0x0a
446 when '103' # bg bright yellow
447 @set_attribute_bits 0xff, 0x0b
448 when '104' # bg bright blue
449 @set_attribute_bits 0xff, 0x0c
450 when '105' # bg bright magenta
451 @set_attribute_bits 0xff, 0x0d
452 when '106' # bg bright cyan
453 @set_attribute_bits 0xff, 0x0e
454 when '107' # bg bright white
455 @set_attribute_bits 0xff, 0x0f
458 # if we don't recognize the style, go back to default
459 log "unrecognized csi_m arg: \"#{arg}\""
464 csi_r: (top, bottom) ->
465 top = -1 + parseInt @fix_esc_arg top, '1'
466 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
472 @scroll_bottom = bottom
475 # move cursor up one line, if it's at the top, scroll everything down
482 # str is the whole escape sequence (minus the esc[ prefix)
483 update_sequence: (str) ->
489 args = str.substr(1, str.length - 2).split ';'
492 args = [str.substr(0, str.length - 1)]
493 command = @[prefix + str.substr(str.length - 1)]
495 log "Unrecognized sequence: ESC[#{str}"
497 command.call this, args...
499 update_sequence_then_text: (str) ->
500 len = @escape_sequence_length str
502 log "couldn't find escape sequence here: #{str.substr 0, 25}"
503 @update_text "ESC" + str
505 @update_sequence str.substr 0, len
506 @update_text str.substr len
508 escape_sequence_length: (str) ->
510 parts = str.match(/^\[[0-9;?]{0,25}./)
511 return -1 unless parts?
512 return parts[0].length
514 log "non[: ESC#{str.substr 0, 10}"
520 my_exports.new = (width, height) ->
521 return new Terminal width, height