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