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