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