#!/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');
var blessed = require('blessed');
var telnet = require('telnet');

process.on('uncaughtException', function(err) {
  console.error(err ? err.stack : err + '');
});

var server = telnet.createServer(function(client) {
  client.do.transmit_binary();
  client.do.terminal_type();
  client.do.window_size();
  client.do.environment_variables();

  client.on('environment variables', function(data) {
    if (data.command === 'sb') {
      if (data.name === 'TERM') {
        screen.terminal = data.value;
        screen.render();
      }
    }
  });

  // XXX For when termtype is implemented in node-telnet
  client.on('terminal type', function(data) {
    if (data.command === 'sb' && data.name) {
      screen.terminal = data.name;
      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,
    terminal: 'xterm-256color'
  });

  client.on('close', function() {
    if (!screen.destroyed) {
      screen.destroy();
    }
  });

  screen.key(['C-c', 'q'], function(ch, key) {
    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();
  });

  screen.render();
}

var test = 'widget-' + (process.argv[2] || 'shadow');
if (test === 'widget-png') process.argv.length = 2;

function loadTest(screen, name) {
  var Screen = blessed.screen;
  var key = screen.key;
  blessed.screen = function() { return screen; };
  screen.key = function(keys) {
    keys = [].concat(keys)
    if (~keys.indexOf('q') || ~keys.indexOf('C-c') || ~keys.indexOf('escape')) {
      return;
    }
    return key.apply(screen, arguments);
  };
  var path = require.resolve('../test/' + name);
  delete require.cache[path];
  require('../test/' + name);
  blessed.screen = Screen;
  screen.key = key;
}

server.listen(2300);
console.log('Listening on 2300...');

@@ -275,24 +275,52 @@
 uire('fs');%0A
+var path = require('path');%0A
 var blessed 
@@ -736,22 +736,12 @@
 'sb'
-) %7B%0A      if (
+ && 
 data
@@ -756,26 +756,24 @@
 = 'TERM') %7B%0A
-  
       screen
@@ -798,26 +798,24 @@
 alue;%0A      
-  
 screen.rende
@@ -827,81 +827,16 @@
     
-  %7D%0A    %7D%0A  %7D);%0A%0A  // XXX For when termtype is implemented in node-telnet
+%7D%0A  %7D);%0A
 %0A  c
@@ -1888,84 +1888,8 @@
 );%0A%0A
-  screen.key(%5B'C-c', 'q'%5D, function(ch, key) %7B%0A    screen.destroy();%0A  %7D);%0A%0A
   sc
@@ -2432,32 +2432,108 @@
 ender();%0A  %7D);%0A%0A
+  screen.key(%5B'C-c', 'q'%5D, function(ch, key) %7B%0A    screen.destroy();%0A  %7D);%0A%0A
   screen.render(
@@ -2553,21 +2553,8 @@
 t = 
-'widget-' + (
 proc
@@ -2573,31 +2573,53 @@
 %7C%7C '
+../test/widget-
 shadow
+.js
 '
-)
 ;%0Aif (
+~
 test
- === 
+.indexOf(
 'wid
@@ -2625,17 +2625,21 @@
 dget-png
-'
+.js')
 ) proces
@@ -2656,16 +2656,58 @@
 gth = 2;
+%0Atest = path.resolve(process.cwd(), test);
 %0A%0Afuncti
@@ -2769,32 +2769,8 @@
 en;%0A
-  var key = screen.key;%0A
   bl
@@ -2819,214 +2819,8 @@
  %7D;%0A
-  screen.key = function(keys) %7B%0A    keys = %5B%5D.concat(keys)%0A    if (~keys.indexOf('q') %7C%7C ~keys.indexOf('C-c') %7C%7C ~keys.indexOf('escape')) %7B%0A      return;%0A    %7D%0A    return key.apply(screen, arguments);%0A  %7D;%0A
   va
@@ -2844,29 +2844,16 @@
 resolve(
-'../test/' + 
 name);%0A 
@@ -2895,21 +2895,8 @@
 ire(
-'../test/' + 
 name
@@ -2929,28 +2929,8 @@
 en;%0A
-  screen.key = key;%0A
 %7D%0A%0As

refactor and improve blessed-telnet.