X-Git-Url: https://jasonwoof.com/gitweb/?p=watch-my-terminal.git;a=blobdiff_plain;f=server.coffee;h=f79fe33108c099279a42e4d2f9b94350562e6fe1;hp=06f6aed56f55f4d9dbe5c90517d6d8f655a07664;hb=HEAD;hpb=37d536e72e3e6fa4de536ad916a0a80119f15509 diff --git a/server.coffee b/server.coffee index 06f6aed..f79fe33 100644 --- a/server.coffee +++ b/server.coffee @@ -1,3 +1,14 @@ +# SETTINGS +term_columns = 97 +term_lines = 63 +http_port = 2218 + +if process.argv.length > 2 + term_columns = parseInt process.argv[2] +if process.argv.length > 3 + term_lines = parseInt process.argv[3] + + handler = (req, res) -> reply_err = (req, res, msg) -> res.writeHead(200, 'Content-Type': 'text/plain') @@ -12,6 +23,9 @@ handler = (req, res) -> when '/terminal.js' filename = __dirname + '/terminal.coffee' type = 'text/javascript' + when '/htmlterm.js' + filename = __dirname + '/htmlterm.coffee' + type = 'text/javascript' when '/client.js' filename = __dirname + '/client.coffee' type = 'text/javascript' @@ -35,9 +49,8 @@ io = require('socket.io').listen(app) fs = require('fs') terminal = require('./terminal.coffee') -# SETTINGS -app.listen(2218) -term = terminal.new(104, 66) +app.listen(http_port) +term = terminal.new(term_columns, term_lines) sockets = [] @@ -74,18 +87,29 @@ io.sockets.on 'connection', (socket) -> return "\x1b[#{parts.join ';'}m" else return '' + encode_screen = (height, width, text, attributes) -> + state = '' + for y in [0...height] + max = width - 1 + while max >= 0 and text[y][max] is ' ' and (attributes[y][max] & 0x08ff00) is 0 + max -= 1 + if max >= 0 + for x in [0..max] + 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 = '' - # FIXME handle alt screen - for y in [0...term.height] - for x in [0...term.width] - if term.attributes[y][x] isnt a - state += attr_diff a, term.attributes[y][x] - a = term.attributes[y][x] - state += term.text[y][x] - if y < term.height - 1 - state += '\n' + 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