diff --git a/README.md b/README.md index be7d5cce..f05e60ee 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Server: ``` js var term = require('term.js'); -express.use(term.middleware()); +app.use(term.middleware()); ... ``` @@ -17,22 +17,33 @@ Client: ``` js window.addEventListener('load', function() { - var term = new Terminal({ - cols: 80, - rows: 24 + var socket = io.connect(); + socket.on('connect', function() { + var term = new Terminal({ + cols: 80, + rows: 24 + }); + + term.on('data', function(data) { + socket.emit('data', data); + }); + + term.on('title', function(title) { + document.title = title; + }); + + term.open(document.body); + + term.write('\x1b[31mWelcome to term.js!\x1b[m\r\n'); + + socket.on('data', function(data) { + term.write(data); + }); + + socket.on('disconnect', function() { + term.destroy(); + }); }); - - term.open(document.body); - - term.on('data', function(data) { - console.log(data); - }); - - term.on('title', function(title) { - console.log('New title: ' + title); - }); - - term.write('\x1b[41hello world\x1b[m'); }, false); ``` diff --git a/example/index.html b/example/index.html index f08d0bcc..16e14c71 100644 --- a/example/index.html +++ b/example/index.html @@ -1,7 +1,7 @@ -tty.js +term.js -

tty.js

+

term.js

diff --git a/example/index.js b/example/index.js index 0f2df1ff..18df3e6a 100644 --- a/example/index.js +++ b/example/index.js @@ -1,18 +1,19 @@ /** - * tty.js + * term.js * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License) */ var http = require('http') , express = require('express') , io = require('socket.io') - , pty = require('pty.js'); + , pty = require('pty.js') + , terminal = require('../'); /** - * tty.js + * term.js */ -process.title = 'tty.js'; +process.title = 'term.js'; /** * Open Terminal @@ -22,8 +23,10 @@ var buff = [] , socket , term; -var term = pty.fork(process.env.SHELL || 'sh', [], { - name: 'xterm', +term = pty.fork(process.env.SHELL || 'sh', [], { + name: require('fs').existsSync('/usr/share/terminfo/x/xterm-256color') + ? 'xterm-256color' + : 'xterm', cols: 80, rows: 24, cwd: process.env.HOME @@ -61,8 +64,15 @@ app.use(function(req, res, next) { next(); }); +app.use(express.basicAuth(function(user, pass, next) { + if (user !== 'foo' || pass !== 'bar') { + return next(true); + } + return next(null, user); +})); + app.use(express.static(__dirname)); -app.use(express.static(__dirname + '/../lib')); +app.use(terminal.middleware()); server.listen(8080); @@ -74,6 +84,7 @@ server.on('connection', function(socket) { } catch (e) { ; } + console.log('Attempted connection from %s. Refused.', address); } }); @@ -81,10 +92,8 @@ server.on('connection', function(socket) { * Sockets */ -io = io.listen(server); - -io.configure(function() { - io.disable('log'); +io = io.listen(server, { + log: false }); io.sockets.on('connection', function(sock) { diff --git a/lib/term.js b/lib/term.js index e8f22554..91c92684 100644 --- a/lib/term.js +++ b/lib/term.js @@ -822,9 +822,9 @@ Terminal.prototype.bindMouse = function() { // be sure to avoid sending // bad positions to the program if (x < 0) x = 0; - if (x >= self.cols) x = self.cols - 1; + if (x > self.cols) x = self.cols; if (y < 0) y = 0; - if (y >= self.rows) y = self.rows - 1; + if (y > self.rows) y = self.rows; // xterm sends raw bytes and // starts at 32 (SP) for each. @@ -909,6 +909,9 @@ Terminal.prototype.destroy = function() { this._events = {}; this.handler = function() {}; this.write = function() {}; + if (this.element.parentNode) { + this.element.parentNode.removeChild(this.element); + } //this.emit('close'); }; @@ -950,9 +953,10 @@ Terminal.prototype.refresh = function(start, end) { width = this.cols; y = start; - // if (end > this.lines.length) { - // end = this.lines.length; - // } + if (end >= this.lines.length) { + this.log('`end` is too large. Most likely a bad CSR.'); + end = this.lines.length - 1; + } for (; y <= end; y++) { row = y + this.ydisp; @@ -984,7 +988,7 @@ Terminal.prototype.refresh = function(start, end) { } if (data !== this.defAttr) { if (data === -1) { - out += ''; + out += ''; } else { out += '