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