JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add cursor incrementals movement: csi_[ABCD]
[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                 @saved_normal_screen = null
22                 @resize width, height
23
24         resize: (width, height) ->
25                 # FIXME: write a version that retains some of the data
26                 @width = width
27                 @height = height
28                 @text = []
29                 @attributes = []
30                 for y in [0...height]
31                         @text[y] = []
32                         @attributes[y] = []
33                         for x in [0...width]
34                                 @text[y].push ' '
35                                 @attributes[y].push 0
36
37         # pass data from stdout
38         update: (data) ->
39                 return unless data?.length > 0
40                 if @partial.length > 0
41                         data = @partial + data
42                         @partial = ''
43                 parts = data.split(/\x1b\[/)
44                 if parts.length > 1
45                         if -1 is @escape_sequence_length parts[parts.length - 1]
46                                 @partial = parts.pop()
47                 if parts.length > 0
48                         for i in [0...parts.length]
49                                 if i is 0
50                                         @update_text parts[i]
51                                 else
52                                         @update_sequence_then_text parts[i]
53                 return
54
55         add_new_line: ->
56                 # clear top line
57                 for i in [0...@width]
58                         @text[0][i] = ' '
59                         @attributes[0][i] = 0
60                 # move (newly cleared) top line to the bottom
61                 tmp = @text.shift()
62                 @text.push(tmp)
63                 tmp = @attributes.shift()
64                 @attributes.push(tmp)
65                 # slide cursor up with rest of text
66                 @y -= 1
67
68         wrap_to_next_line: ->
69                 if @y is @height - 1
70                         @add_new_line()
71                 @y += 1
72                 @x = 0
73
74         # str has no escape sequences
75         update_text: (str) ->
76                 return unless str.length > 0
77                 for c in str
78                         switch c
79                                 when '\t' # tab
80                                         @update_text "        ".substr(@x % 8)
81                                 when '\x07' # bell
82                                         false
83                                 when '\x0d' # cr
84                                         @x = 0
85                                 when '\x08' # backspace
86                                         if @x is 0
87                                                 @x = @width - 1
88                                                 if @y > 0
89                                                         @y -= 1
90                                         else
91                                                 @x -= 1
92                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
93                                         @wrap_to_next_line()
94                                 else
95                                         if @x >= @width
96                                                 @wrap_to_next_line()
97                                         @text[@y][@x] = c
98                                         @attributes[@y][@x] = @a
99                                         @x += 1
100                 return
101
102         set_attribute_bits: (mask, value) ->
103                 @a = ((@a & ~mask) | value)
104
105         # we're supposed to ignore leeding zeros, and while we're at it, lets swap
106         # in the default for blank or missing values
107         fix_esc_arg: (value, deef_alt) ->
108                 if value? and value != ''
109                         while value[0] is '0' and value.length > 1
110                                 value = value.substr 1
111                         return value
112                 else
113                         return deef_alt
114
115         # csi_@: rxvt does nothing I can detect
116
117         # move cursor up
118         csi_A: (lines) ->
119                 lines = parseInt @fix_esc_arg lines, '1'
120                 @y -= lines
121                 if @y < 0
122                         @y = 0
123
124         # move cursor down
125         csi_B: (lines) ->
126                 lines = parseInt @fix_esc_arg lines, '1'
127                 @y += lines
128                 if @y >= @height
129                         @y = @height - 1
130
131         # move cursor right
132         csi_C: (cols) ->
133                 cols = parseInt @fix_esc_arg cols, '1'
134                 @x += cols
135                 if @x >= @width
136                         @x = @width - 1
137
138         # move cursor left
139         csi_D: (cols) ->
140                 cols = parseInt @fix_esc_arg cols, '1'
141                 @x -= cols
142                 if @x < 0
143                         @x = 0
144
145         # set cursor position (one based)
146         csi_H: (row, column) ->
147                 # handle blank/missing args and convert to 0 base
148                 row = -1 + parseInt @fix_esc_arg row, '1'
149                 column = -1 + parseInt @fix_esc_arg column, '1'
150
151                 #clamp values
152                 if column < 0
153                         column = 0
154                 else if column >= @width
155                         column = @width - 1
156                 if row < 0
157                         row = 0
158                 if row >= @height
159                         row = @height - 1
160
161                 #move the cursor
162                 @x = column
163                 @y = row
164
165         # clear to screen edge(es)
166         csi_J: (direction) ->
167                 switch @fix_esc_arg direction, '0'
168                         when '0' # erase down
169                                 # rest of current line
170                                 @csi_K direction
171                                 # rest of lines
172                                 for row in [@y...@height]
173                                         for i in [0...@width]
174                                                 @text[row][i] = ' '
175                                                 @attributes[row][i] = @a
176                         when '1' # erase up
177                                 # beginning of current line
178                                 @csi_K direction
179                                 # all previous lines
180                                 for row in [0..@y]
181                                         for i in [0...@width]
182                                                 @text[row][i] = ' '
183                                                 @attributes[row][i] = @a
184                         when '2' # erase everything
185                                 for row in [0...@height]
186                                         for i in [0...@width]
187                                                 @text[row][i] = ' '
188                                                 @attributes[row][i] = @a
189                         else
190                                 console.log "confusing arg for csi_J: #{direction}"
191                 return
192
193         # clear (some or all of) current line
194         csi_K: (direction) ->
195                 switch @fix_esc_arg direction, '0'
196                         when '0' # erase to right
197                                 for i in [@x...@width]
198                                         @text[@y][i] = ' '
199                                         @attributes[@y][i] = @a
200                         when '1' # erase to left
201                                 # @x can equal @width (after printing to right-most column)
202                                 if @x < @width
203                                         max = @x
204                                 else
205                                         max = @width - 1
206                                 for i in [0..max]
207                                         @text[@y][i] = ' '
208                                         @attributes[@y][i] = @a
209                         when '2' # erase whole line
210                                 for i in [0...@width]
211                                         @text[@y][i] = ' '
212                                         @attributes[@y][i] = @a
213                         else
214                                 console.log "confusing arg for csi_K: #{direction}"
215                 return
216         
217         # misc
218         csiq_h: ->
219                 args = []
220                 for i in arguments
221                         arg = @fix_esc_arg i, ''
222                         switch arg
223                                 when '1049'
224                                         if @saved_normal_screen?
225                                                 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
226                                                 return
227                                         @saved_normal_screen = [@x, @y, @text, @attributes]
228                                         @text = []
229                                         @attributes = []
230                                         for y in [0...@height]
231                                                 @text[y] = []
232                                                 @attributes[y] = []
233                                                 for x in [0...@width]
234                                                         @text[y].push ' '
235                                                         @attributes[y].push 0
236                                 else
237                                         console.log "confusing arg for csiq_h: #{arg}"
238         # unmisc
239         csiq_l: ->
240                 args = []
241                 for i in arguments
242                         arg = @fix_esc_arg i, ''
243                         switch arg
244                                 when '1049'
245                                         if not @saved_normal_screen?
246                                                 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
247                                                 return
248                                         @x = @saved_normal_screen[0]
249                                         @y = @saved_normal_screen[1]
250                                         @text = @saved_normal_screen[2]
251                                         @attributes = @saved_normal_screen[3]
252                                         @saved_normal_screen = null
253                                 else
254                                         console.log "confusing arg for csiq_l: #{arg}"
255
256         # set color, bold, underline, etc
257         csi_m: ->
258                 args = []
259                 for i in arguments
260                         args.push @fix_esc_arg i, '0'
261
262                 while args.length > 0
263                         arg = args.shift()
264                         switch arg
265                                 # remove all style/color
266                                 when '0'
267                                         @a = 0x07
268
269                                 # style attributes
270                                 when '1' # bold
271                                         @set_attribute_bits 0x010000, 0x010000
272                                 when '3' # italic (rare)
273                                         @set_attribute_bits 0x200000, 0x200000
274                                 when '4' # underline
275                                         @set_attribute_bits 0x020000, 0x020000
276                                 when '5' # blink
277                                         @set_attribute_bits 0x040000, 0x040000
278                                 when '7' # inverse
279                                         @set_attribute_bits 0x080000, 0x080000
280                                 when '8' # invisible. urivt ignores this
281                                         @set_attribute_bits 0x100000, 0x100000
282
283                                 # disable style attributes
284                                 when '21' # not bold (rare)
285                                         @set_attribute_bits 0x010000, 0
286                                 when '22' # not bold
287                                         @set_attribute_bits 0x010000, 0
288                                 when '23' # not italic (rare)
289                                         @set_attribute_bits 0x200000, 0
290                                 when '24' # not underline
291                                         @set_attribute_bits 0x020000, 0
292                                 when '25' # not blink
293                                         @set_attribute_bits 0x040000, 0
294                                 when '27' # not inverse
295                                         @set_attribute_bits 0x080000, 0
296                                 when '28' # not invisible
297                                         @set_attribute_bits 0x100000, 0
298
299                                 when '100' # reset colors but not other attributes
300                                         @set_attribute_bits 0xffff, 0x0007
301
302                                 # 8 fg colors
303                                 when '30' # fg black
304                                         @set_attribute_bits 0xff, 0x00
305                                 when '31' # fg red
306                                         @set_attribute_bits 0xff, 0x01
307                                 when '32' # fg green
308                                         @set_attribute_bits 0xff, 0x02
309                                 when '33' # fg yellow
310                                         @set_attribute_bits 0xff, 0x03
311                                 when '34' # fg blue
312                                         @set_attribute_bits 0xff, 0x04
313                                 when '35' # fg magenta
314                                         @set_attribute_bits 0xff, 0x05
315                                 when '36' # fg cyan
316                                         @set_attribute_bits 0xff, 0x06
317                                 when '37', '39' # fg white  (39 is default)
318                                         @set_attribute_bits 0xff, 0x07
319
320                                 when '38'
321                                         if args.length >= 2 and args[0] is '5'
322                                                 args.shift()
323                                                 @set_attribute_bits 0xff, (0xff & args.shift())
324                                         else
325                                                 @set_attribute_bits 0x20000, 0x20000
326
327                                 # 8 bg colors
328                                 when '40' # bg black
329                                         @set_attribute_bits 0xff00, 0x0000
330                                 when '41' # bg red
331                                         @set_attribute_bits 0xff00, 0x0100
332                                 when '42' # bg green
333                                         @set_attribute_bits 0xff00, 0x0200
334                                 when '43' # bg yellow
335                                         @set_attribute_bits 0xff00, 0x0300
336                                 when '44' # bg blue
337                                         @set_attribute_bits 0xff00, 0x0400
338                                 when '45' # bg magenta
339                                         @set_attribute_bits 0xff00, 0x0500
340                                 when '46' # bg cyan
341                                         @set_attribute_bits 0xff00, 0x0600
342                                 when '47' # bg white
343                                         @set_attribute_bits 0xff00, 0x0700
344                                 when '49' # bg default
345                                         @set_attribute_bits 0xff00, 0x0000
346
347                                 when '48'
348                                         if args.length >= 2 and args[0] is '5'
349                                                 args.shift()
350                                                 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
351                                         else
352                                                 @set_attribute_bits 0x20000, 0x20000
353
354                                 # bright fg colors
355                                 when '90' # fg bright black
356                                         @set_attribute_bits 0xff, 0x08
357                                 when '91' # fg bright red
358                                         @set_attribute_bits 0xff, 0x09
359                                 when '92' # fg bright green
360                                         @set_attribute_bits 0xff, 0x0a
361                                 when '93' # fg bright yellow
362                                         @set_attribute_bits 0xff, 0x0b
363                                 when '94' # fg bright blue
364                                         @set_attribute_bits 0xff, 0x0c
365                                 when '95' # fg bright magenta
366                                         @set_attribute_bits 0xff, 0x0d
367                                 when '96' # fg bright cyan
368                                         @set_attribute_bits 0xff, 0x0e
369                                 when '97' # fg bright white
370                                         @set_attribute_bits 0xff, 0x0f
371
372                                 # bright bg colors
373                                 when '100' # bg bright black
374                                         @set_attribute_bits 0xff, 0x08
375                                 when '101' # bg bright red
376                                         @set_attribute_bits 0xff, 0x09
377                                 when '102' # bg bright green
378                                         @set_attribute_bits 0xff, 0x0a
379                                 when '103' # bg bright yellow
380                                         @set_attribute_bits 0xff, 0x0b
381                                 when '104' # bg bright blue
382                                         @set_attribute_bits 0xff, 0x0c
383                                 when '105' # bg bright magenta
384                                         @set_attribute_bits 0xff, 0x0d
385                                 when '106' # bg bright cyan
386                                         @set_attribute_bits 0xff, 0x0e
387                                 when '107' # bg bright white
388                                         @set_attribute_bits 0xff, 0x0f
389
390                                 else
391                                         # if we don't recognize the style, go back to default
392                                         console.log "unrecognized csi_m arg: \"#{arg}\""
393                                         @a = 0
394                 return
395
396         # str is the whole escape sequence (minus the esc[ prefix)
397         update_sequence: (str) ->
398                 prefix = 'csi_'
399                 if str[0] is '?'
400                         prefix = 'csiq_'
401                         str = str.substr 1
402                 command = @[prefix + str.substr(str.length - 1)]
403                 if not command?
404                         console.log "Unrecognized sequence: ESC[#{str}"
405                         return
406                 args = str.substr(0, str.length - 1).split ';'
407                 command.call this, args...
408
409         update_sequence_then_text: (str) ->
410                 len = @escape_sequence_length str
411                 if len is -1
412                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
413                         @update_text "ESC[" + str
414                 else
415                         @update_sequence str.substr 0, len
416                         @update_text str.substr len
417
418         escape_sequence_length: (str) ->
419                 parts = str.match(/^[0-9;?]{0,25}./)
420                 return -1 unless parts?
421                 return parts[0].length
422
423 my_exports.new = (width, height) ->
424         return new Terminal width, height