var Pty = require("pty.js");

var Tty = module.exports = {};

var _term = null;
var _data = [];

Tty.create = function (options, callback) {

    _term = _term || Pty.fork(process.env.SHELL, [], {
        name: "Browser Term",
        cols: options.cols,
        rows: options.rows,
        cwd: process.env.HOME
    });

    _term.on("data", function(data) {
        _data.push(data);
        options.socket.emit("data", data);
    });

    _term.on("close", function() {
        options.socket.emit("kill");
        Tty.kill();
    });

    for (var i = 0; i < _data.length; ++i) {
        options.socket.emit("data", _data[i]);
    }

    return _term;
};

Tty.data = function (data) {
    if (!_term) { return; }
    _term.write(data);
};

Tty.kill= function () {
    if (!_term) { return; }
    _term.destroy();
    _term = null;
};

Tty.resize= function () {
    if (!_term) { return; }
    _term.resize(cols, rows);
};

Tty.process= function (callback) {
    if (!_term) { return; }
    callback(_term.process);
};

Tty.disconnect= function () {
    // TODO
};

Tty.paste= function (callback) {
    // TODO
    callback();
};

@@ -76,24 +76,8 @@
 ull;
-%0Avar _data = %5B%5D;
 %0A%0ATt
@@ -134,17 +134,8 @@
 rm =
- _term %7C%7C
  Pty
@@ -332,34 +332,8 @@
 ) %7B%0A
-        _data.push(data);%0A
     
@@ -468,24 +468,24 @@
 Tty.kill();%0A
+
     %7D);%0A%0A   
@@ -485,107 +485,8 @@
 );%0A%0A
-    for (var i = 0; i %3C _data.length; ++i) %7B%0A        options.socket.emit(%22data%22, _data%5Bi%5D);%0A    %7D%0A%0A
     

Create new terminal instances for each socket.