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 @set_attribute_bits 0xffffff, 0
116 @set_attribute_bits 0x10000, 0x10000
118 @set_attribute_bits 0x20000, 0x20000
120 @set_attribute_bits 0x40000, 0x40000
122 @set_attribute_bits 0x80000, 0x80000
124 when '22' # not bold... according to a page
125 @set_attribute_bits 0x10000, 0
126 when '21' # ... though this would make more sense for "not bold"
127 @set_attribute_bits 0x10000, 0
128 when '24' # not underline
129 @set_attribute_bits 0x20000, 0
130 when '25' # not blink
131 @set_attribute_bits 0x40000, 0
132 when '28' # not invisible
133 @set_attribute_bits 0x80000, 0
136 @set_attribute_bits 0xff, 0x00
138 @set_attribute_bits 0xff, 0x01
140 @set_attribute_bits 0xff, 0x02
141 when '33' # fg yellow
142 @set_attribute_bits 0xff, 0x03
144 @set_attribute_bits 0xff, 0x04
145 when '35' # fg magenta
146 @set_attribute_bits 0xff, 0x05
148 @set_attribute_bits 0xff, 0x06
149 when '37', '39' # fg white (39 is default)
150 @set_attribute_bits 0xff, 0x07
153 @set_attribute_bits 0xff00, 0x0000
155 @set_attribute_bits 0xff00, 0x0100
157 @set_attribute_bits 0xff00, 0x0200
158 when '43' # bg yellow
159 @set_attribute_bits 0xff00, 0x0300
161 @set_attribute_bits 0xff00, 0x0400
162 when '45' # bg magenta
163 @set_attribute_bits 0xff00, 0x0500
165 @set_attribute_bits 0xff00, 0x0600
167 @set_attribute_bits 0xff00, 0x0700
168 when '49' # bg default
169 @set_attribute_bits 0xff00, 0x0000
172 # if we don't recognize the style, go back to default
173 @set_attribute_bits 0xffffff, 0
176 # str is the whole escape sequence (minus the esc[ prefix)
177 update_sequence: (str) ->
178 command = @["csi_#{str.substr str.length - 1}"]
179 return unless command?
180 args = str.substr(0, str.length - 1).split ';'
181 for i in [0...args.length]
183 args[i] = command.default
184 command.go.call this, args...
186 update_sequence_then_text: (str) ->
187 len = @escape_sequence_length str
189 console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
190 @update_text "ESC[" + str
192 @update_sequence str.substr 0, len
193 @update_text str.substr len
195 escape_sequence_length: (str) ->
196 parts = str.match(/^[0-9;?]{0,25}./)
197 return -1 unless parts?
198 return parts[0].length
200 my_exports.new = (width, height) ->
201 return new Terminal width, height