JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
751a53accdc55c5960b863221e0b0b94294c5694
[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 > 0
87                                                 @x -= 1
88                                                 @text[@y][@x] = ' '
89                                                 # should this set the attribute too?
90                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
91                                         @wrap_to_next_line()
92                                 else
93                                         @text[@y][@x] = c
94                                         @attributes[@y][@x] = @a
95                                         @x += 1
96                                         if @x is @width
97                                                 @wrap_to_next_line()
98                 return
99
100         set_attribute_bits: (mask, value) ->
101                 @a = (@a & ~mask) | value
102
103         # we're supposed to ignore leeding zeros, and while we're at it, lets swap
104         # in the default for blank or missing values
105         fix_esc_arg: (value, deef_alt) ->
106                 if value? and value != ''
107                         while value[0] is '0' and value.length > 1
108                                 value = value.substr 1
109                         return value
110                 else
111                         return deef_alt
112
113         # set cursor position (one based)
114         csi_H: (row, column) ->
115                 row = 0 + @fix_esc_arg row, 1
116                 column = 0 + @fix_esc_arg column, 1
117
118                 # convert to 0 base
119                 column -= 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
162         # clear (some or all of) current line
163         csi_K: (direction) ->
164                 switch @fix_esc_arg direction, '0'
165                         when '0' # erase to right
166                                 for i in [@x...@width]
167                                         @text[@y][i] = ' '
168                                         @attributes[@y][i] = @a
169                         when '1' # erase to left
170                                 for i in [0..@x]
171                                         @text[@y][i] = ' '
172                                         @attributes[@y][i] = @a
173                         when '2' # erase whole line
174                                 for i in [0...@width]
175                                         @text[@y][i] = ' '
176                                         @attributes[@y][i] = @a
177                         else
178                                 console.log "confusing arg for csi_K: #{direction}"
179         
180         # misc
181         csiq_h: ->
182                 args = []
183                 for i in arguments
184                         switch @fix_esc_arg i, ''
185                                 when '1049'
186                                         if @saved_normal_screen?
187                                                 console.log "ignoring request to switch to the alt screen because we're already on the alt screen"
188                                                 return
189                                         @saved_normal_screen = [@x, @y, @text, @attributes]
190                                         @text = []
191                                         @attributes = []
192                                         for y in [0...@height]
193                                                 @text[y] = []
194                                                 @attributes[y] = []
195                                                 for x in [0...@width]
196                                                         @text[y].push ' '
197                                                         @attributes[y].push 0
198         # unmisc
199         csiq_l: ->
200                 args = []
201                 for i in arguments
202                         switch @fix_esc_arg i, ''
203                                 when '1049'
204                                         if not @saved_normal_screen?
205                                                 console.log "ignoring request to switch to the normal screen because we're already on the normal screen"
206                                                 return
207                                         @x = @saved_normal_screen[0]
208                                         @y = @saved_normal_screen[1]
209                                         @text = @saved_normal_screen[2]
210                                         @attributes = @saved_normal_screen[3]
211                                         @saved_normal_screen = null
212
213         # set color, bold, underline, etc
214         csi_m: ->
215                 args = []
216                 for i in arguments
217                         args.push @fix_esc_arg i, 0
218
219                 while args.length > 0
220                         switch args.shift()
221
222                                 # remove all style/color
223                                 when '0'
224                                         @set_attribute_bits 0xffffff, 0x000007
225
226                                 # style attributes
227                                 when '1' # bold
228                                         @set_attribute_bits 0x10000, 0x10000
229                                 when '4' # underline
230                                         @set_attribute_bits 0x20000, 0x20000
231                                 when '5' # blink
232                                         @set_attribute_bits 0x40000, 0x40000
233                                 when '8' # invisible
234                                         @set_attribute_bits 0x80000, 0x80000
235
236                                 # disable style attributes
237                                 when '22' # not bold... according to a page
238                                         @set_attribute_bits 0x10000, 0
239                                 when '21' # ... though this would make more sense for "not bold"
240                                         @set_attribute_bits 0x10000, 0
241                                 when '24' # not underline
242                                         @set_attribute_bits 0x20000, 0
243                                 when '25' # not blink
244                                         @set_attribute_bits 0x40000, 0
245                                 when '28' # not invisible
246                                         @set_attribute_bits 0x80000, 0
247
248                                 when '100' # reset colors but not other attributes
249                                         @set_attribute_bits 0xffff, 0x0007
250
251                                 # 8 fg colors
252                                 when '30' # fg black
253                                         @set_attribute_bits 0xff, 0x00
254                                 when '31' # fg red
255                                         @set_attribute_bits 0xff, 0x01
256                                 when '32' # fg green
257                                         @set_attribute_bits 0xff, 0x02
258                                 when '33' # fg yellow
259                                         @set_attribute_bits 0xff, 0x03
260                                 when '34' # fg blue
261                                         @set_attribute_bits 0xff, 0x04
262                                 when '35' # fg magenta
263                                         @set_attribute_bits 0xff, 0x05
264                                 when '36' # fg cyan
265                                         @set_attribute_bits 0xff, 0x06
266                                 when '37', '39' # fg white  (39 is default)
267                                         @set_attribute_bits 0xff, 0x07
268
269                                 when '38'
270                                         if args.length >= 2 and args[0] is '5'
271                                                 args.shift()
272                                                 @set_attribute_bits 0xff, (0xff & args.shift())
273                                         else
274                                                 @set_attribute_bits 0x20000, 0x20000
275
276                                 # 8 bg colors
277                                 when '40' # bg black
278                                         @set_attribute_bits 0xff00, 0x0000
279                                 when '41' # bg red
280                                         @set_attribute_bits 0xff00, 0x0100
281                                 when '42' # bg green
282                                         @set_attribute_bits 0xff00, 0x0200
283                                 when '43' # bg yellow
284                                         @set_attribute_bits 0xff00, 0x0300
285                                 when '44' # bg blue
286                                         @set_attribute_bits 0xff00, 0x0400
287                                 when '45' # bg magenta
288                                         @set_attribute_bits 0xff00, 0x0500
289                                 when '46' # bg cyan
290                                         @set_attribute_bits 0xff00, 0x0600
291                                 when '47' # bg white
292                                         @set_attribute_bits 0xff00, 0x0700
293                                 when '49' # bg default
294                                         @set_attribute_bits 0xff00, 0x0000
295
296                                 when '48'
297                                         if args.length >= 2 and args[0] is '5'
298                                                 args.shift()
299                                                 @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
300                                         else
301                                                 @set_attribute_bits 0x20000, 0x20000
302
303                                 # bright fg colors
304                                 when '90' # fg bright black
305                                         @set_attribute_bits 0xff, 0x08
306                                 when '91' # fg bright red
307                                         @set_attribute_bits 0xff, 0x09
308                                 when '92' # fg bright green
309                                         @set_attribute_bits 0xff, 0x0a
310                                 when '93' # fg bright yellow
311                                         @set_attribute_bits 0xff, 0x0b
312                                 when '94' # fg bright blue
313                                         @set_attribute_bits 0xff, 0x0c
314                                 when '95' # fg bright magenta
315                                         @set_attribute_bits 0xff, 0x0d
316                                 when '96' # fg bright cyan
317                                         @set_attribute_bits 0xff, 0x0e
318                                 when '97' # fg bright white
319                                         @set_attribute_bits 0xff, 0x0f
320
321                                 # bright bg colors
322                                 when '100' # bg bright black
323                                         @set_attribute_bits 0xff, 0x08
324                                 when '101' # bg bright red
325                                         @set_attribute_bits 0xff, 0x09
326                                 when '102' # bg bright green
327                                         @set_attribute_bits 0xff, 0x0a
328                                 when '103' # bg bright yellow
329                                         @set_attribute_bits 0xff, 0x0b
330                                 when '104' # bg bright blue
331                                         @set_attribute_bits 0xff, 0x0c
332                                 when '105' # bg bright magenta
333                                         @set_attribute_bits 0xff, 0x0d
334                                 when '106' # bg bright cyan
335                                         @set_attribute_bits 0xff, 0x0e
336                                 when '107' # bg bright white
337                                         @set_attribute_bits 0xff, 0x0f
338
339
340                                 else
341                                         # if we don't recognize the style, go back to default
342                                         @set_attribute_bits 0xffffff, 0
343                 return
344
345         # str is the whole escape sequence (minus the esc[ prefix)
346         update_sequence: (str) ->
347                 prefix = 'csi_'
348                 if str[0] is '?'
349                         prefix = 'csiq_'
350                         str = str.substr 1
351                 command = @[prefix + str.substr(str.length - 1)]
352                 if not command?
353                         console.log "Unrecognized sequence: ESC[#{str}"
354                         return
355                 args = str.substr(0, str.length - 1).split ';'
356                 command.call this, args...
357
358         update_sequence_then_text: (str) ->
359                 len = @escape_sequence_length str
360                 if len is -1
361                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
362                         @update_text "ESC[" + str
363                 else
364                         @update_sequence str.substr 0, len
365                         @update_text str.substr len
366
367         escape_sequence_length: (str) ->
368                 parts = str.match(/^[0-9;?]{0,25}./)
369                 return -1 unless parts?
370                 return parts[0].length
371
372 my_exports.new = (width, height) ->
373         return new Terminal width, height