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