JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
run.sh: complain if not passed width/height
[watch-my-terminal.git] / server.coffee
1 # SETTINGS
2 term_columns = 97
3 term_lines = 63
4 http_port = 2218
5
6 if process.argv.length > 2
7         term_columns = parseInt process.argv[2]
8 if process.argv.length > 3
9         term_lines = parseInt process.argv[3]
10
11
12 handler = (req, res) ->
13         reply_err = (req, res, msg) ->
14                 res.writeHead(200, 'Content-Type': 'text/plain')
15                 return res.end("Error loading #{req.url} \"#{msg}\"")
16         switch req.url
17                 when '/', '/index.html'
18                         filename = __dirname + '/index.html'
19                         type = 'text/html'
20                 when '/jquery.js'
21                         filename = '/usr/share/javascript/jquery/jquery.min.js'
22                         type = 'text/javascript'
23                 when '/terminal.js'
24                         filename = __dirname + '/terminal.coffee'
25                         type = 'text/javascript'
26                 when '/htmlterm.js'
27                         filename = __dirname + '/htmlterm.coffee'
28                         type = 'text/javascript'
29                 when '/client.js'
30                         filename = __dirname + '/client.coffee'
31                         type = 'text/javascript'
32                 else
33                         return reply_err req, res
34         fs.readFile filename, 'utf8', (err, data) ->
35                 return reply_err req, res, err if err
36
37                 if filename.substr(filename.length - 7) is '.coffee'
38                         try
39                                 data = coffee.compile data
40                         catch e
41                                 return reply_err req, res, "server faild to compile #{filename}: #{JSON.stringify(e)}"
42
43                 res.writeHead(200, 'Content-Type': type)
44                 res.end(data)
45
46 coffee = require 'coffee-script'
47 app = require('http').createServer(handler)
48 io = require('socket.io').listen(app)
49 fs = require('fs')
50 terminal = require('./terminal.coffee')
51
52 app.listen(http_port)
53 term = terminal.new(term_columns, term_lines)
54
55 sockets = []
56
57 io.sockets.on 'connection', (socket) ->
58         sockets.push socket
59         socket.on 'disconnect', ->
60                 for i in [i...sockets.length]
61                         if sockets[i] is socket
62                                 sockets.splice i, 1
63                                 return
64
65
66         enc_color = (prefix, c) ->
67                 if c < 8
68                         return "#{prefix}#{c}"
69                 if c < 16
70                         return "#{prefix + 6}#{c - 8}"
71                 return "#{prefix}8;5;#{c}"
72
73         attr_diff = (a, b) ->
74                 xo = a ^ b
75                 parts = []
76                 if (xo & 0xff)
77                         parts.push enc_color 3, (b & 0xff)
78                 if (xo & 0xff00)
79                         parts.push enc_color 4, ((b & 0xff00) >> 8)
80                 for [bit, code] in [[0x010000, '1'], [0x200000, '3'], [0x020000, '4'], [0x040000, '5'], [0x080000, '7'], [0x100000, '8']]
81                         if (xo & bit)
82                                 if (b & bit)
83                                         parts.push code
84                                 else
85                                         parts.push '2' + code
86                 if parts.length
87                         return "\x1b[#{parts.join ';'}m"
88                 else
89                         return ''
90         encode_screen = (height, width, text, attributes) ->
91                 state = ''
92                 for y in [0...height]
93                         max = width - 1
94                         while max >= 0 and text[y][max] is ' ' and (attributes[y][max] & 0x08ff00) is 0
95                                 max -= 1
96                         if max >= 0
97                                 for x in [0..max]
98                                         if attributes[y][x] isnt a
99                                                 state += attr_diff a, attributes[y][x]
100                                                 a = attributes[y][x]
101                                         state += text[y][x]
102                         if y < height - 1
103                                 state += '\n'
104                 return state
105
106         a = 0x07
107         state = ''
108         if term.saved_normal_screen?
109                 state += encode_screen term.height, term.width, term.saved_normal_screen.text, term.saved_normal_screen.attributes
110                 state += "\x1b[#{term.saved_normal_screen.y + 1};#{term.saved_normal_screen.x + 1}H"
111                 state += "\x1b[?1049h\x1b[H" # flip to alt screen and move cursor home
112         state += encode_screen term.height, term.width, term.text, term.attributes
113         state += attr_diff a, term.a
114         state += "\x1b[#{term.y + 1};#{term.x + 1}H"
115         unless term.cursor_visible
116                 state += "\x1b[?25l"
117         unless term.scroll_top is 0 and term.scroll_bottom is term.height - 1
118                 state += "\x1b[#{term.scroll_top};#{term.scroll_bottom}r"
119         socket.emit 'init', width: term.width, height: term.height, text: state
120
121 process.stdin.resume()
122 process.stdin.setEncoding 'utf8'
123
124 process.stdin.on 'data', (data) ->
125         term.update data
126         for s in sockets
127                 s.emit 'data', data
128
129 process.stdin.on 'end', -> process.exit()