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