Skip to content

Instantly share code, notes, and snippets.

@eddyb
Created February 21, 2012 12:49
Show Gist options
  • Save eddyb/1876363 to your computer and use it in GitHub Desktop.
Save eddyb/1876363 to your computer and use it in GitHub Desktop.
Terminal.cursorBlink = false;
$ui.Terminal = function Terminal(socket) {
var term = new window.Terminal(80, 30, function(data) {
socket.emit('terminal.data', data, term.id);
});
term.open();
var grip = $('<div class=grip>'), el = $('<div class=wrapper>').append(term.element, grip);
//body.appendChild(el);
term.wrapper = el[0], term.grip = grip[0];
grip.mousedown(function(ev) {
term.focus();
var resizeW = el[0].offsetWidth, resizeH = el[0].offsetHeight;
el.css({overflow: 'hidden', opacity: 0.70, cursor: 'se-resize'});
$(term.element).css('height', '100%');
$(document).on('mousemove.resize', function(ev) {
el.width(ev.pageX - el[0].offsetLeft).height(ev.pageY - el[0].offsetTop);
}).on('mouseup.resize', function(ev) {
var x = (el[0].offsetWidth / resizeW * term.cols) | 0,
y = (el[0].offsetHeight / resizeH * term.rows) | 0;
socket.emit('terminal.resize', x, y, term.id);
term.resize(x, y);
el.css({width: '', height: '', overflow: '', opacity: '', cursor: ''});
$(term.element).css('height', '');
$(document).off('mousemove.resize').off('mouseup.resize');
});
return false;
});
el.mousedown(function(ev) {
if(ev.target !== el[0])
return;
term.focus();
var drag = el.offset();
drag.pageX = ev.pageX;
drag.pageY = ev.pageY;
el.css({opacity: 0.60, cursor: 'move'});
$(document).on('mousemove.drag', function(ev) {
el.offset({left: drag.left + ev.pageX - drag.pageX, top: drag.top + ev.pageY - drag.pageY});
}).on('mouseup.drag', function(ev) {
el.css({opacity: '', cursor: ''});
$(document).off('mousemove.drag').off('mouseup.drag');
});
return false;
});
socket.emit('terminal.create', function(id) {
term.id = id;
socket.terms[id] = term;
});
return el;
};
$ui.Terminal.enable = function enable(socket) {
if(socket.terms)
return;
socket.terms = [];
socket.on('terminal.data', function(data, id) {
socket.terms[id].write(data);
});
socket.on('terminal.kill', function(id) {
delete socket.terms[id];
});
};
$ui.Terminal.disable = function disable(socket) {
if(!socket.terms)
return;
var term;
while(socket.terms.length)
if(term = socket.terms.pop())
socket.emit('terminal.kill', term.id);
delete socket.terms;
};
var Terminal = require('tty.js/lib/term.js');
// Path to shell, or the process to execute in the terminal.
var shellPath = process.env.SHELL || 'sh';
// $TERM
var termName = 'xterm';
exports.enable = function enable(socket) {
if(socket.terms)
return;
socket.terms = [];
socket.on('terminal.create', function(callback) {
var term = new Terminal(shellPath, termName, 80, 30);
var id = socket.terms.push(term) - 1;
term.on('data', function(data) {
socket.emit('terminal.data', data, id);
});
term.on('close', function() {
socket.emit('terminal.kill', id);
});
callback(id);
});
socket.on('terminal.data', function(data, id) {
socket.terms[id].write(data);
});
socket.on('terminal.kill', function(id) {
if(!socket.terms[id])
return;
socket.terms[id].destroy();
delete socket.terms[id];
});
socket.on('terminal.resize', function(cols, rows, id) {
socket.terms[id].resize(cols, rows);
});
};
exports.disable = function disable(socket) {
var term;
while(socket.terms.length)
if(term = socket.terms.pop())
term.destroy();
delete socket.terms;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment