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]
54 clear_rest_of_line: ->
55 for i in [@x...@width]
57 @attributes[@y][i] = @a
64 # move (newly cleared) top line to the bottom
67 tmp = @attributes.shift()
69 # slide cursor up with rest of text
78 # str has no escape sequences
80 return unless str.length > 0
84 @update_text " ".substr(@x % 8)
89 when '\x08' # backspace
93 # should this set the attribute too?
94 when '\x0a', '\x0b' # lf, vertical tab (same thing)
98 @attributes[@y][@x] = @a
104 set_attribute_bits: (mask, value) ->
105 @a = (@a & ~mask) | value
107 csi_m: default: "0", go: ->
112 while args.length > 0
114 while fixed[0] is '0'
115 fixed = fixed.substr 1
120 # remove all style/color
122 @set_attribute_bits 0xffffff, 0x000007
126 @set_attribute_bits 0x10000, 0x10000
128 @set_attribute_bits 0x20000, 0x20000
130 @set_attribute_bits 0x40000, 0x40000
132 @set_attribute_bits 0x80000, 0x80000
134 # disable style attributes
135 when '22' # not bold... according to a page
136 @set_attribute_bits 0x10000, 0
137 when '21' # ... though this would make more sense for "not bold"
138 @set_attribute_bits 0x10000, 0
139 when '24' # not underline
140 @set_attribute_bits 0x20000, 0
141 when '25' # not blink
142 @set_attribute_bits 0x40000, 0
143 when '28' # not invisible
144 @set_attribute_bits 0x80000, 0
146 when '100' # reset colors but not other attributes
147 @set_attribute_bits 0xffff, 0x0007
151 @set_attribute_bits 0xff, 0x00
153 @set_attribute_bits 0xff, 0x01
155 @set_attribute_bits 0xff, 0x02
156 when '33' # fg yellow
157 @set_attribute_bits 0xff, 0x03
159 @set_attribute_bits 0xff, 0x04
160 when '35' # fg magenta
161 @set_attribute_bits 0xff, 0x05
163 @set_attribute_bits 0xff, 0x06
164 when '37', '39' # fg white (39 is default)
165 @set_attribute_bits 0xff, 0x07
168 if args.length >= 2 and args[0] is '5'
170 @set_attribute_bits 0xff, (0xff & args.shift())
172 @set_attribute_bits 0x20000, 0x20000
176 @set_attribute_bits 0xff00, 0x0000
178 @set_attribute_bits 0xff00, 0x0100
180 @set_attribute_bits 0xff00, 0x0200
181 when '43' # bg yellow
182 @set_attribute_bits 0xff00, 0x0300
184 @set_attribute_bits 0xff00, 0x0400
185 when '45' # bg magenta
186 @set_attribute_bits 0xff00, 0x0500
188 @set_attribute_bits 0xff00, 0x0600
190 @set_attribute_bits 0xff00, 0x0700
191 when '49' # bg default
192 @set_attribute_bits 0xff00, 0x0000
195 if args.length >= 2 and args[0] is '5'
197 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
199 @set_attribute_bits 0x20000, 0x20000
202 when '90' # fg bright black
203 @set_attribute_bits 0xff, 0x08
204 when '91' # fg bright red
205 @set_attribute_bits 0xff, 0x09
206 when '92' # fg bright green
207 @set_attribute_bits 0xff, 0x0a
208 when '93' # fg bright yellow
209 @set_attribute_bits 0xff, 0x0b
210 when '94' # fg bright blue
211 @set_attribute_bits 0xff, 0x0c
212 when '95' # fg bright magenta
213 @set_attribute_bits 0xff, 0x0d
214 when '96' # fg bright cyan
215 @set_attribute_bits 0xff, 0x0e
216 when '97' # fg bright white
217 @set_attribute_bits 0xff, 0x0f
220 when '100' # bg bright black
221 @set_attribute_bits 0xff, 0x08
222 when '101' # bg bright red
223 @set_attribute_bits 0xff, 0x09
224 when '102' # bg bright green
225 @set_attribute_bits 0xff, 0x0a
226 when '103' # bg bright yellow
227 @set_attribute_bits 0xff, 0x0b
228 when '104' # bg bright blue
229 @set_attribute_bits 0xff, 0x0c
230 when '105' # bg bright magenta
231 @set_attribute_bits 0xff, 0x0d
232 when '106' # bg bright cyan
233 @set_attribute_bits 0xff, 0x0e
234 when '107' # bg bright white
235 @set_attribute_bits 0xff, 0x0f
239 # if we don't recognize the style, go back to default
240 @set_attribute_bits 0xffffff, 0
243 # str is the whole escape sequence (minus the esc[ prefix)
244 update_sequence: (str) ->
245 command = @["csi_#{str.substr str.length - 1}"]
246 return unless command?
247 args = str.substr(0, str.length - 1).split ';'
248 for i in [0...args.length]
250 args[i] = command.default
251 command.go.call this, args...
253 update_sequence_then_text: (str) ->
254 len = @escape_sequence_length str
256 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
257 @update_text "ESC[" + str
259 @update_sequence str.substr 0, len
260 @update_text str.substr len
262 escape_sequence_length: (str) ->
263 parts = str.match(/^[0-9;?]{0,25}./)
264 return -1 unless parts?
265 return parts[0].length
267 my_exports.new = (width, height) ->
268 return new Terminal width, height