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 # clear (some or all of) current line
113 csi_K: (direction) ->
114 switch @fix_esc_arg direction, '0'
115 when '0' # erase to right
116 for i in [@x...@width]
118 @attributes[@y][i] = @a
119 when '1' # erase to left
122 @attributes[@y][i] = @a
123 when '0' # erase whole line
124 for i in [0...@width]
126 @attributes[@y][i] = @a
128 # set cursor position (one based)
129 csi_H: (row, column) ->
130 row = 0 + @fix_esc_arg row, 1
131 column = 0 + @fix_esc_arg column, 1
134 if 0 <= column < @width
137 console.log "tried to move cursor to invalid column: #{column}"
139 if 0 <= row < @height
142 console.log "tried to move cursor to invalid row: #{row}"
144 # set color, bold, underline, etc
148 args.push @fix_esc_arg i, 0
150 while args.length > 0
153 # remove all style/color
155 @set_attribute_bits 0xffffff, 0x000007
159 @set_attribute_bits 0x10000, 0x10000
161 @set_attribute_bits 0x20000, 0x20000
163 @set_attribute_bits 0x40000, 0x40000
165 @set_attribute_bits 0x80000, 0x80000
167 # disable style attributes
168 when '22' # not bold... according to a page
169 @set_attribute_bits 0x10000, 0
170 when '21' # ... though this would make more sense for "not bold"
171 @set_attribute_bits 0x10000, 0
172 when '24' # not underline
173 @set_attribute_bits 0x20000, 0
174 when '25' # not blink
175 @set_attribute_bits 0x40000, 0
176 when '28' # not invisible
177 @set_attribute_bits 0x80000, 0
179 when '100' # reset colors but not other attributes
180 @set_attribute_bits 0xffff, 0x0007
184 @set_attribute_bits 0xff, 0x00
186 @set_attribute_bits 0xff, 0x01
188 @set_attribute_bits 0xff, 0x02
189 when '33' # fg yellow
190 @set_attribute_bits 0xff, 0x03
192 @set_attribute_bits 0xff, 0x04
193 when '35' # fg magenta
194 @set_attribute_bits 0xff, 0x05
196 @set_attribute_bits 0xff, 0x06
197 when '37', '39' # fg white (39 is default)
198 @set_attribute_bits 0xff, 0x07
201 if args.length >= 2 and args[0] is '5'
203 @set_attribute_bits 0xff, (0xff & args.shift())
205 @set_attribute_bits 0x20000, 0x20000
209 @set_attribute_bits 0xff00, 0x0000
211 @set_attribute_bits 0xff00, 0x0100
213 @set_attribute_bits 0xff00, 0x0200
214 when '43' # bg yellow
215 @set_attribute_bits 0xff00, 0x0300
217 @set_attribute_bits 0xff00, 0x0400
218 when '45' # bg magenta
219 @set_attribute_bits 0xff00, 0x0500
221 @set_attribute_bits 0xff00, 0x0600
223 @set_attribute_bits 0xff00, 0x0700
224 when '49' # bg default
225 @set_attribute_bits 0xff00, 0x0000
228 if args.length >= 2 and args[0] is '5'
230 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
232 @set_attribute_bits 0x20000, 0x20000
235 when '90' # fg bright black
236 @set_attribute_bits 0xff, 0x08
237 when '91' # fg bright red
238 @set_attribute_bits 0xff, 0x09
239 when '92' # fg bright green
240 @set_attribute_bits 0xff, 0x0a
241 when '93' # fg bright yellow
242 @set_attribute_bits 0xff, 0x0b
243 when '94' # fg bright blue
244 @set_attribute_bits 0xff, 0x0c
245 when '95' # fg bright magenta
246 @set_attribute_bits 0xff, 0x0d
247 when '96' # fg bright cyan
248 @set_attribute_bits 0xff, 0x0e
249 when '97' # fg bright white
250 @set_attribute_bits 0xff, 0x0f
253 when '100' # bg bright black
254 @set_attribute_bits 0xff, 0x08
255 when '101' # bg bright red
256 @set_attribute_bits 0xff, 0x09
257 when '102' # bg bright green
258 @set_attribute_bits 0xff, 0x0a
259 when '103' # bg bright yellow
260 @set_attribute_bits 0xff, 0x0b
261 when '104' # bg bright blue
262 @set_attribute_bits 0xff, 0x0c
263 when '105' # bg bright magenta
264 @set_attribute_bits 0xff, 0x0d
265 when '106' # bg bright cyan
266 @set_attribute_bits 0xff, 0x0e
267 when '107' # bg bright white
268 @set_attribute_bits 0xff, 0x0f
272 # if we don't recognize the style, go back to default
273 @set_attribute_bits 0xffffff, 0
276 # str is the whole escape sequence (minus the esc[ prefix)
277 update_sequence: (str) ->
278 command = @["csi_#{str.substr str.length - 1}"]
280 console.log "Unrecognized sequence: ESC[#{str}"
282 args = str.substr(0, str.length - 1).split ';'
283 command.call this, args...
285 update_sequence_then_text: (str) ->
286 len = @escape_sequence_length str
288 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
289 @update_text "ESC[" + str
291 @update_sequence str.substr 0, len
292 @update_text str.substr len
294 escape_sequence_length: (str) ->
295 parts = str.match(/^[0-9;?]{0,25}./)
296 return -1 unless parts?
297 return parts[0].length
299 my_exports.new = (width, height) ->
300 return new Terminal width, height