JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix bright bg, add ansi chars, more esc seqs
[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 alternate_charset_table = {
16         A: "↑"
17         B: "↓"
18         C: "→"
19         D: "←"
20         E: "█"
21         F: "▚"
22         G: "☃"
23         '_': " "
24         '`': "◆"
25         a: "▒"
26         b: "␉"
27         c: "␌"
28         d: "␍"
29         e: "␊"
30         f: "°"
31         g: "±"
32         h: "␤"
33         i: "␋"
34         j: "┘"
35         k: "┐"
36         l: "┌"
37         m: "└"
38         n: "┼"
39         o: "⎺"
40         p: "⎻"
41         q: "─"
42         r: "⎼"
43         s: "⎽"
44         t: "├"
45         u: "┤"
46         v: "┴"
47         w: "┬"
48         x: "│"
49         y: "≤"
50         z: "≥"
51         '{': "π"
52         '|': "≠"
53         '}': "£"
54         '~': "·"
55 }
56
57 class Terminal
58         # public:
59         constructor: (width, height) ->
60                 @width = 1
61                 @height = 1
62                 @text = []
63                 @attributes = []
64                 @x = 0
65                 @y = 0
66                 @a = 0x000007 # cursor attributes
67                 @partial = ''
68                 @saved_normal_screen = null
69                 @cursor_visible = true
70                 @scroll_top = 0
71                 @scroll_bottom = height - 1
72                 @resize width, height
73
74         resize: (width, height) ->
75                 # FIXME: write a version that retains some of the data
76                 # FIXME: clamp variables (eg x, y, saved.*, scrolling region) if getting smaller
77                 @width = width
78                 @height = height
79                 @text = []
80                 @attributes = []
81                 for y in [0...height]
82                         @text[y] = []
83                         @attributes[y] = []
84                         for x in [0...width]
85                                 @text[y].push ' '
86                                 @attributes[y].push 0x07
87
88         # pass data from stdout
89         update: (data) ->
90                 return unless data?.length > 0
91                 if @partial.length > 0
92                         data = @partial + data
93                         @partial = ''
94                 parts = data.split(/\x1b/)
95                 if parts.length > 1
96                         if -1 is @escape_sequence_length parts[parts.length - 1]
97                                 @partial = parts.pop()
98                 for i in [0...parts.length]
99                         if i is 0
100                                 @update_text parts[i]
101                         else
102                                 @update_sequence_then_text parts[i]
103                 return
104
105         add_new_line: ->
106                 # clear the line at the top of the scrolling region
107                 for i in [0...@width]
108                         @text[@scroll_top][i] = ' '
109                         @attributes[@scroll_top][i] = 0x07
110
111                 rearrange = (a) =>
112                         return [
113                                 a[0...@scroll_top]..., # up to but not including scroll top
114                                 a[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
115                                 a[@scroll_top], # top line of scroll region (already cleared)
116                                 a[@scroll_bottom + 1 ... @height]... # rest of screen
117                         ]
118                 @text = rearrange @text
119                 @attributes = rearrange @attributes
120
121                 # slide cursor up with rest of text
122                 @y -= 1
123
124         wrap_to_next_line: ->
125                 if @y is @scroll_bottom
126                         @add_new_line()
127                 @y += 1
128                 @x = 0
129
130         # str has no escape sequences
131         update_text: (str) ->
132                 return unless str.length > 0
133                 for c in str
134                         switch c
135                                 when '\t' # tab
136                                         @update_text "        ".substr(@x % 8)
137                                 when '\x07' # bell
138                                         false
139                                 when '\x0d' # cr
140                                         @x = 0
141                                 when '\x08' # backspace
142                                         if @x is 0
143                                                 @x = @width - 1
144                                                 if @y > 0
145                                                         @y -= 1
146                                         else
147                                                 @x -= 1
148                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
149                                         @wrap_to_next_line()
150                                 when '\x0f'
151                                         @alternate_charset = true
152                                 when '\x0e'
153                                         @alternate_charset = false
154                                 else
155                                         if @x >= @width
156                                                 @wrap_to_next_line()
157                                         if @alternate_charset
158                                                 if alternate_charset_table[c]?
159                                                         c = alternate_charset_table[c]
160                                         @text[@y][@x] = c
161                                         @attributes[@y][@x] = @a
162                                         @x += 1
163                 return
164
165         set_attribute_bits: (mask, value) ->
166                 @a = ((@a & ~mask) | value)
167
168         # we're supposed to ignore leeding zeros, and while we're at it, lets swap
169         # in the default for blank or missing values
170         fix_esc_arg: (value, deef_alt) ->
171                 if value? and value != ''
172                         while value[0] is '0' and value.length > 1
173                                 value = value.substr 1
174                         return value
175                 else
176                         return deef_alt
177
178         # csi_@: rxvt does nothing I can detect
179
180         # "The auxiliary keypad keys will send ASCII codes corresponding to the
181         # characters engraved on their keys." (numlock?)
182         "esc_>": ->
183
184         # move cursor up
185         csi_A: (lines) ->
186                 lines = parseInt @fix_esc_arg lines, '1'
187                 @y -= lines
188                 if @y < 0
189                         @y = 0
190                 return
191
192         # move cursor down
193         csi_B: (lines) ->
194                 lines = parseInt @fix_esc_arg lines, '1'
195                 @y += lines
196                 if @y >= @height
197                         @y = @height - 1
198                 return
199
200         # move cursor right
201         csi_C: (cols) ->
202                 cols = parseInt @fix_esc_arg cols, '1'
203                 @x += cols
204                 if @x >= @width
205                         @x = @width - 1
206                 return
207
208         # move cursor left
209         csi_D: (cols) ->
210                 cols = parseInt @fix_esc_arg cols, '1'
211                 @x -= cols
212                 if @x < 0
213                         @x = 0
214                 return
215
216         # cursor set_row (y)
217         csi_d: (row) ->
218                 row = parseInt @fix_esc_arg row, '1'
219                 @set_row_clamped row
220                 return
221
222         # cursor column (x)
223         csi_G: (col) ->
224                 row = parseInt @fix_esc_arg col, '1'
225                 @set_row_clamped col
226                 return
227
228         # set cursor row (zero based)
229         set_row_clamped: (row) ->
230                 if row < 0
231                         row = 0
232                 if row >= @height
233                         row = @height - 1
234                 @y = row
235                 return
236         # set cursor column (zero based)
237         set_column_clamped: (column) ->
238                 if column < 0
239                         column = 0
240                 if column >= @width
241                         column = @width - 1
242                 @x = column
243                 return
244         # set cursor position (one based)
245         csi_H: (row, column) ->
246                 # handle blank/missing args and convert to 0 base
247                 row = -1 + parseInt @fix_esc_arg row, '1'
248                 column = -1 + parseInt @fix_esc_arg column, '1'
249                 @set_row_clamped row
250                 @set_column_clamped column
251                 return
252
253         # clear to screen edge(es)
254         csi_J: (direction) ->
255                 switch @fix_esc_arg direction, '0'
256                         when '0' # erase down
257                                 # rest of current line
258                                 @csi_K direction
259                                 # rest of lines
260                                 for row in [@y...@height]
261                                         for i in [0...@width]
262                                                 @text[row][i] = ' '
263                                                 @attributes[row][i] = @a
264                         when '1' # erase up
265                                 # beginning of current line
266                                 @csi_K direction
267                                 # all previous lines
268                                 for row in [0..@y]
269                                         for i in [0...@width]
270                                                 @text[row][i] = ' '
271                                                 @attributes[row][i] = @a
272                         when '2' # erase everything
273                                 for row in [0...@height]
274                                         for i in [0...@width]
275                                                 @text[row][i] = ' '
276                                                 @attributes[row][i] = @a
277                         else
278                                 log "confusing arg for csi_J: #{direction}"
279                 return
280
281         # clear (some or all of) current line
282         csi_K: (direction) ->
283                 switch @fix_esc_arg direction, '0'
284                         when '0' # erase to right
285                                 for i in [@x...@width]
286                                         @text[@y][i] = ' '
287                                         @attributes[@y][i] = @a
288                         when '1' # erase to left
289                                 # @x can equal @width (after printing to right-most column)
290                                 if @x < @width
291                                         max = @x
292                                 else
293                                         max = @width - 1
294                                 for i in [0..max]
295                                         @text[@y][i] = ' '
296                                         @attributes[@y][i] = @a
297                         when '2' # erase whole line
298                                 for i in [0...@width]
299                                         @text[@y][i] = ' '
300                                         @attributes[@y][i] = @a
301                         else
302                                 log "confusing arg for csi_K: #{direction}"
303                 return
304
305         # move lines downwards (arg is how far)
306         csi_L: (lines) ->
307                 lines = parseInt @fix_esc_arg lines, '1'
308
309                 rearrange = (a) =>
310                         return [
311                                 a[0...@y]..., # keep everything above cursor
312                                 a[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
313                                 a[@y..@scroll_bottom - lines]..., # lines that are moving down
314                                 a[@scroll_bottom + 1 ... @height]... # rest of screen
315                         ]
316                 @text = rearrange @text
317                 @attributes = rearrange @attributes
318
319                 # clear the lines we scrolled off (and put back in as "new")
320                 for y in [@y...@y+lines]
321                         for x in [0...@width]
322                                 @text[y][x] = ' '
323                                 @attributes[y][x] = 0x07
324
325         # move lines upwards (arg is how far)
326         # this obliterates the line under the cursor and arg-1 following it
327         csi_M: (lines) ->
328                 lines = parseInt @fix_esc_arg lines, '1'
329
330                 rearrange = (a) =>
331                         return [
332                                 a[0 ... @y]..., # keep everything above cursor
333                                 a[@y + lines .. @scroll_bottom]..., # lines we're moving up
334                                 a[@y ... @y + lines]..., # recycle these
335                                 a[@scroll_bottom + 1 ... @height]... # keep the rest
336                         ]
337                 @text = rearrange @text
338                 @attributes = rearrange @attributes
339
340                 # clear the lines we're recycling
341                 for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
342                         for x in [0...@width]
343                                 @text[y][x] = ' '
344                                 @attributes[y][x] = 0x07
345
346         # delete chars (I think this shouldn't move the cursor)
347         csi_X: (chars) ->
348                 chars = parseInt @fix_esc_arg chars, '1'
349                 x = @x
350                 for c in [0...chars]
351                         if x >= @width
352                                 return
353                         @text[@y][x] = ' '
354                         @attributes[@y][x] = @a
355                         x += 1
356                 return
357
358         # set modes starting with "?"
359         csiq_h: ->
360                 args = []
361                 for i in arguments
362                         arg = @fix_esc_arg i, ''
363                         switch arg
364                                 when '1' # mode ?1
365                                         # numlock on
366                                         return
367                                 when '25'
368                                         @cursor_visible = true
369                                 when '1000'
370                                         # x11 normal mouse tracking
371                                         return
372                                 when '1049'
373                                         if @saved_normal_screen?
374                                                 log "ignoring request to switch to the alt screen because we're already on the alt screen"
375                                                 return
376                                         @saved_normal_screen = x: @x, y: @y, text: @text, attributes: @attributes
377                                         @text = []
378                                         @attributes = []
379                                         for y in [0...@height]
380                                                 @text[y] = []
381                                                 @attributes[y] = []
382                                                 for x in [0...@width]
383                                                         @text[y].push ' '
384                                                         @attributes[y].push 0x07
385                                 else
386                                         log "Unimplemented arg for csiq_h: #{arg}"
387                 return
388
389         # reset (turn off) modes
390         # note that modes starting with ? call csiq_l, not this
391         csi_l: ->
392                 for i in arguments
393                         arg = @fix_esc_arg i, ''
394                         switch arg
395                                 when '0'
396                                         # error (ignored
397                                         return
398                                 when '2'
399                                         # KAM -- keyboard action
400                                         return
401                                 when '4'
402                                         # put cursor in "REPLACE" mode
403                                         return
404                                 when '12'
405                                         # turn local echo on (or off?)
406                                         return
407                                 # TODO when '20' LNM linefeed/newline
408
409         # reset (turn off) modes starting with ?
410         # in st source, these are priv=true
411         csiq_l: ->
412                 args = []
413                 for i in arguments
414                         arg = @fix_esc_arg i, ''
415                         switch arg
416                                 when '1' # mode ?1 reset
417                                         # numlock off
418                                         return
419                                 when '12' # mode ?12
420                                         # stop blinking the cursor (ignored)
421                                         return
422                                 when '25' # mode ?25 reset
423                                         @cursor_visible = false
424                                 when '0', '2', '3', '4', '8', '18', '19', '42', '12'
425                                         # st ignores all of these
426                                         return
427                                 when '9', '1000', '1002', '1003', '1004', '1006'
428                                         # mouse reporting and such (ignored)
429                                         return
430                                 when '1049'
431                                         if not @saved_normal_screen?
432                                                 log "ignoring request to switch to the normal screen because we're already on the normal screen"
433                                                 return
434                                         @x = @saved_normal_screen.x
435                                         @y = @saved_normal_screen.y
436                                         @text = @saved_normal_screen.text
437                                         @attributes = @saved_normal_screen.attributes
438                                         @saved_normal_screen = null
439                                 else
440                                         log "Unimplemented arg for csiq_l: #{arg}"
441                 return
442
443         # set color, bold, underline, etc
444         csi_m: ->
445                 args = []
446                 for i in arguments
447                         args.push @fix_esc_arg i, '0'
448
449                 while args.length > 0
450                         arg = args.shift()
451                         switch arg
452                                 # remove all style/color
453                                 when '0'
454                                         @a = 0x07
455
456                                 # style attributes
457                                 when '1' # bold
458                                         @set_attribute_bits 0x010000, 0x010000
459                                 when '3' # italic (rare)
460                                         @set_attribute_bits 0x200000, 0x200000
461                                 when '4' # underline
462                                         @set_attribute_bits 0x020000, 0x020000
463                                 when '5' # blink
464                                         @set_attribute_bits 0x040000, 0x040000
465                                 when '7' # inverse
466                                         @set_attribute_bits 0x080000, 0x080000
467                                 when '8' # invisible. urivt ignores this
468                                         @set_attribute_bits 0x100000, 0x100000
469
470                                 # disable style attributes
471                                 when '21' # not bold (rare)
472                                         @set_attribute_bits 0x010000, 0
473                                 when '22' # not bold
474                                         @set_attribute_bits 0x010000, 0
475                                 when '23' # not italic (rare)
476                                         @set_attribute_bits 0x200000, 0
477                                 when '24' # not underline
478                                         @set_attribute_bits 0x020000, 0
479                                 when '25' # not blink
480                                         @set_attribute_bits 0x040000, 0
481                                 when '27' # not inverse
482                                         @set_attribute_bits 0x080000, 0
483                                 when '28' # not invisible
484                                         @set_attribute_bits 0x100000, 0
485
486                                 when '100' # reset colors but not other attributes
487                                         @set_attribute_bits 0xffff, 0x0007
488
489                                 # 8 fg colors
490                                 when '30' # fg black
491                                         @set_attribute_bits 0xff, 0x00
492                                 when '31' # fg red
493                                         @set_attribute_bits 0xff, 0x01
494                                 when '32' # fg green
495                                         @set_attribute_bits 0xff, 0x02
496                                 when '33' # fg yellow
497                                         @set_attribute_bits 0xff, 0x03
498                                 when '34' # fg blue
499                                         @set_attribute_bits 0xff, 0x04
500                                 when '35' # fg magenta
501                                         @set_attribute_bits 0xff, 0x05
502                                 when '36' # fg cyan
503                                         @set_attribute_bits 0xff, 0x06
504                                 when '37', '39' # fg white  (39 is default)
505                                         @set_attribute_bits 0xff, 0x07
506
507                                 when '38'
508                                         if args.length >= 2 and args[0] is '5'
509                                                 args.shift()
510                                                 @set_attribute_bits 0xff, (0xff & args.shift())
511                                         else
512                                                 @set_attribute_bits 0x20000, 0x20000
513
514                                 # 8 bg colors
515                                 when '40' # bg black
516                                         @set_attribute_bits 0xff00, 0x0000
517                                 when '41' # bg red
518                                         @set_attribute_bits 0xff00, 0x0100
519                                 when '42' # bg green
520                                         @set_attribute_bits 0xff00, 0x0200
521                                 when '43' # bg yellow
522                                         @set_attribute_bits 0xff00, 0x0300
523                                 when '44' # bg blue
524                                         @set_attribute_bits 0xff00, 0x0400
525                                 when '45' # bg magenta
526                                         @set_attribute_bits 0xff00, 0x0500
527                                 when '46' # bg cyan
528                                         @set_attribute_bits 0xff00, 0x0600
529                                 when '47' # bg white
530                                         @set_attribute_bits 0xff00, 0x0700
531                                 when '49' # bg default
532                                         @set_attribute_bits 0xff00, 0x0000
533
534                                 when '48'
535                                         if args.length >= 2 and args[0] is '5'
536                                                 args.shift()
537                                                 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
538                                         else
539                                                 @set_attribute_bits 0x20000, 0x20000
540
541                                 # bright fg colors
542                                 when '90' # fg bright black
543                                         @set_attribute_bits 0xff, 0x08
544                                 when '91' # fg bright red
545                                         @set_attribute_bits 0xff, 0x09
546                                 when '92' # fg bright green
547                                         @set_attribute_bits 0xff, 0x0a
548                                 when '93' # fg bright yellow
549                                         @set_attribute_bits 0xff, 0x0b
550                                 when '94' # fg bright blue
551                                         @set_attribute_bits 0xff, 0x0c
552                                 when '95' # fg bright magenta
553                                         @set_attribute_bits 0xff, 0x0d
554                                 when '96' # fg bright cyan
555                                         @set_attribute_bits 0xff, 0x0e
556                                 when '97' # fg bright white
557                                         @set_attribute_bits 0xff, 0x0f
558
559                                 # bright bg colors
560                                 when '100' # bg bright black
561                                         @set_attribute_bits 0xff00, 0x0800
562                                 when '101' # bg bright red
563                                         @set_attribute_bits 0xff00, 0x0900
564                                 when '102' # bg bright green
565                                         @set_attribute_bits 0xff00, 0x0a00
566                                 when '103' # bg bright yellow
567                                         @set_attribute_bits 0xff00, 0x0b00
568                                 when '104' # bg bright blue
569                                         @set_attribute_bits 0xff00, 0x0c00
570                                 when '105' # bg bright magenta
571                                         @set_attribute_bits 0xff00, 0x0d00
572                                 when '106' # bg bright cyan
573                                         @set_attribute_bits 0xff00, 0x0e00
574                                 when '107' # bg bright white
575                                         @set_attribute_bits 0xff00, 0x0f00
576
577                                 else
578                                         # if we don't recognize the style, go back to default
579                                         log "unrecognized csi_m arg: \"#{arg}\""
580                                         @a = 0
581                 return
582
583         # set scroll region
584         csi_r: (top, bottom) ->
585                 top = -1 + parseInt @fix_esc_arg top, '1'
586                 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
587                 if top < 0
588                         top = 0
589                 if bottom >= @height
590                         bottom = @height - 1
591                 @scroll_top = top
592                 @scroll_bottom = bottom
593                 return
594
595         # move cursor up one line, if it's at the top, scroll everything down
596         esc_M: ->
597                 if @y > 0
598                         @csi_A '1'
599                 else
600                         @csi_L '1'
601         
602         # esc ]
603         "esc_]": (seq) ->
604                 sem = seq.indexOf ';'
605                 if sem < 1 || seq[0] isnt '0'
606                         log "Unimplemented esc_] (OS command) sequence: #{seq}"
607                         return
608                 if window?
609                         title = seq.substr sem + 1
610                         title = title.substr 0, title.length - 1
611                         window.document.title = title
612
613         # "The auxiliary keypad keys will transmit control sequences."
614         "esc_=": ->
615
616         # esc (
617         "esc_)": ->
618                 @alternate_charset = true
619         # esc ( 0
620         gzd4_0: ->
621                 @alternate_charset = true
622         # esc ( B
623         gzd4_B: ->
624                 @alternate_charset = false
625
626         esc_n: ->
627                 @alternate_charset = false
628
629         esc_o: ->
630                 @alternate_charset = true
631
632         # str is the whole escape sequence (minus the esc character)
633         update_sequence: (str) ->
634                 if str[0] is '['
635                         if str[1] is '?'
636                                 method_prefix = 'csiq_'
637                                 args = str.substr(2, str.length - 3).split ';'
638                         else
639                                 method_prefix = 'csi_'
640                                 args = str.substr(1, str.length - 2).split ';'
641                         method_name = method_prefix + str.substr str.length - 1
642                 else if str[0] is '(' # character set
643                         method_name = 'gzd4_' + str.substr 1, 1
644                         args = [str.substr 1]
645                 else
646                         method_name = 'esc_' + str.substr 0, 1
647                         args = [str.substr 1]
648                 method = @[method_name]
649                 if not method?
650                         log "Unimplemented #{method_name}, code: ESC#{str}"
651                         return
652                 method.apply this, args
653
654         update_sequence_then_text: (str) ->
655                 len = @escape_sequence_length str
656                 if len is -1
657                         log "couldn't find escape sequence here: #{str.substr 0, 25}"
658                         @update_text "ESC" + str
659                 else
660                         @update_sequence str.substr 0, len
661                         @update_text str.substr len
662
663         escape_sequence_length: (str) ->
664                 if str[0] is '['
665                         parts = str.match(/^\[[0-9;?]{0,25}./)
666                         return -1 unless parts?
667                         return parts[0].length
668                 else if str[0] is '('
669                         if str.length == 1
670                                 return -1
671                         if str[1] is '!' or str[1] is '%'
672                                 return 3
673                         else
674                                 return 2
675                 else if str[0] is ']'
676                         st = str.indexOf "\u0007"
677                         if st is -1
678                                 return -1
679                         return st + 1
680                 else
681                         #log "non[: ESC#{str.substr 0, 10}"
682                         if str.length >= 1
683                                 return 1
684                         else
685                                 return -1
686
687 my_exports.new = (width, height) ->
688         return new Terminal width, height