JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
plain text flows and wraps properly
[watch-my-terminal.git] / terminal.coffee
1 async = require 'async'
2 fs = require 'fs'
3
4 class Terminal
5         # public:
6         constructor: (width, height) ->
7                 @width = 1
8                 @height = 1
9                 @text = []
10                 @attributes = []
11                 @x = 0
12                 @y = 0
13                 @a = 0 # cursor attributes
14                 @partial = ''
15                 @resize width, height
16         
17         resize: (width, height) ->
18                 # FIXME: write a version that retains some of the data
19                 @width = width
20                 @height = height
21                 @text = []
22                 @attributes = []
23                 for y in [0...height]
24                         @text[y] = []
25                         @attributes[y] = []
26                         for x in [0...width]
27                                 @text[y].push ' '
28                                 @attributes[y].push 0
29
30         # pass data from stdout
31         update: (data) ->
32                 return unless data?.length > 0
33                 if @partial.length > 0
34                         data = @partial + data
35                         @partial = ''
36                 parts = data.split(/\x1b\[/)
37                 if parts.length > 1
38                         if -1 is @escape_sequence_length parts[parts.length - 1]
39                                 @partial = parts.pop()
40                 if parts.length > 0
41                         for i in [0...parts.length]
42                                 if i is 0
43                                         @update_text parts[i]
44                                 else
45                                         @update_sequence_then_text parts[i]
46                 return
47         
48         clear_rest_of_line: ->
49                 for i in [@x...@width]
50                         @text[@y][i] = ' '
51                         @attributes[@y][i] = @a
52         
53         add_new_line: ->
54                 # clear top line
55                 for i in [0...@width]
56                         @text[0][i] = ' '
57                         @attributes[0][i] = 0
58                 # move (newly cleared) top line to the bottom
59                 tmp = @text.shift()
60                 @text.push(tmp)
61                 tmp = @attributes.shift()
62                 @attributes.push(tmp)
63                 # slide cursor up with rest of text
64                 @y -= 1
65         
66         wrap_to_next_line: ->
67                 if @y is @height - 1
68                         @add_new_line()
69                 @y += 1
70                 @x = 0
71
72         # str has no escape sequences
73         update_text: (str) ->
74                 return unless str.length > 0
75                 for c in str
76                         switch c
77                                 when '\t' # tab
78                                         @update_text "        ".substr(@x % 8)
79                                 when '\x07' # bell
80                                         false
81                                 when '\x0d' # cr
82                                         @x = 0
83                                 when '\x08' # backspace
84                                         if @x > 0
85                                                 @x -= 1
86                                                 @text[@y][@x] = ' '
87                                                 # should this set the attribute too?
88                                 when '\x0a', '\x0b' # lf, vertical tab (same thing)
89                                         @wrap_to_next_line()
90                                 else
91                                         @text[@y][@x] = c
92                                         @attributes[@y][@x] = @a
93                                         @x += 1
94                                         if @x is @width
95                                                 @wrap_to_next_line()
96                 return
97         
98         set_attribute_bits: (mask, value) ->
99                 @a = (@a & ~mask) | value
100
101         csi_m: default: "0", go: ->
102                 for i in arguments
103                         switch i
104                                 when '0'
105                                         @set_attribute_bits 0xffffff, 0
106                                 when '1' # bold
107                                         @set_attribute_bits 0x100, 1
108                                 when '4' # underline
109                                         @set_attribute_bits 0x200, 1
110                                 when '5' # blink
111                                         @set_attribute_bits 0x400, 1
112                                 when '8' # invisible
113                                         @set_attribute_bits 0x800, 1
114                                 
115                                 when '22' # not bold... according to a page
116                                         @set_attribute_bits 0x100, 0
117                                 when '21' # ... though this would make more sense for "not bold"
118                                         @set_attribute_bits 0x100, 0
119                                 when '24' # not underline
120                                         @set_attribute_bits 0x200, 0
121                                 when '25' # not blink
122                                         @set_attribute_bits 0x400, 0
123                                 when '28' # not invisible
124                                         @set_attribute_bits 0x800, 0
125
126                                 when '30' # fg black
127                                         @set_attribute_bits 0xff, 0
128                                 when '31' # fg red
129                                         @set_attribute_bits 0xff, 0xe0
130                                 when '32' # fg green
131                                         @set_attribute_bits 0xff, 0x1c
132                                 when '33' # fg yellow
133                                         @set_attribute_bits 0xff, 0xfc
134                                 when '34' # fg blue
135                                         @set_attribute_bits 0xff, 0x02
136                                 when '35' # fg magenta
137                                         @set_attribute_bits 0xff, 0xe2
138                                 when '36' # fg cyan
139                                         @set_attribute_bits 0xff, 0x1f
140                                 when '37', '39' # fg white  (39 is default)
141                                         @set_attribute_bits 0xff, 0xff
142
143                                 when '40' # bg black
144                                         @set_attribute_bits 0xff00, 0
145                                 when '41' # bg red
146                                         @set_attribute_bits 0xff00, 0xe000
147                                 when '42' # bg green
148                                         @set_attribute_bits 0xff00, 0x1c00
149                                 when '43' # bg yellow
150                                         @set_attribute_bits 0xff00, 0xfc00
151                                 when '44' # bg blue
152                                         @set_attribute_bits 0xff00, 0x0200
153                                 when '45' # bg magenta
154                                         @set_attribute_bits 0xff00, 0xe200
155                                 when '46' # bg cyan
156                                         @set_attribute_bits 0xff00, 0x1f00
157                                 when '47', '49' # bg white  (49 is default)
158                                         @set_attribute_bits 0xff00, 0xff
159
160                                 else
161                                         # if we don't recognize the style, go back to default
162                                         @set_attribute_bits 0xffffff, 0
163                 return
164
165         # str is the whole escape sequence (minus the esc[ prefix)
166         update_sequence: (str) ->
167                 command = @["csi_#{str.substr str.length - 1}"]
168                 return unless command?
169                 args = str.substr(0, str.length - 1).split ';'
170                 for i in [0...args.length]
171                         if args[i] is ''
172                                 args[i] = command.default
173                 command.go.call this, args...
174
175         update_sequence_then_text: (str) ->
176                 len = @escape_sequence_length str
177                 if len is -1
178                         console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
179                         @update_text "ESC[" + str
180                 else
181                         @update_sequence str.substr 0, len
182                         @update_text str.substr len
183
184         escape_sequence_length: (str) ->
185                 parts = str.match(/^[0-9;?]{0,25}./)
186                 return -1 unless parts?
187                 return parts[0].length
188
189 exports.new = (width, height) ->
190         return new Terminal width, height