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 = 0 # 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: ->
110 while fixed[0] is '0'
111 fixed = fixed.substr 1
114 # remove all style/color
115 when '' # leading zeros are removed (even if that's all of them)
116 @set_attribute_bits 0xffffff, 0
120 @set_attribute_bits 0x10000, 0x10000
122 @set_attribute_bits 0x20000, 0x20000
124 @set_attribute_bits 0x40000, 0x40000
126 @set_attribute_bits 0x80000, 0x80000
128 # disable style attributes
129 when '22' # not bold... according to a page
130 @set_attribute_bits 0x10000, 0
131 when '21' # ... though this would make more sense for "not bold"
132 @set_attribute_bits 0x10000, 0
133 when '24' # not underline
134 @set_attribute_bits 0x20000, 0
135 when '25' # not blink
136 @set_attribute_bits 0x40000, 0
137 when '28' # not invisible
138 @set_attribute_bits 0x80000, 0
142 @set_attribute_bits 0xff, 0x00
144 @set_attribute_bits 0xff, 0x01
146 @set_attribute_bits 0xff, 0x02
147 when '33' # fg yellow
148 @set_attribute_bits 0xff, 0x03
150 @set_attribute_bits 0xff, 0x04
151 when '35' # fg magenta
152 @set_attribute_bits 0xff, 0x05
154 @set_attribute_bits 0xff, 0x06
155 when '37', '39' # fg white (39 is default)
156 @set_attribute_bits 0xff, 0x07
160 @set_attribute_bits 0xff00, 0x0000
162 @set_attribute_bits 0xff00, 0x0100
164 @set_attribute_bits 0xff00, 0x0200
165 when '43' # bg yellow
166 @set_attribute_bits 0xff00, 0x0300
168 @set_attribute_bits 0xff00, 0x0400
169 when '45' # bg magenta
170 @set_attribute_bits 0xff00, 0x0500
172 @set_attribute_bits 0xff00, 0x0600
174 @set_attribute_bits 0xff00, 0x0700
175 when '49' # bg default
176 @set_attribute_bits 0xff00, 0x0000
179 when '90' # fg bright black
180 @set_attribute_bits 0xff, 0x08
181 when '91' # fg bright red
182 @set_attribute_bits 0xff, 0x09
183 when '92' # fg bright green
184 @set_attribute_bits 0xff, 0x0a
185 when '93' # fg bright yellow
186 @set_attribute_bits 0xff, 0x0b
187 when '94' # fg bright blue
188 @set_attribute_bits 0xff, 0x0c
189 when '95' # fg bright magenta
190 @set_attribute_bits 0xff, 0x0d
191 when '96' # fg bright cyan
192 @set_attribute_bits 0xff, 0x0e
193 when '97' # fg bright white
194 @set_attribute_bits 0xff, 0x0f
197 when '100' # bg bright black
198 @set_attribute_bits 0xff, 0x08
199 when '101' # bg bright red
200 @set_attribute_bits 0xff, 0x09
201 when '102' # bg bright green
202 @set_attribute_bits 0xff, 0x0a
203 when '103' # bg bright yellow
204 @set_attribute_bits 0xff, 0x0b
205 when '104' # bg bright blue
206 @set_attribute_bits 0xff, 0x0c
207 when '105' # bg bright magenta
208 @set_attribute_bits 0xff, 0x0d
209 when '106' # bg bright cyan
210 @set_attribute_bits 0xff, 0x0e
211 when '107' # bg bright white
212 @set_attribute_bits 0xff, 0x0f
216 # if we don't recognize the style, go back to default
217 @set_attribute_bits 0xffffff, 0
220 # str is the whole escape sequence (minus the esc[ prefix)
221 update_sequence: (str) ->
222 command = @["csi_#{str.substr str.length - 1}"]
223 return unless command?
224 args = str.substr(0, str.length - 1).split ';'
225 for i in [0...args.length]
227 args[i] = command.default
228 command.go.call this, args...
230 update_sequence_then_text: (str) ->
231 len = @escape_sequence_length str
233 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
234 @update_text "ESC[" + str
236 @update_sequence str.substr 0, len
237 @update_text str.substr len
239 escape_sequence_length: (str) ->
240 parts = str.match(/^[0-9;?]{0,25}./)
241 return -1 unless parts?
242 return parts[0].length
244 my_exports.new = (width, height) ->
245 return new Terminal width, height