handler = (req, res) ->
- err = (req, res) ->
- res.writeHead(404, 'Content-Type': 'text/plain')
- return res.end("Error loading #{req.url}")
+ reply_err = (req, res, msg) ->
+ res.writeHead(200, 'Content-Type': 'text/plain')
+ return res.end("Error loading #{req.url} \"#{msg}\"")
switch req.url
when '/', '/index.html'
filename = __dirname + '/index.html'
when '/jquery.js'
filename = '/usr/share/javascript/jquery/jquery.min.js'
type = 'text/javascript'
+ when '/terminal.js'
+ filename = __dirname + '/terminal.coffee'
+ type = 'text/javascript'
+ when '/client.js'
+ filename = __dirname + '/client.coffee'
+ type = 'text/javascript'
else
- return err req, res
- fs.readFile filename, (err, data) ->
- return err req, res if err
+ return reply_err req, res
+ fs.readFile filename, 'utf8', (err, data) ->
+ return reply_err req, res, err if err
+
+ if filename.substr(filename.length - 7) is '.coffee'
+ try
+ data = coffee.compile data
+ catch e
+ return reply_err req, res, "server faild to compile #{filename}: #{JSON.stringify(e)}"
res.writeHead(200, 'Content-Type': type)
res.end(data)
+coffee = require 'coffee-script'
app = require('http').createServer(handler)
io = require('socket.io').listen(app)
fs = require('fs')
terminal = require('./terminal.coffee')
# SETTINGS
-app.listen(9293)
-term = terminal.new(105, 66)
+app.listen(2218)
+term = terminal.new(104, 66)
sockets = []
sockets.splice i, 1
return
+
+ enc_color = (prefix, c) ->
+ if c < 8
+ return "#{prefix}#{c}"
+ if c < 16
+ return "#{prefix + 6}#{c - 8}"
+ return "#{prefix}8;5;#{c}"
+
+ attr_diff = (a, b) ->
+ xo = a ^ b
+ parts = []
+ if (xo & 0xff)
+ parts.push enc_color 3, (b & 0xff)
+ if (xo & 0xff00)
+ parts.push enc_color 4, ((b & 0xff00) >> 8)
+ for [bit, code] in [[0x010000, '1'], [0x200000, '3'], [0x020000, '4'], [0x040000, '5'], [0x080000, '7'], [0x100000, '8']]
+ if (xo & bit)
+ if (b & bit)
+ parts.push code
+ else
+ parts.push '2' + code
+ if parts.length
+ return "\x1b[#{parts.join ';'}m"
+ else
+ return ''
+ encode_screen = (height, width, text, attributes) ->
+ state = ''
+ for y in [0...height]
+ for x in [0...width]
+ if attributes[y][x] isnt a
+ state += attr_diff a, attributes[y][x]
+ a = attributes[y][x]
+ state += text[y][x]
+ if y < height - 1
+ state += '\n'
+ return state
+
+ a = 0x07
+ state = ''
+ if term.saved_normal_screen?
+ state += encode_screen term.height, term.width, term.saved_normal_screen.text, term.saved_normal_screen.attributes
+ state += "\x1b[#{term.saved_normal_screen.y + 1};#{term.saved_normal_screen.x + 1}H"
+ state += "\x1b[?1049h\x1b[H" # flip to alt screen and move cursor home
+ state += encode_screen term.height, term.width, term.text, term.attributes
+ state += attr_diff a, term.a
+ state += "\x1b[#{term.y + 1};#{term.x + 1}H"
+ unless term.cursor_visible
+ state += "\x1b[?25l"
+ unless term.scroll_top is 0 and term.scroll_bottom is term.height - 1
+ state += "\x1b[#{term.scroll_top};#{term.scroll_bottom}r"
+ socket.emit 'init', width: term.width, height: term.height, text: state
+
process.stdin.resume()
process.stdin.setEncoding 'utf8'
process.stdin.on 'data', (data) ->
term.update data
for s in sockets
- s.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes
+ s.emit 'data', data
process.stdin.on 'end', -> process.exit()