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