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
89 # should this set the attribute too?
90 when '\x0a', '\x0b' # lf, vertical tab (same thing)
94 @attributes[@y][@x] = @a
100 set_attribute_bits: (mask, value) ->
101 @a = (@a & ~mask) | value
103 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
104 # in the default for blank or missing values
105 fix_esc_arg: (value, deef_alt) ->
106 if value? and value != ''
107 while value[0] is '0' and value.length > 1
108 value = value.substr 1
113 # set cursor position (one based)
114 csi_H: (row, column) ->
115 row = 0 + @fix_esc_arg row, 1
116 column = 0 + @fix_esc_arg column, 1
124 else if column >= @width
135 # clear lines (implemented inclusive of the current line)
136 csi_J: (direction) ->
137 switch @fix_esc_arg direction, '0'
138 when '0' # erase down
139 for row in [@y...@height]
140 for i in [0...@width]
142 @attributes[row][i] = @a
145 for i in [0...@width]
147 @attributes[row][i] = @a
148 when '2' # erase everything
149 for row in [0...@height]
150 for i in [0...@width]
152 @attributes[row][i] = @a
154 console.log "confusing arg for csi_J: #{direction}"
156 # clear (some or all of) current line
157 csi_K: (direction) ->
158 switch @fix_esc_arg direction, '0'
159 when '0' # erase to right
160 for i in [@x...@width]
162 @attributes[@y][i] = @a
163 when '1' # erase to left
166 @attributes[@y][i] = @a
167 when '2' # erase whole line
168 for i in [0...@width]
170 @attributes[@y][i] = @a
172 console.log "confusing arg for csi_K: #{direction}"
178 switch @fix_esc_arg i, ''
180 if @saved_normal_screen?
181 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
183 @saved_normal_screen = [@x, @y, @text, @attributes]
186 for y in [0...@height]
189 for x in [0...@width]
191 @attributes[y].push 0
196 switch @fix_esc_arg i, ''
198 if not @saved_normal_screen?
199 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
201 @x = @saved_normal_screen[0]
202 @y = @saved_normal_screen[1]
203 @text = @saved_normal_screen[2]
204 @attributes = @saved_normal_screen[3]
205 @saved_normal_screen = null
207 # set color, bold, underline, etc
211 args.push @fix_esc_arg i, 0
213 while args.length > 0
216 # remove all style/color
218 @set_attribute_bits 0xffffff, 0x000007
222 @set_attribute_bits 0x10000, 0x10000
224 @set_attribute_bits 0x20000, 0x20000
226 @set_attribute_bits 0x40000, 0x40000
228 @set_attribute_bits 0x80000, 0x80000
230 # disable style attributes
231 when '22' # not bold... according to a page
232 @set_attribute_bits 0x10000, 0
233 when '21' # ... though this would make more sense for "not bold"
234 @set_attribute_bits 0x10000, 0
235 when '24' # not underline
236 @set_attribute_bits 0x20000, 0
237 when '25' # not blink
238 @set_attribute_bits 0x40000, 0
239 when '28' # not invisible
240 @set_attribute_bits 0x80000, 0
242 when '100' # reset colors but not other attributes
243 @set_attribute_bits 0xffff, 0x0007
247 @set_attribute_bits 0xff, 0x00
249 @set_attribute_bits 0xff, 0x01
251 @set_attribute_bits 0xff, 0x02
252 when '33' # fg yellow
253 @set_attribute_bits 0xff, 0x03
255 @set_attribute_bits 0xff, 0x04
256 when '35' # fg magenta
257 @set_attribute_bits 0xff, 0x05
259 @set_attribute_bits 0xff, 0x06
260 when '37', '39' # fg white (39 is default)
261 @set_attribute_bits 0xff, 0x07
264 if args.length >= 2 and args[0] is '5'
266 @set_attribute_bits 0xff, (0xff & args.shift())
268 @set_attribute_bits 0x20000, 0x20000
272 @set_attribute_bits 0xff00, 0x0000
274 @set_attribute_bits 0xff00, 0x0100
276 @set_attribute_bits 0xff00, 0x0200
277 when '43' # bg yellow
278 @set_attribute_bits 0xff00, 0x0300
280 @set_attribute_bits 0xff00, 0x0400
281 when '45' # bg magenta
282 @set_attribute_bits 0xff00, 0x0500
284 @set_attribute_bits 0xff00, 0x0600
286 @set_attribute_bits 0xff00, 0x0700
287 when '49' # bg default
288 @set_attribute_bits 0xff00, 0x0000
291 if args.length >= 2 and args[0] is '5'
293 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
295 @set_attribute_bits 0x20000, 0x20000
298 when '90' # fg bright black
299 @set_attribute_bits 0xff, 0x08
300 when '91' # fg bright red
301 @set_attribute_bits 0xff, 0x09
302 when '92' # fg bright green
303 @set_attribute_bits 0xff, 0x0a
304 when '93' # fg bright yellow
305 @set_attribute_bits 0xff, 0x0b
306 when '94' # fg bright blue
307 @set_attribute_bits 0xff, 0x0c
308 when '95' # fg bright magenta
309 @set_attribute_bits 0xff, 0x0d
310 when '96' # fg bright cyan
311 @set_attribute_bits 0xff, 0x0e
312 when '97' # fg bright white
313 @set_attribute_bits 0xff, 0x0f
316 when '100' # bg bright black
317 @set_attribute_bits 0xff, 0x08
318 when '101' # bg bright red
319 @set_attribute_bits 0xff, 0x09
320 when '102' # bg bright green
321 @set_attribute_bits 0xff, 0x0a
322 when '103' # bg bright yellow
323 @set_attribute_bits 0xff, 0x0b
324 when '104' # bg bright blue
325 @set_attribute_bits 0xff, 0x0c
326 when '105' # bg bright magenta
327 @set_attribute_bits 0xff, 0x0d
328 when '106' # bg bright cyan
329 @set_attribute_bits 0xff, 0x0e
330 when '107' # bg bright white
331 @set_attribute_bits 0xff, 0x0f
335 # if we don't recognize the style, go back to default
336 @set_attribute_bits 0xffffff, 0
339 # str is the whole escape sequence (minus the esc[ prefix)
340 update_sequence: (str) ->
345 command = @[prefix + str.substr(str.length - 1)]
347 console.log "Unrecognized sequence: ESC[#{str}"
349 args = str.substr(0, str.length - 1).split ';'
350 command.call this, args...
352 update_sequence_then_text: (str) ->
353 len = @escape_sequence_length str
355 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
356 @update_text "ESC[" + str
358 @update_sequence str.substr 0, len
359 @update_text str.substr len
361 escape_sequence_length: (str) ->
362 parts = str.match(/^[0-9;?]{0,25}./)
363 return -1 unless parts?
364 return parts[0].length
366 my_exports.new = (width, height) ->
367 return new Terminal width, height