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