JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add csi_L (scroll backwards)
[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                 # move (newly cleared) top line to the bottom of the scrolling region
66                 @text = [
67                         @text[0...@scroll_top]..., # up to but not including scroll top
68                         @text[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
69                         @text[@scroll_top], # top line of scroll region (already cleared)
70                         @text[@scroll_bottom + 1 ... @height]... # rest of screen
71                 ]
72                 @attributes = [
73                         @attributes[0...@scroll_top]..., # up to but not including scroll top
74                         @attributes[@scroll_top + 1 .. @scroll_bottom]..., # scroll region except top line of it
75                         @attributes[@scroll_top], # top line of scroll region (already cleared)
76                         @attributes[@scroll_bottom + 1 ... @height]... # rest of screen
77                 ]
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                                 console.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                                 console.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                 # move (newly cleared) top line to the bottom of the scrolling region
241                 @text = [
242                         @text[0...@y]..., # keep everything above cursor
243                         @text[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
244                         @text[@y..@scroll_bottom - lines]..., # lines that are moving down
245                         @text[@scroll_bottom + 1 ... @height]... # rest of screen
246                 ]
247                 @attributes = [
248                         @attributes[0...@y]..., # keep everything above cursor
249                         @attributes[@scroll_bottom - lines + 1 .. @scroll_bottom]..., # we'll clear these shortly
250                         @attributes[@y..@scroll_bottom - lines]..., # lines that are moving down
251                         @attributes[@scroll_bottom + 1 ... @height]... # rest of screen
252                 ]
253
254                 # clear the lines we scrolled off (and put back in as "new")
255                 for y in [@y...@y+lines]
256                         for x in [0...@width]
257                                 @text[y][x] = ' '
258                                 @attributes[y][x] = 0x07
259
260         # misc
261         csiq_h: ->
262                 args = []
263                 for i in arguments
264                         arg = @fix_esc_arg i, ''
265                         switch arg
266                                 when '25'
267                                         @cursor_visible = true
268                                 when '1049'
269                                         if @saved_normal_screen?
270                                                 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
271                                                 return
272                                         @saved_normal_screen = [@x, @y, @text, @attributes]
273                                         @text = []
274                                         @attributes = []
275                                         for y in [0...@height]
276                                                 @text[y] = []
277                                                 @attributes[y] = []
278                                                 for x in [0...@width]
279                                                         @text[y].push ' '
280                                                         @attributes[y].push 0x07
281                                 else
282                                         console.log "confusing arg for csiq_h: #{arg}"
283                 return
284
285         # unmisc
286         csiq_l: ->
287                 args = []
288                 for i in arguments
289                         arg = @fix_esc_arg i, ''
290                         switch arg
291                                 when '25'
292                                         @cursor_visible = false
293                                 when '1049'
294                                         if not @saved_normal_screen?
295                                                 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
296                                                 return
297                                         @x = @saved_normal_screen[0]
298                                         @y = @saved_normal_screen[1]
299                                         @text = @saved_normal_screen[2]
300                                         @attributes = @saved_normal_screen[3]
301                                         @saved_normal_screen = null
302                                 else
303                                         console.log "confusing arg for csiq_l: #{arg}"
304                 return
305
306         # set color, bold, underline, etc
307         csi_m: ->
308                 args = []
309                 for i in arguments
310                         args.push @fix_esc_arg i, '0'
311
312                 while args.length > 0
313                         arg = args.shift()
314                         switch arg
315                                 # remove all style/color
316                                 when '0'
317                                         @a = 0x07
318
319                                 # style attributes
320                                 when '1' # bold
321                                         @set_attribute_bits 0x010000, 0x010000
322                                 when '3' # italic (rare)
323                                         @set_attribute_bits 0x200000, 0x200000
324                                 when '4' # underline
325                                         @set_attribute_bits 0x020000, 0x020000
326                                 when '5' # blink
327                                         @set_attribute_bits 0x040000, 0x040000
328                                 when '7' # inverse
329                                         @set_attribute_bits 0x080000, 0x080000
330                                 when '8' # invisible. urivt ignores this
331                                         @set_attribute_bits 0x100000, 0x100000
332
333                                 # disable style attributes
334                                 when '21' # not bold (rare)
335                                         @set_attribute_bits 0x010000, 0
336                                 when '22' # not bold
337                                         @set_attribute_bits 0x010000, 0
338                                 when '23' # not italic (rare)
339                                         @set_attribute_bits 0x200000, 0
340                                 when '24' # not underline
341                                         @set_attribute_bits 0x020000, 0
342                                 when '25' # not blink
343                                         @set_attribute_bits 0x040000, 0
344                                 when '27' # not inverse
345                                         @set_attribute_bits 0x080000, 0
346                                 when '28' # not invisible
347                                         @set_attribute_bits 0x100000, 0
348
349                                 when '100' # reset colors but not other attributes
350                                         @set_attribute_bits 0xffff, 0x0007
351
352                                 # 8 fg colors
353                                 when '30' # fg black
354                                         @set_attribute_bits 0xff, 0x00
355                                 when '31' # fg red
356                                         @set_attribute_bits 0xff, 0x01
357                                 when '32' # fg green
358                                         @set_attribute_bits 0xff, 0x02
359                                 when '33' # fg yellow
360                                         @set_attribute_bits 0xff, 0x03
361                                 when '34' # fg blue
362                                         @set_attribute_bits 0xff, 0x04
363                                 when '35' # fg magenta
364                                         @set_attribute_bits 0xff, 0x05
365                                 when '36' # fg cyan
366                                         @set_attribute_bits 0xff, 0x06
367                                 when '37', '39' # fg white  (39 is default)
368                                         @set_attribute_bits 0xff, 0x07
369
370                                 when '38'
371                                         if args.length >= 2 and args[0] is '5'
372                                                 args.shift()
373                                                 @set_attribute_bits 0xff, (0xff & args.shift())
374                                         else
375                                                 @set_attribute_bits 0x20000, 0x20000
376
377                                 # 8 bg colors
378                                 when '40' # bg black
379                                         @set_attribute_bits 0xff00, 0x0000
380                                 when '41' # bg red
381                                         @set_attribute_bits 0xff00, 0x0100
382                                 when '42' # bg green
383                                         @set_attribute_bits 0xff00, 0x0200
384                                 when '43' # bg yellow
385                                         @set_attribute_bits 0xff00, 0x0300
386                                 when '44' # bg blue
387                                         @set_attribute_bits 0xff00, 0x0400
388                                 when '45' # bg magenta
389                                         @set_attribute_bits 0xff00, 0x0500
390                                 when '46' # bg cyan
391                                         @set_attribute_bits 0xff00, 0x0600
392                                 when '47' # bg white
393                                         @set_attribute_bits 0xff00, 0x0700
394                                 when '49' # bg default
395                                         @set_attribute_bits 0xff00, 0x0000
396
397                                 when '48'
398                                         if args.length >= 2 and args[0] is '5'
399                                                 args.shift()
400                                                 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
401                                         else
402                                                 @set_attribute_bits 0x20000, 0x20000
403
404                                 # bright fg colors
405                                 when '90' # fg bright black
406                                         @set_attribute_bits 0xff, 0x08
407                                 when '91' # fg bright red
408                                         @set_attribute_bits 0xff, 0x09
409                                 when '92' # fg bright green
410                                         @set_attribute_bits 0xff, 0x0a
411                                 when '93' # fg bright yellow
412                                         @set_attribute_bits 0xff, 0x0b
413                                 when '94' # fg bright blue
414                                         @set_attribute_bits 0xff, 0x0c
415                                 when '95' # fg bright magenta
416                                         @set_attribute_bits 0xff, 0x0d
417                                 when '96' # fg bright cyan
418                                         @set_attribute_bits 0xff, 0x0e
419                                 when '97' # fg bright white
420                                         @set_attribute_bits 0xff, 0x0f
421
422                                 # bright bg colors
423                                 when '100' # bg bright black
424                                         @set_attribute_bits 0xff, 0x08
425                                 when '101' # bg bright red
426                                         @set_attribute_bits 0xff, 0x09
427                                 when '102' # bg bright green
428                                         @set_attribute_bits 0xff, 0x0a
429                                 when '103' # bg bright yellow
430                                         @set_attribute_bits 0xff, 0x0b
431                                 when '104' # bg bright blue
432                                         @set_attribute_bits 0xff, 0x0c
433                                 when '105' # bg bright magenta
434                                         @set_attribute_bits 0xff, 0x0d
435                                 when '106' # bg bright cyan
436                                         @set_attribute_bits 0xff, 0x0e
437                                 when '107' # bg bright white
438                                         @set_attribute_bits 0xff, 0x0f
439
440                                 else
441                                         # if we don't recognize the style, go back to default
442                                         console.log "unrecognized csi_m arg: \"#{arg}\""
443                                         @a = 0
444                 return
445
446         # set scrolling region
447         csi_r: (top, bottom) ->
448                 top = -1 + parseInt @fix_esc_arg top, '1'
449                 bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
450                 if top < 0
451                         top = 0
452                 if bottom >= @height
453                         bottom = @height - 1
454                 @scroll_top = top
455                 @scroll_bottom = bottom
456                 return
457
458         # str is the whole escape sequence (minus the esc[ prefix)
459         update_sequence: (str) ->
460                 prefix = 'csi_'
461                 if str[0] is '?'
462                         prefix = 'csiq_'
463                         str = str.substr 1
464                 command = @[prefix + str.substr(str.length - 1)]
465                 if not command?
466                         console.log "Unrecognized sequence: ESC[#{str}"
467                         return
468                 args = str.substr(0, str.length - 1).split ';'
469                 command.call this, args...
470
471         update_sequence_then_text: (str) ->
472                 len = @escape_sequence_length str
473                 if len is -1
474                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
475                         @update_text "ESC[" + str
476                 else
477                         @update_sequence str.substr 0, len
478                         @update_text str.substr len
479
480         escape_sequence_length: (str) ->
481                 parts = str.match(/^[0-9;?]{0,25}./)
482                 return -1 unless parts?
483                 return parts[0].length
484
485 my_exports.new = (width, height) ->
486         return new Terminal width, height