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()
57 for i in [0...parts.length]
61 @update_sequence_then_text parts[i]
65 # clear the line at the top of the scrolling region
67 @text[@scroll_top][i] = ' '
68 @attributes[@scroll_top][i] = 0x07
72 a[0...@scroll_top]..., # up to but not including scroll top
73 a[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
74 a[@scroll_top], # top line of scroll region (already cleared)
75 a[@scroll_bottom + 1 ... @height]... # rest of screen
77 @text = rearrange @text
78 @attributes = rearrange @attributes
80 # slide cursor up with rest of text
84 if @y is @scroll_bottom
89 # str has no escape sequences
91 return unless str.length > 0
95 @update_text " ".substr(@x % 8)
100 when '\x08' # backspace
107 when '\x0a', '\x0b' # lf, vertical tab (same thing)
113 @attributes[@y][@x] = @a
117 set_attribute_bits: (mask, value) ->
118 @a = ((@a & ~mask) | value)
120 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
121 # in the default for blank or missing values
122 fix_esc_arg: (value, deef_alt) ->
123 if value? and value != ''
124 while value[0] is '0' and value.length > 1
125 value = value.substr 1
130 # csi_@: rxvt does nothing I can detect
134 lines = parseInt @fix_esc_arg lines, '1'
142 lines = parseInt @fix_esc_arg lines, '1'
150 cols = parseInt @fix_esc_arg cols, '1'
158 cols = parseInt @fix_esc_arg cols, '1'
164 # set cursor position (one based)
165 csi_H: (row, column) ->
166 # handle blank/missing args and convert to 0 base
167 row = -1 + parseInt @fix_esc_arg row, '1'
168 column = -1 + parseInt @fix_esc_arg column, '1'
173 else if column >= @width
185 # clear to screen edge(es)
186 csi_J: (direction) ->
187 switch @fix_esc_arg direction, '0'
188 when '0' # erase down
189 # rest of current line
192 for row in [@y...@height]
193 for i in [0...@width]
195 @attributes[row][i] = @a
197 # beginning of current line
201 for i in [0...@width]
203 @attributes[row][i] = @a
204 when '2' # erase everything
205 for row in [0...@height]
206 for i in [0...@width]
208 @attributes[row][i] = @a
210 log "confusing arg for csi_J: #{direction}"
213 # clear (some or all of) current line
214 csi_K: (direction) ->
215 switch @fix_esc_arg direction, '0'
216 when '0' # erase to right
217 for i in [@x...@width]
219 @attributes[@y][i] = @a
220 when '1' # erase to left
221 # @x can equal @width (after printing to right-most column)
228 @attributes[@y][i] = @a
229 when '2' # erase whole line
230 for i in [0...@width]
232 @attributes[@y][i] = @a
234 log "confusing arg for csi_K: #{direction}"
237 # move lines downwards (arg is how far)
239 lines = parseInt @fix_esc_arg lines, '1'
243 a[0...@y]..., # keep everything above cursor
244 a[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
245 a[@y..@scroll_bottom - lines]..., # lines that are moving down
246 a[@scroll_bottom + 1 ... @height]... # rest of screen
248 @text = rearrange @text
249 @attributes = rearrange @attributes
251 # clear the lines we scrolled off (and put back in as "new")
252 for y in [@y...@y+lines]
253 for x in [0...@width]
255 @attributes[y][x] = 0x07
257 # move lines upwards (arg is how far)
258 # this obliterates the line under the cursor and arg-1 following it
260 lines = parseInt @fix_esc_arg lines, '1'
264 a[0 ... @y]..., # keep everything above cursor
265 a[@y + lines .. @scroll_bottom]..., # lines we're moving up
266 a[@y ... @y + lines]..., # recycle these
267 a[@scroll_bottom + 1 ... @height]... # keep the rest
269 @text = rearrange @text
270 @attributes = rearrange @attributes
272 # clear the lines we're recycling
273 for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
274 for x in [0...@width]
276 @attributes[y][x] = 0x07
282 arg = @fix_esc_arg i, ''
285 @cursor_visible = true
287 if @saved_normal_screen?
288 log "ignoring request to switch to the alt screen because we're already on the alt screen"
290 @saved_normal_screen = [@x, @y, @text, @attributes]
293 for y in [0...@height]
296 for x in [0...@width]
298 @attributes[y].push 0x07
300 log "confusing arg for csiq_h: #{arg}"
307 arg = @fix_esc_arg i, ''
310 @cursor_visible = false
312 if not @saved_normal_screen?
313 log "ignoring request to switch to the normal screen because we're already on the normal screen"
315 @x = @saved_normal_screen[0]
316 @y = @saved_normal_screen[1]
317 @text = @saved_normal_screen[2]
318 @attributes = @saved_normal_screen[3]
319 @saved_normal_screen = null
321 log "confusing arg for csiq_l: #{arg}"
324 # set color, bold, underline, etc
328 args.push @fix_esc_arg i, '0'
330 while args.length > 0
333 # remove all style/color
339 @set_attribute_bits 0x010000, 0x010000
340 when '3' # italic (rare)
341 @set_attribute_bits 0x200000, 0x200000
343 @set_attribute_bits 0x020000, 0x020000
345 @set_attribute_bits 0x040000, 0x040000
347 @set_attribute_bits 0x080000, 0x080000
348 when '8' # invisible. urivt ignores this
349 @set_attribute_bits 0x100000, 0x100000
351 # disable style attributes
352 when '21' # not bold (rare)
353 @set_attribute_bits 0x010000, 0
355 @set_attribute_bits 0x010000, 0
356 when '23' # not italic (rare)
357 @set_attribute_bits 0x200000, 0
358 when '24' # not underline
359 @set_attribute_bits 0x020000, 0
360 when '25' # not blink
361 @set_attribute_bits 0x040000, 0
362 when '27' # not inverse
363 @set_attribute_bits 0x080000, 0
364 when '28' # not invisible
365 @set_attribute_bits 0x100000, 0
367 when '100' # reset colors but not other attributes
368 @set_attribute_bits 0xffff, 0x0007
372 @set_attribute_bits 0xff, 0x00
374 @set_attribute_bits 0xff, 0x01
376 @set_attribute_bits 0xff, 0x02
377 when '33' # fg yellow
378 @set_attribute_bits 0xff, 0x03
380 @set_attribute_bits 0xff, 0x04
381 when '35' # fg magenta
382 @set_attribute_bits 0xff, 0x05
384 @set_attribute_bits 0xff, 0x06
385 when '37', '39' # fg white (39 is default)
386 @set_attribute_bits 0xff, 0x07
389 if args.length >= 2 and args[0] is '5'
391 @set_attribute_bits 0xff, (0xff & args.shift())
393 @set_attribute_bits 0x20000, 0x20000
397 @set_attribute_bits 0xff00, 0x0000
399 @set_attribute_bits 0xff00, 0x0100
401 @set_attribute_bits 0xff00, 0x0200
402 when '43' # bg yellow
403 @set_attribute_bits 0xff00, 0x0300
405 @set_attribute_bits 0xff00, 0x0400
406 when '45' # bg magenta
407 @set_attribute_bits 0xff00, 0x0500
409 @set_attribute_bits 0xff00, 0x0600
411 @set_attribute_bits 0xff00, 0x0700
412 when '49' # bg default
413 @set_attribute_bits 0xff00, 0x0000
416 if args.length >= 2 and args[0] is '5'
418 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
420 @set_attribute_bits 0x20000, 0x20000
423 when '90' # fg bright black
424 @set_attribute_bits 0xff, 0x08
425 when '91' # fg bright red
426 @set_attribute_bits 0xff, 0x09
427 when '92' # fg bright green
428 @set_attribute_bits 0xff, 0x0a
429 when '93' # fg bright yellow
430 @set_attribute_bits 0xff, 0x0b
431 when '94' # fg bright blue
432 @set_attribute_bits 0xff, 0x0c
433 when '95' # fg bright magenta
434 @set_attribute_bits 0xff, 0x0d
435 when '96' # fg bright cyan
436 @set_attribute_bits 0xff, 0x0e
437 when '97' # fg bright white
438 @set_attribute_bits 0xff, 0x0f
441 when '100' # bg bright black
442 @set_attribute_bits 0xff, 0x08
443 when '101' # bg bright red
444 @set_attribute_bits 0xff, 0x09
445 when '102' # bg bright green
446 @set_attribute_bits 0xff, 0x0a
447 when '103' # bg bright yellow
448 @set_attribute_bits 0xff, 0x0b
449 when '104' # bg bright blue
450 @set_attribute_bits 0xff, 0x0c
451 when '105' # bg bright magenta
452 @set_attribute_bits 0xff, 0x0d
453 when '106' # bg bright cyan
454 @set_attribute_bits 0xff, 0x0e
455 when '107' # bg bright white
456 @set_attribute_bits 0xff, 0x0f
459 # if we don't recognize the style, go back to default
460 log "unrecognized csi_m arg: \"#{arg}\""
464 # set scrolling region
465 csi_r: (top, bottom) ->
466 top = -1 + parseInt @fix_esc_arg top, '1'
467 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
473 @scroll_bottom = bottom
476 # str is the whole escape sequence (minus the esc[ prefix)
477 update_sequence: (str) ->
482 command = @[prefix + str.substr(str.length - 1)]
484 log "Unrecognized sequence: ESC[#{str}"
486 args = str.substr(0, str.length - 1).split ';'
487 command.call this, args...
489 update_sequence_then_text: (str) ->
490 len = @escape_sequence_length str
492 log "couldn't find escape sequence here: #{str.substr 0, 25}"
493 @update_text "ESC[" + str
495 @update_sequence str.substr 0, len
496 @update_text str.substr len
498 escape_sequence_length: (str) ->
499 parts = str.match(/^[0-9;?]{0,25}./)
500 return -1 unless parts?
501 return parts[0].length
503 my_exports.new = (width, height) ->
504 return new Terminal width, height