2015-08-05 14:18:47 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* blessed-telnet.js
|
|
|
|
* https://github.com/chjj/blessed
|
|
|
|
* Copyright (c) 2013-2015, Christopher Jeffrey (MIT License)
|
|
|
|
* A blessed telnet server.
|
|
|
|
* See: https://github.com/TooTallNate/node-telnet
|
|
|
|
*/
|
|
|
|
|
|
|
|
process.title = 'blessed-telnet';
|
|
|
|
|
|
|
|
var fs = require('fs');
|
2015-08-08 04:02:26 +00:00
|
|
|
var path = require('path');
|
2015-08-05 14:18:47 +00:00
|
|
|
var blessed = require('blessed');
|
|
|
|
var telnet = require('telnet');
|
|
|
|
|
|
|
|
var server = telnet.createServer(function(client) {
|
|
|
|
client.do.transmit_binary();
|
|
|
|
client.do.terminal_type();
|
|
|
|
client.do.window_size();
|
2015-08-06 10:27:18 +00:00
|
|
|
client.do.environment_variables();
|
|
|
|
|
2015-08-08 22:02:53 +00:00
|
|
|
client.on('debug', function(msg) {
|
|
|
|
console.error(msg);
|
|
|
|
});
|
|
|
|
|
2015-08-06 10:27:18 +00:00
|
|
|
client.on('environment variables', function(data) {
|
2015-08-09 00:20:32 +00:00
|
|
|
if (data.command === 'sb') {
|
|
|
|
if (data.name === 'TERM') {
|
|
|
|
screen.terminal = data.value;
|
|
|
|
} else {
|
|
|
|
// Clear the screen since they may have used `env send [var]`.
|
|
|
|
screen.alloc();
|
|
|
|
screen.clearRegion(0, screen.width, 0, screen.height, true);
|
|
|
|
}
|
2015-08-08 04:02:26 +00:00
|
|
|
screen.render();
|
2015-08-06 10:27:18 +00:00
|
|
|
}
|
|
|
|
});
|
2015-08-05 14:18:47 +00:00
|
|
|
|
|
|
|
client.on('terminal type', function(data) {
|
2015-08-06 10:33:00 +00:00
|
|
|
if (data.command === 'sb' && data.name) {
|
|
|
|
screen.terminal = data.name;
|
2015-08-05 14:18:47 +00:00
|
|
|
screen.render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('window size', function(data) {
|
|
|
|
if (data.command === 'sb') {
|
|
|
|
client.columns = data.columns;
|
|
|
|
client.rows = data.rows;
|
|
|
|
client.emit('resize');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the client look like a tty:
|
|
|
|
client.setRawMode = function(mode) {
|
|
|
|
client.isRaw = mode;
|
|
|
|
if (!client.writable) return;
|
|
|
|
if (mode) {
|
|
|
|
client.do.suppress_go_ahead();
|
|
|
|
client.will.suppress_go_ahead();
|
|
|
|
client.will.echo();
|
|
|
|
} else {
|
|
|
|
client.dont.suppress_go_ahead();
|
|
|
|
client.wont.suppress_go_ahead();
|
|
|
|
client.wont.echo();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
client.isTTY = true;
|
|
|
|
client.isRaw = false;
|
|
|
|
client.columns = 80;
|
|
|
|
client.rows = 24;
|
|
|
|
|
|
|
|
var screen = blessed.screen({
|
|
|
|
smartCSR: true,
|
|
|
|
input: client,
|
|
|
|
output: client,
|
2015-08-06 02:34:18 +00:00
|
|
|
terminal: 'xterm-256color'
|
2015-08-05 14:18:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', function() {
|
|
|
|
if (!screen.destroyed) {
|
|
|
|
screen.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.on('destroy', function() {
|
|
|
|
if (client.writable) {
|
|
|
|
client.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (test === 'widget-simple') {
|
|
|
|
return simpleTest(screen);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadTest(screen, test);
|
|
|
|
});
|
|
|
|
|
|
|
|
function simpleTest(screen) {
|
|
|
|
screen.data.main = blessed.box({
|
|
|
|
parent: screen,
|
|
|
|
width: '80%',
|
|
|
|
height: '90%',
|
|
|
|
border: 'line',
|
|
|
|
content: 'Welcome to my server. Here is your own private session.',
|
|
|
|
style: {
|
|
|
|
bg: 'red'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.key('i', function() {
|
|
|
|
screen.data.main.style.bg = 'blue';
|
|
|
|
screen.render();
|
|
|
|
});
|
|
|
|
|
2015-08-08 04:02:26 +00:00
|
|
|
screen.key(['C-c', 'q'], function(ch, key) {
|
|
|
|
screen.destroy();
|
|
|
|
});
|
|
|
|
|
2015-08-05 14:18:47 +00:00
|
|
|
screen.render();
|
|
|
|
}
|
|
|
|
|
2015-08-08 12:04:06 +00:00
|
|
|
var test = process.argv[2] || path.resolve(__dirname, '../test/widget-shadow.js');
|
2015-08-08 04:02:26 +00:00
|
|
|
if (~test.indexOf('widget-png.js')) process.argv.length = 2;
|
|
|
|
test = path.resolve(process.cwd(), test);
|
2015-08-05 14:18:47 +00:00
|
|
|
|
|
|
|
function loadTest(screen, name) {
|
|
|
|
var Screen = blessed.screen;
|
|
|
|
blessed.screen = function() { return screen; };
|
2015-08-08 04:02:26 +00:00
|
|
|
var path = require.resolve(name);
|
2015-08-05 14:18:47 +00:00
|
|
|
delete require.cache[path];
|
2015-08-08 04:02:26 +00:00
|
|
|
require(name);
|
2015-08-05 14:18:47 +00:00
|
|
|
blessed.screen = Screen;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.listen(2300);
|
|
|
|
console.log('Listening on 2300...');
|