JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
stop pretending what fg white is 0
[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                 @resize width, height
22         
23         resize: (width, height) ->
24                 # FIXME: write a version that retains some of the data
25                 @width = width
26                 @height = height
27                 @text = []
28                 @attributes = []
29                 for y in [0...height]
30                         @text[y] = []
31                         @attributes[y] = []
32                         for x in [0...width]
33                                 @text[y].push ' '
34                                 @attributes[y].push 0
35
36         # pass data from stdout
37         update: (data) ->
38                 return unless data?.length > 0
39                 if @partial.length > 0
40                         data = @partial + data
41                         @partial = ''
42                 parts = data.split(/\x1b\[/)
43                 if parts.length > 1
44                         if -1 is @escape_sequence_length parts[parts.length - 1]
45                                 @partial = parts.pop()
46                 if parts.length > 0
47                         for i in [0...parts.length]
48                                 if i is 0
49                                         @update_text parts[i]
50                                 else
51                                         @update_sequence_then_text parts[i]
52                 return
53         
54         clear_rest_of_line: ->
55                 for i in [@x...@width]
56                         @text[@y][i] = ' '
57                         @attributes[@y][i] = @a
58         
59         add_new_line: ->
60                 # clear top line
61                 for i in [0...@width]
62                         @text[0][i] = ' '
63                         @attributes[0][i] = 0
64                 # move (newly cleared) top line to the bottom
65                 tmp = @text.shift()
66                 @text.push(tmp)
67                 tmp = @attributes.shift()
68                 @attributes.push(tmp)
69                 # slide cursor up with rest of text
70                 @y -= 1
71         
72         wrap_to_next_line: ->
73                 if @y is @height - 1
74                         @add_new_line()
75                 @y += 1
76                 @x = 0
77
78         # str has no escape sequences
79         update_text: (str) ->
80                 return unless str.length > 0
81                 for c in str
82                         switch c
83                                 when '\t' # tab
84                                         @update_text "        ".substr(@x % 8)
85                                 when '\x07' # bell
86                                         false
87                                 when '\x0d' # cr
88                                         @x = 0
89                                 when '\x08' # backspace
90                                         if @x > 0
91                                                 @x -= 1
92                                                 @text[@y][@x] = ' '
93                                                 # should this set the attribute too?
94                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
95                                         @wrap_to_next_line()
96                                 else
97                                         @text[@y][@x] = c
98                                         @attributes[@y][@x] = @a
99                                         @x += 1
100                                         if @x is @width
101                                                 @wrap_to_next_line()
102                 return
103         
104         set_attribute_bits: (mask, value) ->
105                 @a = (@a & ~mask) | value
106
107         csi_m: default: "0", go: ->
108                 for i in arguments
109                         fixed = i
110                         while fixed[0] is '0'
111                                 fixed = fixed.substr 1
112                         switch fixed
113
114                                 # remove all style/color
115                                 when '' # leading zeros are removed (even if that's all of them)
116                                         @set_attribute_bits 0xffffff, 0x000007
117
118                                 # style attributes
119                                 when '1' # bold
120                                         @set_attribute_bits 0x10000, 0x10000
121                                 when '4' # underline
122                                         @set_attribute_bits 0x20000, 0x20000
123                                 when '5' # blink
124                                         @set_attribute_bits 0x40000, 0x40000
125                                 when '8' # invisible
126                                         @set_attribute_bits 0x80000, 0x80000
127                                 
128                                 # disable style attributes
129                                 when '22' # not bold... according to a page
130                                         @set_attribute_bits 0x10000, 0
131                                 when '21' # ... though this would make more sense for "not bold"
132                                         @set_attribute_bits 0x10000, 0
133                                 when '24' # not underline
134                                         @set_attribute_bits 0x20000, 0
135                                 when '25' # not blink
136                                         @set_attribute_bits 0x40000, 0
137                                 when '28' # not invisible
138                                         @set_attribute_bits 0x80000, 0
139
140                                 when '100' # reset colors but not other attributes
141                                         @set_attribute_bits 0xffff, 0x0007
142
143                                 # 8 fg colors
144                                 when '30' # fg black
145                                         @set_attribute_bits 0xff, 0x00
146                                 when '31' # fg red
147                                         @set_attribute_bits 0xff, 0x01
148                                 when '32' # fg green
149                                         @set_attribute_bits 0xff, 0x02
150                                 when '33' # fg yellow
151                                         @set_attribute_bits 0xff, 0x03
152                                 when '34' # fg blue
153                                         @set_attribute_bits 0xff, 0x04
154                                 when '35' # fg magenta
155                                         @set_attribute_bits 0xff, 0x05
156                                 when '36' # fg cyan
157                                         @set_attribute_bits 0xff, 0x06
158                                 when '37', '39' # fg white  (39 is default)
159                                         @set_attribute_bits 0xff, 0x07
160
161                                 # 8 bg colors
162                                 when '40' # bg black
163                                         @set_attribute_bits 0xff00, 0x0000
164                                 when '41' # bg red
165                                         @set_attribute_bits 0xff00, 0x0100
166                                 when '42' # bg green
167                                         @set_attribute_bits 0xff00, 0x0200
168                                 when '43' # bg yellow
169                                         @set_attribute_bits 0xff00, 0x0300
170                                 when '44' # bg blue
171                                         @set_attribute_bits 0xff00, 0x0400
172                                 when '45' # bg magenta
173                                         @set_attribute_bits 0xff00, 0x0500
174                                 when '46' # bg cyan
175                                         @set_attribute_bits 0xff00, 0x0600
176                                 when '47' # bg white
177                                         @set_attribute_bits 0xff00, 0x0700
178                                 when '49' # bg default
179                                         @set_attribute_bits 0xff00, 0x0000
180
181                                 # bright fg colors
182                                 when '90' # fg bright black
183                                         @set_attribute_bits 0xff, 0x08
184                                 when '91' # fg bright red
185                                         @set_attribute_bits 0xff, 0x09
186                                 when '92' # fg bright green
187                                         @set_attribute_bits 0xff, 0x0a
188                                 when '93' # fg bright yellow
189                                         @set_attribute_bits 0xff, 0x0b
190                                 when '94' # fg bright blue
191                                         @set_attribute_bits 0xff, 0x0c
192                                 when '95' # fg bright magenta
193                                         @set_attribute_bits 0xff, 0x0d
194                                 when '96' # fg bright cyan
195                                         @set_attribute_bits 0xff, 0x0e
196                                 when '97' # fg bright white
197                                         @set_attribute_bits 0xff, 0x0f
198
199                                 # bright bg colors
200                                 when '100' # bg bright black
201                                         @set_attribute_bits 0xff, 0x08
202                                 when '101' # bg bright red
203                                         @set_attribute_bits 0xff, 0x09
204                                 when '102' # bg bright green
205                                         @set_attribute_bits 0xff, 0x0a
206                                 when '103' # bg bright yellow
207                                         @set_attribute_bits 0xff, 0x0b
208                                 when '104' # bg bright blue
209                                         @set_attribute_bits 0xff, 0x0c
210                                 when '105' # bg bright magenta
211                                         @set_attribute_bits 0xff, 0x0d
212                                 when '106' # bg bright cyan
213                                         @set_attribute_bits 0xff, 0x0e
214                                 when '107' # bg bright white
215                                         @set_attribute_bits 0xff, 0x0f
216
217
218                                 else
219                                         # if we don't recognize the style, go back to default
220                                         @set_attribute_bits 0xffffff, 0
221                 return
222
223         # str is the whole escape sequence (minus the esc[ prefix)
224         update_sequence: (str) ->
225                 command = @["csi_#{str.substr str.length - 1}"]
226                 return unless command?
227                 args = str.substr(0, str.length - 1).split ';'
228                 for i in [0...args.length]
229                         if args[i] is ''
230                                 args[i] = command.default
231                 command.go.call this, args...
232
233         update_sequence_then_text: (str) ->
234                 len = @escape_sequence_length str
235                 if len is -1
236                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
237                         @update_text "ESC[" + str
238                 else
239                         @update_sequence str.substr 0, len
240                         @update_text str.substr len
241
242         escape_sequence_length: (str) ->
243                 parts = str.match(/^[0-9;?]{0,25}./)
244                 return -1 unless parts?
245                 return parts[0].length
246
247 my_exports.new = (width, height) ->
248         return new Terminal width, height