JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
csi_* in alphabetic order in source
[watch-my-terminal.git] / terminal.coffee
1 # this file is used by the client and server.
2
3 # work around lack of module system in the browser:
4 if exports?
5         my_exports = exports
6 else
7         window.terminal = {}
8         my_exports = window.terminal
9
10 class Terminal
11         # public:
12         constructor: (width, height) ->
13                 @width = 1
14                 @height = 1
15                 @text = []
16                 @attributes = []
17                 @x = 0
18                 @y = 0
19                 @a = 0x000007 # cursor attributes
20                 @partial = ''
21                 @resize width, height
22
23         resize: (width, height) ->
24                 # FIXME: write a version that retains some of the data
25                 @width = width
26                 @height = height
27                 @text = []
28                 @attributes = []
29                 for y in [0...height]
30                         @text[y] = []
31                         @attributes[y] = []
32                         for x in [0...width]
33                                 @text[y].push ' '
34                                 @attributes[y].push 0
35
36         # pass data from stdout
37         update: (data) ->
38                 return unless data?.length > 0
39                 if @partial.length > 0
40                         data = @partial + data
41                         @partial = ''
42                 parts = data.split(/\x1b\[/)
43                 if parts.length > 1
44                         if -1 is @escape_sequence_length parts[parts.length - 1]
45                                 @partial = parts.pop()
46                 if parts.length > 0
47                         for i in [0...parts.length]
48                                 if i is 0
49                                         @update_text parts[i]
50                                 else
51                                         @update_sequence_then_text parts[i]
52                 return
53
54         add_new_line: ->
55                 # clear top line
56                 for i in [0...@width]
57                         @text[0][i] = ' '
58                         @attributes[0][i] = 0
59                 # move (newly cleared) top line to the bottom
60                 tmp = @text.shift()
61                 @text.push(tmp)
62                 tmp = @attributes.shift()
63                 @attributes.push(tmp)
64                 # slide cursor up with rest of text
65                 @y -= 1
66
67         wrap_to_next_line: ->
68                 if @y is @height - 1
69                         @add_new_line()
70                 @y += 1
71                 @x = 0
72
73         # str has no escape sequences
74         update_text: (str) ->
75                 return unless str.length > 0
76                 for c in str
77                         switch c
78                                 when '\t' # tab
79                                         @update_text "        ".substr(@x % 8)
80                                 when '\x07' # bell
81                                         false
82                                 when '\x0d' # cr
83                                         @x = 0
84                                 when '\x08' # backspace
85                                         if @x > 0
86                                                 @x -= 1
87                                                 @text[@y][@x] = ' '
88                                                 # should this set the attribute too?
89                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
90                                         @wrap_to_next_line()
91                                 else
92                                         @text[@y][@x] = c
93                                         @attributes[@y][@x] = @a
94                                         @x += 1
95                                         if @x is @width
96                                                 @wrap_to_next_line()
97                 return
98
99         set_attribute_bits: (mask, value) ->
100                 @a = (@a & ~mask) | value
101
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
108                         return value
109                 else
110                         return deef_alt
111
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
116                 # convert to 0 base
117                 column -= 1
118                 if 0 <= column < @width
119                         @x = column
120                 else
121                         console.log "tried to move cursor to invalid column: #{column}"
122                 row -= 1
123                 if 0 <= row < @height
124                         @y = row
125                 else
126                         console.log "tried to move cursor to invalid row: #{row}"
127
128         # clear (some or all of) current line
129         csi_K: (direction) ->
130                 switch @fix_esc_arg direction, '0'
131                         when '0' # erase to right
132                                 for i in [@x...@width]
133                                         @text[@y][i] = ' '
134                                         @attributes[@y][i] = @a
135                         when '1' # erase to left
136                                 for i in [0...@x]
137                                         @text[@y][i] = ' '
138                                         @attributes[@y][i] = @a
139                         when '0' # erase whole line
140                                 for i in [0...@width]
141                                         @text[@y][i] = ' '
142                                         @attributes[@y][i] = @a
143
144         # set color, bold, underline, etc
145         csi_m: ->
146                 args = []
147                 for i in arguments
148                         args.push @fix_esc_arg i, 0
149
150                 while args.length > 0
151                         switch args.shift()
152
153                                 # remove all style/color
154                                 when '0'
155                                         @set_attribute_bits 0xffffff, 0x000007
156
157                                 # style attributes
158                                 when '1' # bold
159                                         @set_attribute_bits 0x10000, 0x10000
160                                 when '4' # underline
161                                         @set_attribute_bits 0x20000, 0x20000
162                                 when '5' # blink
163                                         @set_attribute_bits 0x40000, 0x40000
164                                 when '8' # invisible
165                                         @set_attribute_bits 0x80000, 0x80000
166
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
178
179                                 when '100' # reset colors but not other attributes
180                                         @set_attribute_bits 0xffff, 0x0007
181
182                                 # 8 fg colors
183                                 when '30' # fg black
184                                         @set_attribute_bits 0xff, 0x00
185                                 when '31' # fg red
186                                         @set_attribute_bits 0xff, 0x01
187                                 when '32' # fg green
188                                         @set_attribute_bits 0xff, 0x02
189                                 when '33' # fg yellow
190                                         @set_attribute_bits 0xff, 0x03
191                                 when '34' # fg blue
192                                         @set_attribute_bits 0xff, 0x04
193                                 when '35' # fg magenta
194                                         @set_attribute_bits 0xff, 0x05
195                                 when '36' # fg cyan
196                                         @set_attribute_bits 0xff, 0x06
197                                 when '37', '39' # fg white  (39 is default)
198                                         @set_attribute_bits 0xff, 0x07
199
200                                 when '38'
201                                         if args.length >= 2 and args[0] is '5'
202                                                 args.shift()
203                                                 @set_attribute_bits 0xff, (0xff & args.shift())
204                                         else
205                                                 @set_attribute_bits 0x20000, 0x20000
206
207                                 # 8 bg colors
208                                 when '40' # bg black
209                                         @set_attribute_bits 0xff00, 0x0000
210                                 when '41' # bg red
211                                         @set_attribute_bits 0xff00, 0x0100
212                                 when '42' # bg green
213                                         @set_attribute_bits 0xff00, 0x0200
214                                 when '43' # bg yellow
215                                         @set_attribute_bits 0xff00, 0x0300
216                                 when '44' # bg blue
217                                         @set_attribute_bits 0xff00, 0x0400
218                                 when '45' # bg magenta
219                                         @set_attribute_bits 0xff00, 0x0500
220                                 when '46' # bg cyan
221                                         @set_attribute_bits 0xff00, 0x0600
222                                 when '47' # bg white
223                                         @set_attribute_bits 0xff00, 0x0700
224                                 when '49' # bg default
225                                         @set_attribute_bits 0xff00, 0x0000
226
227                                 when '48'
228                                         if args.length >= 2 and args[0] is '5'
229                                                 args.shift()
230                                                 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
231                                         else
232                                                 @set_attribute_bits 0x20000, 0x20000
233
234                                 # bright fg colors
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
251
252                                 # bright bg colors
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
269
270
271                                 else
272                                         # if we don't recognize the style, go back to default
273                                         @set_attribute_bits 0xffffff, 0
274                 return
275
276         # str is the whole escape sequence (minus the esc[ prefix)
277         update_sequence: (str) ->
278                 command = @["csi_#{str.substr str.length - 1}"]
279                 if not command?
280                         console.log "Unrecognized sequence: ESC[#{str}"
281                         return
282                 args = str.substr(0, str.length - 1).split ';'
283                 command.call this, args...
284
285         update_sequence_then_text: (str) ->
286                 len = @escape_sequence_length str
287                 if len is -1
288                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
289                         @update_text "ESC[" + str
290                 else
291                         @update_sequence str.substr 0, len
292                         @update_text str.substr len
293
294         escape_sequence_length: (str) ->
295                 parts = str.match(/^[0-9;?]{0,25}./)
296                 return -1 unless parts?
297                 return parts[0].length
298
299 my_exports.new = (width, height) ->
300         return new Terminal width, height