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
24 resize: (width, height) ->
25 # FIXME: write a version that retains some of the data
37 # pass data from stdout
39 return unless data?.length > 0
40 if @partial.length > 0
41 data = @partial + data
43 parts = data.split(/\x1b\[/)
45 if -1 is @escape_sequence_length parts[parts.length - 1]
46 @partial = parts.pop()
48 for i in [0...parts.length]
52 @update_sequence_then_text parts[i]
60 # move (newly cleared) top line to the bottom
63 tmp = @attributes.shift()
65 # slide cursor up with rest of text
74 # str has no escape sequences
76 return unless str.length > 0
80 @update_text " ".substr(@x % 8)
85 when '\x08' # backspace
92 when '\x0a', '\x0b' # lf, vertical tab (same thing)
98 @attributes[@y][@x] = @a
102 set_attribute_bits: (mask, value) ->
103 @a = ((@a & ~mask) | value)
105 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
106 # in the default for blank or missing values
107 fix_esc_arg: (value, deef_alt) ->
108 if value? and value != ''
109 while value[0] is '0' and value.length > 1
110 value = value.substr 1
115 # csi_@: rxvt does nothing I can detect
119 lines = parseInt @fix_esc_arg lines, '1'
126 lines = parseInt @fix_esc_arg lines, '1'
133 cols = parseInt @fix_esc_arg cols, '1'
140 cols = parseInt @fix_esc_arg cols, '1'
145 # set cursor position (one based)
146 csi_H: (row, column) ->
147 # handle blank/missing args and convert to 0 base
148 row = -1 + parseInt @fix_esc_arg row, '1'
149 column = -1 + parseInt @fix_esc_arg column, '1'
154 else if column >= @width
165 # clear to screen edge(es)
166 csi_J: (direction) ->
167 switch @fix_esc_arg direction, '0'
168 when '0' # erase down
169 # rest of current line
172 for row in [@y...@height]
173 for i in [0...@width]
175 @attributes[row][i] = @a
177 # beginning of current line
181 for i in [0...@width]
183 @attributes[row][i] = @a
184 when '2' # erase everything
185 for row in [0...@height]
186 for i in [0...@width]
188 @attributes[row][i] = @a
190 console.log "confusing arg for csi_J: #{direction}"
193 # clear (some or all of) current line
194 csi_K: (direction) ->
195 switch @fix_esc_arg direction, '0'
196 when '0' # erase to right
197 for i in [@x...@width]
199 @attributes[@y][i] = @a
200 when '1' # erase to left
201 # @x can equal @width (after printing to right-most column)
208 @attributes[@y][i] = @a
209 when '2' # erase whole line
210 for i in [0...@width]
212 @attributes[@y][i] = @a
214 console.log "confusing arg for csi_K: #{direction}"
221 arg = @fix_esc_arg i, ''
224 if @saved_normal_screen?
225 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
227 @saved_normal_screen = [@x, @y, @text, @attributes]
230 for y in [0...@height]
233 for x in [0...@width]
235 @attributes[y].push 0
237 console.log "confusing arg for csiq_h: #{arg}"
242 arg = @fix_esc_arg i, ''
245 if not @saved_normal_screen?
246 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
248 @x = @saved_normal_screen[0]
249 @y = @saved_normal_screen[1]
250 @text = @saved_normal_screen[2]
251 @attributes = @saved_normal_screen[3]
252 @saved_normal_screen = null
254 console.log "confusing arg for csiq_l: #{arg}"
256 # set color, bold, underline, etc
260 args.push @fix_esc_arg i, '0'
262 while args.length > 0
265 # remove all style/color
271 @set_attribute_bits 0x010000, 0x010000
272 when '3' # italic (rare)
273 @set_attribute_bits 0x200000, 0x200000
275 @set_attribute_bits 0x020000, 0x020000
277 @set_attribute_bits 0x040000, 0x040000
279 @set_attribute_bits 0x080000, 0x080000
280 when '8' # invisible. urivt ignores this
281 @set_attribute_bits 0x100000, 0x100000
283 # disable style attributes
284 when '21' # not bold (rare)
285 @set_attribute_bits 0x010000, 0
287 @set_attribute_bits 0x010000, 0
288 when '23' # not italic (rare)
289 @set_attribute_bits 0x200000, 0
290 when '24' # not underline
291 @set_attribute_bits 0x020000, 0
292 when '25' # not blink
293 @set_attribute_bits 0x040000, 0
294 when '27' # not inverse
295 @set_attribute_bits 0x080000, 0
296 when '28' # not invisible
297 @set_attribute_bits 0x100000, 0
299 when '100' # reset colors but not other attributes
300 @set_attribute_bits 0xffff, 0x0007
304 @set_attribute_bits 0xff, 0x00
306 @set_attribute_bits 0xff, 0x01
308 @set_attribute_bits 0xff, 0x02
309 when '33' # fg yellow
310 @set_attribute_bits 0xff, 0x03
312 @set_attribute_bits 0xff, 0x04
313 when '35' # fg magenta
314 @set_attribute_bits 0xff, 0x05
316 @set_attribute_bits 0xff, 0x06
317 when '37', '39' # fg white (39 is default)
318 @set_attribute_bits 0xff, 0x07
321 if args.length >= 2 and args[0] is '5'
323 @set_attribute_bits 0xff, (0xff & args.shift())
325 @set_attribute_bits 0x20000, 0x20000
329 @set_attribute_bits 0xff00, 0x0000
331 @set_attribute_bits 0xff00, 0x0100
333 @set_attribute_bits 0xff00, 0x0200
334 when '43' # bg yellow
335 @set_attribute_bits 0xff00, 0x0300
337 @set_attribute_bits 0xff00, 0x0400
338 when '45' # bg magenta
339 @set_attribute_bits 0xff00, 0x0500
341 @set_attribute_bits 0xff00, 0x0600
343 @set_attribute_bits 0xff00, 0x0700
344 when '49' # bg default
345 @set_attribute_bits 0xff00, 0x0000
348 if args.length >= 2 and args[0] is '5'
350 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
352 @set_attribute_bits 0x20000, 0x20000
355 when '90' # fg bright black
356 @set_attribute_bits 0xff, 0x08
357 when '91' # fg bright red
358 @set_attribute_bits 0xff, 0x09
359 when '92' # fg bright green
360 @set_attribute_bits 0xff, 0x0a
361 when '93' # fg bright yellow
362 @set_attribute_bits 0xff, 0x0b
363 when '94' # fg bright blue
364 @set_attribute_bits 0xff, 0x0c
365 when '95' # fg bright magenta
366 @set_attribute_bits 0xff, 0x0d
367 when '96' # fg bright cyan
368 @set_attribute_bits 0xff, 0x0e
369 when '97' # fg bright white
370 @set_attribute_bits 0xff, 0x0f
373 when '100' # bg bright black
374 @set_attribute_bits 0xff, 0x08
375 when '101' # bg bright red
376 @set_attribute_bits 0xff, 0x09
377 when '102' # bg bright green
378 @set_attribute_bits 0xff, 0x0a
379 when '103' # bg bright yellow
380 @set_attribute_bits 0xff, 0x0b
381 when '104' # bg bright blue
382 @set_attribute_bits 0xff, 0x0c
383 when '105' # bg bright magenta
384 @set_attribute_bits 0xff, 0x0d
385 when '106' # bg bright cyan
386 @set_attribute_bits 0xff, 0x0e
387 when '107' # bg bright white
388 @set_attribute_bits 0xff, 0x0f
391 # if we don't recognize the style, go back to default
392 console.log "unrecognized csi_m arg: \"#{arg}\""
396 # str is the whole escape sequence (minus the esc[ prefix)
397 update_sequence: (str) ->
402 command = @[prefix + str.substr(str.length - 1)]
404 console.log "Unrecognized sequence: ESC[#{str}"
406 args = str.substr(0, str.length - 1).split ';'
407 command.call this, args...
409 update_sequence_then_text: (str) ->
410 len = @escape_sequence_length str
412 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
413 @update_text "ESC[" + str
415 @update_sequence str.substr 0, len
416 @update_text str.substr len
418 escape_sequence_length: (str) ->
419 parts = str.match(/^[0-9;?]{0,25}./)
420 return -1 unless parts?
421 return parts[0].length
423 my_exports.new = (width, height) ->
424 return new Terminal width, height