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 # set cursor position (one based)
116 csi_H: (row, column) ->
117 row = 0 + @fix_esc_arg row, 1
118 column = 0 + @fix_esc_arg column, 1
126 else if column >= @width
137 # clear to screen edge(es)
138 csi_J: (direction) ->
139 switch @fix_esc_arg direction, '0'
140 when '0' # erase down
141 # rest of current line
144 for row in [@y...@height]
145 for i in [0...@width]
147 @attributes[row][i] = @a
149 # beginning of current line
153 for i in [0...@width]
155 @attributes[row][i] = @a
156 when '2' # erase everything
157 for row in [0...@height]
158 for i in [0...@width]
160 @attributes[row][i] = @a
162 console.log "confusing arg for csi_J: #{direction}"
164 # clear (some or all of) current line
165 csi_K: (direction) ->
166 switch @fix_esc_arg direction, '0'
167 when '0' # erase to right
168 for i in [@x...@width]
170 @attributes[@y][i] = @a
171 when '1' # erase to left
174 @attributes[@y][i] = @a
175 when '2' # erase whole line
176 for i in [0...@width]
178 @attributes[@y][i] = @a
180 console.log "confusing arg for csi_K: #{direction}"
186 switch @fix_esc_arg i, ''
188 if @saved_normal_screen?
189 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
191 @saved_normal_screen = [@x, @y, @text, @attributes]
194 for y in [0...@height]
197 for x in [0...@width]
199 @attributes[y].push 0
204 switch @fix_esc_arg i, ''
206 if not @saved_normal_screen?
207 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
209 @x = @saved_normal_screen[0]
210 @y = @saved_normal_screen[1]
211 @text = @saved_normal_screen[2]
212 @attributes = @saved_normal_screen[3]
213 @saved_normal_screen = null
215 # set color, bold, underline, etc
219 args.push @fix_esc_arg i, 0
221 while args.length > 0
224 # remove all style/color
226 @set_attribute_bits 0xffffff, 0x000007
230 @set_attribute_bits 0x10000, 0x10000
232 @set_attribute_bits 0x20000, 0x20000
234 @set_attribute_bits 0x40000, 0x40000
236 @set_attribute_bits 0x80000, 0x80000
238 # disable style attributes
239 when '22' # not bold... according to a page
240 @set_attribute_bits 0x10000, 0
241 when '21' # ... though this would make more sense for "not bold"
242 @set_attribute_bits 0x10000, 0
243 when '24' # not underline
244 @set_attribute_bits 0x20000, 0
245 when '25' # not blink
246 @set_attribute_bits 0x40000, 0
247 when '28' # not invisible
248 @set_attribute_bits 0x80000, 0
250 when '100' # reset colors but not other attributes
251 @set_attribute_bits 0xffff, 0x0007
255 @set_attribute_bits 0xff, 0x00
257 @set_attribute_bits 0xff, 0x01
259 @set_attribute_bits 0xff, 0x02
260 when '33' # fg yellow
261 @set_attribute_bits 0xff, 0x03
263 @set_attribute_bits 0xff, 0x04
264 when '35' # fg magenta
265 @set_attribute_bits 0xff, 0x05
267 @set_attribute_bits 0xff, 0x06
268 when '37', '39' # fg white (39 is default)
269 @set_attribute_bits 0xff, 0x07
272 if args.length >= 2 and args[0] is '5'
274 @set_attribute_bits 0xff, (0xff & args.shift())
276 @set_attribute_bits 0x20000, 0x20000
280 @set_attribute_bits 0xff00, 0x0000
282 @set_attribute_bits 0xff00, 0x0100
284 @set_attribute_bits 0xff00, 0x0200
285 when '43' # bg yellow
286 @set_attribute_bits 0xff00, 0x0300
288 @set_attribute_bits 0xff00, 0x0400
289 when '45' # bg magenta
290 @set_attribute_bits 0xff00, 0x0500
292 @set_attribute_bits 0xff00, 0x0600
294 @set_attribute_bits 0xff00, 0x0700
295 when '49' # bg default
296 @set_attribute_bits 0xff00, 0x0000
299 if args.length >= 2 and args[0] is '5'
301 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
303 @set_attribute_bits 0x20000, 0x20000
306 when '90' # fg bright black
307 @set_attribute_bits 0xff, 0x08
308 when '91' # fg bright red
309 @set_attribute_bits 0xff, 0x09
310 when '92' # fg bright green
311 @set_attribute_bits 0xff, 0x0a
312 when '93' # fg bright yellow
313 @set_attribute_bits 0xff, 0x0b
314 when '94' # fg bright blue
315 @set_attribute_bits 0xff, 0x0c
316 when '95' # fg bright magenta
317 @set_attribute_bits 0xff, 0x0d
318 when '96' # fg bright cyan
319 @set_attribute_bits 0xff, 0x0e
320 when '97' # fg bright white
321 @set_attribute_bits 0xff, 0x0f
324 when '100' # bg bright black
325 @set_attribute_bits 0xff, 0x08
326 when '101' # bg bright red
327 @set_attribute_bits 0xff, 0x09
328 when '102' # bg bright green
329 @set_attribute_bits 0xff, 0x0a
330 when '103' # bg bright yellow
331 @set_attribute_bits 0xff, 0x0b
332 when '104' # bg bright blue
333 @set_attribute_bits 0xff, 0x0c
334 when '105' # bg bright magenta
335 @set_attribute_bits 0xff, 0x0d
336 when '106' # bg bright cyan
337 @set_attribute_bits 0xff, 0x0e
338 when '107' # bg bright white
339 @set_attribute_bits 0xff, 0x0f
343 # if we don't recognize the style, go back to default
344 @set_attribute_bits 0xffffff, 0
347 # str is the whole escape sequence (minus the esc[ prefix)
348 update_sequence: (str) ->
353 command = @[prefix + str.substr(str.length - 1)]
355 console.log "Unrecognized sequence: ESC[#{str}"
357 args = str.substr(0, str.length - 1).split ';'
358 command.call this, args...
360 update_sequence_then_text: (str) ->
361 len = @escape_sequence_length str
363 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
364 @update_text "ESC[" + str
366 @update_sequence str.substr 0, len
367 @update_text str.substr len
369 escape_sequence_length: (str) ->
370 parts = str.match(/^[0-9;?]{0,25}./)
371 return -1 unless parts?
372 return parts[0].length
374 my_exports.new = (width, height) ->
375 return new Terminal width, height