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
25 resize: (width, height) ->
26 # FIXME: write a version that retains some of the data
36 @attributes[y].push 0x07
38 # pass data from stdout
40 return unless data?.length > 0
41 if @partial.length > 0
42 data = @partial + data
44 parts = data.split(/\x1b\[/)
46 if -1 is @escape_sequence_length parts[parts.length - 1]
47 @partial = parts.pop()
49 for i in [0...parts.length]
53 @update_sequence_then_text parts[i]
60 @attributes[0][i] = 0x07
61 # move (newly cleared) top line to the bottom
64 tmp = @attributes.shift()
66 # slide cursor up with rest of text
75 # str has no escape sequences
77 return unless str.length > 0
81 @update_text " ".substr(@x % 8)
86 when '\x08' # backspace
93 when '\x0a', '\x0b' # lf, vertical tab (same thing)
99 @attributes[@y][@x] = @a
103 set_attribute_bits: (mask, value) ->
104 @a = ((@a & ~mask) | value)
106 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
107 # in the default for blank or missing values
108 fix_esc_arg: (value, deef_alt) ->
109 if value? and value != ''
110 while value[0] is '0' and value.length > 1
111 value = value.substr 1
116 # csi_@: rxvt does nothing I can detect
120 lines = parseInt @fix_esc_arg lines, '1'
127 lines = parseInt @fix_esc_arg lines, '1'
134 cols = parseInt @fix_esc_arg cols, '1'
141 cols = parseInt @fix_esc_arg cols, '1'
146 # set cursor position (one based)
147 csi_H: (row, column) ->
148 # handle blank/missing args and convert to 0 base
149 row = -1 + parseInt @fix_esc_arg row, '1'
150 column = -1 + parseInt @fix_esc_arg column, '1'
155 else if column >= @width
166 # clear to screen edge(es)
167 csi_J: (direction) ->
168 switch @fix_esc_arg direction, '0'
169 when '0' # erase down
170 # rest of current line
173 for row in [@y...@height]
174 for i in [0...@width]
176 @attributes[row][i] = @a
178 # beginning of current line
182 for i in [0...@width]
184 @attributes[row][i] = @a
185 when '2' # erase everything
186 for row in [0...@height]
187 for i in [0...@width]
189 @attributes[row][i] = @a
191 console.log "confusing arg for csi_J: #{direction}"
194 # clear (some or all of) current line
195 csi_K: (direction) ->
196 switch @fix_esc_arg direction, '0'
197 when '0' # erase to right
198 for i in [@x...@width]
200 @attributes[@y][i] = @a
201 when '1' # erase to left
202 # @x can equal @width (after printing to right-most column)
209 @attributes[@y][i] = @a
210 when '2' # erase whole line
211 for i in [0...@width]
213 @attributes[@y][i] = @a
215 console.log "confusing arg for csi_K: #{direction}"
222 arg = @fix_esc_arg i, ''
225 @cursor_visible = true
227 if @saved_normal_screen?
228 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
230 @saved_normal_screen = [@x, @y, @text, @attributes]
233 for y in [0...@height]
236 for x in [0...@width]
238 @attributes[y].push 0x07
240 console.log "confusing arg for csiq_h: #{arg}"
245 arg = @fix_esc_arg i, ''
248 @cursor_visible = false
250 if not @saved_normal_screen?
251 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
253 @x = @saved_normal_screen[0]
254 @y = @saved_normal_screen[1]
255 @text = @saved_normal_screen[2]
256 @attributes = @saved_normal_screen[3]
257 @saved_normal_screen = null
259 console.log "confusing arg for csiq_l: #{arg}"
261 # set color, bold, underline, etc
265 args.push @fix_esc_arg i, '0'
267 while args.length > 0
270 # remove all style/color
276 @set_attribute_bits 0x010000, 0x010000
277 when '3' # italic (rare)
278 @set_attribute_bits 0x200000, 0x200000
280 @set_attribute_bits 0x020000, 0x020000
282 @set_attribute_bits 0x040000, 0x040000
284 @set_attribute_bits 0x080000, 0x080000
285 when '8' # invisible. urivt ignores this
286 @set_attribute_bits 0x100000, 0x100000
288 # disable style attributes
289 when '21' # not bold (rare)
290 @set_attribute_bits 0x010000, 0
292 @set_attribute_bits 0x010000, 0
293 when '23' # not italic (rare)
294 @set_attribute_bits 0x200000, 0
295 when '24' # not underline
296 @set_attribute_bits 0x020000, 0
297 when '25' # not blink
298 @set_attribute_bits 0x040000, 0
299 when '27' # not inverse
300 @set_attribute_bits 0x080000, 0
301 when '28' # not invisible
302 @set_attribute_bits 0x100000, 0
304 when '100' # reset colors but not other attributes
305 @set_attribute_bits 0xffff, 0x0007
309 @set_attribute_bits 0xff, 0x00
311 @set_attribute_bits 0xff, 0x01
313 @set_attribute_bits 0xff, 0x02
314 when '33' # fg yellow
315 @set_attribute_bits 0xff, 0x03
317 @set_attribute_bits 0xff, 0x04
318 when '35' # fg magenta
319 @set_attribute_bits 0xff, 0x05
321 @set_attribute_bits 0xff, 0x06
322 when '37', '39' # fg white (39 is default)
323 @set_attribute_bits 0xff, 0x07
326 if args.length >= 2 and args[0] is '5'
328 @set_attribute_bits 0xff, (0xff & args.shift())
330 @set_attribute_bits 0x20000, 0x20000
334 @set_attribute_bits 0xff00, 0x0000
336 @set_attribute_bits 0xff00, 0x0100
338 @set_attribute_bits 0xff00, 0x0200
339 when '43' # bg yellow
340 @set_attribute_bits 0xff00, 0x0300
342 @set_attribute_bits 0xff00, 0x0400
343 when '45' # bg magenta
344 @set_attribute_bits 0xff00, 0x0500
346 @set_attribute_bits 0xff00, 0x0600
348 @set_attribute_bits 0xff00, 0x0700
349 when '49' # bg default
350 @set_attribute_bits 0xff00, 0x0000
353 if args.length >= 2 and args[0] is '5'
355 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
357 @set_attribute_bits 0x20000, 0x20000
360 when '90' # fg bright black
361 @set_attribute_bits 0xff, 0x08
362 when '91' # fg bright red
363 @set_attribute_bits 0xff, 0x09
364 when '92' # fg bright green
365 @set_attribute_bits 0xff, 0x0a
366 when '93' # fg bright yellow
367 @set_attribute_bits 0xff, 0x0b
368 when '94' # fg bright blue
369 @set_attribute_bits 0xff, 0x0c
370 when '95' # fg bright magenta
371 @set_attribute_bits 0xff, 0x0d
372 when '96' # fg bright cyan
373 @set_attribute_bits 0xff, 0x0e
374 when '97' # fg bright white
375 @set_attribute_bits 0xff, 0x0f
378 when '100' # bg bright black
379 @set_attribute_bits 0xff, 0x08
380 when '101' # bg bright red
381 @set_attribute_bits 0xff, 0x09
382 when '102' # bg bright green
383 @set_attribute_bits 0xff, 0x0a
384 when '103' # bg bright yellow
385 @set_attribute_bits 0xff, 0x0b
386 when '104' # bg bright blue
387 @set_attribute_bits 0xff, 0x0c
388 when '105' # bg bright magenta
389 @set_attribute_bits 0xff, 0x0d
390 when '106' # bg bright cyan
391 @set_attribute_bits 0xff, 0x0e
392 when '107' # bg bright white
393 @set_attribute_bits 0xff, 0x0f
396 # if we don't recognize the style, go back to default
397 console.log "unrecognized csi_m arg: \"#{arg}\""
401 # str is the whole escape sequence (minus the esc[ prefix)
402 update_sequence: (str) ->
407 command = @[prefix + str.substr(str.length - 1)]
409 console.log "Unrecognized sequence: ESC[#{str}"
411 args = str.substr(0, str.length - 1).split ';'
412 command.call this, args...
414 update_sequence_then_text: (str) ->
415 len = @escape_sequence_length str
417 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
418 @update_text "ESC[" + str
420 @update_sequence str.substr 0, len
421 @update_text str.substr len
423 escape_sequence_length: (str) ->
424 parts = str.match(/^[0-9;?]{0,25}./)
425 return -1 unless parts?
426 return parts[0].length
428 my_exports.new = (width, height) ->
429 return new Terminal width, height