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
23 resize: (width, height) ->
24 # FIXME: write a version that retains some of the data
36 # pass data from stdout
38 return unless data?.length > 0
39 if @partial.length > 0
40 data = @partial + data
42 parts = data.split(/\x1b\[/)
44 if -1 is @escape_sequence_length parts[parts.length - 1]
45 @partial = parts.pop()
47 for i in [0...parts.length]
51 @update_sequence_then_text parts[i]
59 # move (newly cleared) top line to the bottom
62 tmp = @attributes.shift()
64 # slide cursor up with rest of text
73 # str has no escape sequences
75 return unless str.length > 0
79 @update_text " ".substr(@x % 8)
84 when '\x08' # backspace
88 # should this set the attribute too?
89 when '\x0a', '\x0b' # lf, vertical tab (same thing)
93 @attributes[@y][@x] = @a
99 set_attribute_bits: (mask, value) ->
100 @a = (@a & ~mask) | value
102 # we're supposed to ignore leeding zeros, and while we're at it, lets swap
103 # in the default for blank or missing values
104 fix_esc_arg: (value, deef_alt) ->
105 if value? and value != ''
106 while value[0] is '0' and value.length > 1
107 value = value.substr 1
112 # set cursor position (one based)
113 csi_H: (row, column) ->
114 row = 0 + @fix_esc_arg row, 1
115 column = 0 + @fix_esc_arg column, 1
118 if 0 <= column < @width
121 console.log "tried to move cursor to invalid column: #{column}"
123 if 0 <= row < @height
126 console.log "tried to move cursor to invalid row: #{row}"
128 # clear lines (implemented inclusive of the current line)
129 csi_J: (direction) ->
130 switch @fix_esc_arg direction, '0'
131 when '0' # erase down
132 for row in [@y...@height]
133 for i in [0...@width]
135 @attributes[row][i] = @a
138 for i in [0...@width]
140 @attributes[row][i] = @a
141 when '2' # erase everything
142 for row in [0...@height]
143 for i in [0...@width]
145 @attributes[row][i] = @a
147 console.log "confusing arg for csi_J: #{direction}"
149 # clear (some or all of) current line
150 csi_K: (direction) ->
151 switch @fix_esc_arg direction, '0'
152 when '0' # erase to right
153 for i in [@x...@width]
155 @attributes[@y][i] = @a
156 when '1' # erase to left
159 @attributes[@y][i] = @a
160 when '2' # erase whole line
161 for i in [0...@width]
163 @attributes[@y][i] = @a
165 console.log "confusing arg for csi_K: #{direction}"
167 # set color, bold, underline, etc
171 args.push @fix_esc_arg i, 0
173 while args.length > 0
176 # remove all style/color
178 @set_attribute_bits 0xffffff, 0x000007
182 @set_attribute_bits 0x10000, 0x10000
184 @set_attribute_bits 0x20000, 0x20000
186 @set_attribute_bits 0x40000, 0x40000
188 @set_attribute_bits 0x80000, 0x80000
190 # disable style attributes
191 when '22' # not bold... according to a page
192 @set_attribute_bits 0x10000, 0
193 when '21' # ... though this would make more sense for "not bold"
194 @set_attribute_bits 0x10000, 0
195 when '24' # not underline
196 @set_attribute_bits 0x20000, 0
197 when '25' # not blink
198 @set_attribute_bits 0x40000, 0
199 when '28' # not invisible
200 @set_attribute_bits 0x80000, 0
202 when '100' # reset colors but not other attributes
203 @set_attribute_bits 0xffff, 0x0007
207 @set_attribute_bits 0xff, 0x00
209 @set_attribute_bits 0xff, 0x01
211 @set_attribute_bits 0xff, 0x02
212 when '33' # fg yellow
213 @set_attribute_bits 0xff, 0x03
215 @set_attribute_bits 0xff, 0x04
216 when '35' # fg magenta
217 @set_attribute_bits 0xff, 0x05
219 @set_attribute_bits 0xff, 0x06
220 when '37', '39' # fg white (39 is default)
221 @set_attribute_bits 0xff, 0x07
224 if args.length >= 2 and args[0] is '5'
226 @set_attribute_bits 0xff, (0xff & args.shift())
228 @set_attribute_bits 0x20000, 0x20000
232 @set_attribute_bits 0xff00, 0x0000
234 @set_attribute_bits 0xff00, 0x0100
236 @set_attribute_bits 0xff00, 0x0200
237 when '43' # bg yellow
238 @set_attribute_bits 0xff00, 0x0300
240 @set_attribute_bits 0xff00, 0x0400
241 when '45' # bg magenta
242 @set_attribute_bits 0xff00, 0x0500
244 @set_attribute_bits 0xff00, 0x0600
246 @set_attribute_bits 0xff00, 0x0700
247 when '49' # bg default
248 @set_attribute_bits 0xff00, 0x0000
251 if args.length >= 2 and args[0] is '5'
253 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
255 @set_attribute_bits 0x20000, 0x20000
258 when '90' # fg bright black
259 @set_attribute_bits 0xff, 0x08
260 when '91' # fg bright red
261 @set_attribute_bits 0xff, 0x09
262 when '92' # fg bright green
263 @set_attribute_bits 0xff, 0x0a
264 when '93' # fg bright yellow
265 @set_attribute_bits 0xff, 0x0b
266 when '94' # fg bright blue
267 @set_attribute_bits 0xff, 0x0c
268 when '95' # fg bright magenta
269 @set_attribute_bits 0xff, 0x0d
270 when '96' # fg bright cyan
271 @set_attribute_bits 0xff, 0x0e
272 when '97' # fg bright white
273 @set_attribute_bits 0xff, 0x0f
276 when '100' # bg bright black
277 @set_attribute_bits 0xff, 0x08
278 when '101' # bg bright red
279 @set_attribute_bits 0xff, 0x09
280 when '102' # bg bright green
281 @set_attribute_bits 0xff, 0x0a
282 when '103' # bg bright yellow
283 @set_attribute_bits 0xff, 0x0b
284 when '104' # bg bright blue
285 @set_attribute_bits 0xff, 0x0c
286 when '105' # bg bright magenta
287 @set_attribute_bits 0xff, 0x0d
288 when '106' # bg bright cyan
289 @set_attribute_bits 0xff, 0x0e
290 when '107' # bg bright white
291 @set_attribute_bits 0xff, 0x0f
295 # if we don't recognize the style, go back to default
296 @set_attribute_bits 0xffffff, 0
299 # str is the whole escape sequence (minus the esc[ prefix)
300 update_sequence: (str) ->
301 command = @["csi_#{str.substr str.length - 1}"]
303 console.log "Unrecognized sequence: ESC[#{str}"
305 args = str.substr(0, str.length - 1).split ';'
306 command.call this, args...
308 update_sequence_then_text: (str) ->
309 len = @escape_sequence_length str
311 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
312 @update_text "ESC[" + str
314 @update_sequence str.substr 0, len
315 @update_text str.substr len
317 escape_sequence_length: (str) ->
318 parts = str.match(/^[0-9;?]{0,25}./)
319 return -1 unless parts?
320 return parts[0].length
322 my_exports.new = (width, height) ->
323 return new Terminal width, height