#!/usr/bin/env node

var Chalk = require('chalk');
var sendError = function(msg){
	msg = '\ntty-table says: ' + msg + '\n';
	console.log(msg);
	process.exit(1);
};

//get arguments
var argv = require('minimist')(process.argv.slice(2));	

//check if help called
if(argv.help){
	var msg = '\n';
			msg += Chalk.bgBlack.green('OPTIONS')+'\n';
			msg += '-------\n\n'; 
			msg += '--format (JSON,csv)\n';
	console.log(msg);
	process.exit();
}

//note that this is the first run
var alreadyRendered = false;
var previousHeight = 0;

//check format of input data
var dataFormat = 'csv' //default

if(argv.format){
	dataFormat = argv.format
}

switch(true){
	case(dataFormat.toString().match(/json/i) !== null):
		dataFormat = 'json';
		break;
	default:
		var csv = require('csv');
}

//sometimes users need to speciy delimiter or special options
var formatterOptions = {};
if(dataFormat === 'csv'){
	Object.keys(argv).forEach(function(key){
		if(key.substr(0,3)==='csv'){
			formatterOptions[key.substr(4,key.length)] = argv[key]	
		}
	});
}

//because diffent dataFormats 
var runTable = function(input){
	
	var header = [], 
	body = input, 
	//footer = [], 
	options = {};

	var Table = require('./public.js');
	var t1 = Table.setup(header,body,options);
	
	//hide cursor
	console.log('\u001b[?25l');
	
	//wipe existing if already rendered
	if(alreadyRendered){

		//move cursor up number to the top of the previous print
		//before deleting
		console.log('\u001b['+(previousHeight+3)+'A');
		
		//delete to end of terminal
		console.log('\u001b[0J');
	}
	else{
		alreadyRendered = true;
	}
	
	console.log(t1.render());
	
	//reset the previous height to the height of this output
	//for when we next clear the print
	previousHeight = t1.height;

};

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {

	//handle dataFormats
	switch(true){
		case(dataFormat==='json'):	
			try {
				var data = JSON.parse(chunk);
			}
			catch(e){
				var msg = "JSON parse error.";
				msg = Chalk.bgRed.white(msg);
				msg = msg + "\n\nPlease check to make sure that your input data consists of JSON or specify a different format with the --format flag.";
				sendError(msg);
			}
			runTable(data);
			break;
		default:
			csv.parse(chunk,formatterOptions,function(err, data){
				//validate csv	
				if(typeof data === 'undefined'){
					var msg = "CSV parse error.";
					msg = Chalk.bgRed.white(msg);
					msg = msg + "\n\nPlease check to make sure that your input data consists of comma separated values or specify a different format with the --format flag.";
					sendError(msg);
				}
				runTable(data);
			});
	}
});

process.stdin.on('end', function() {
	//nothing
});


@@ -13,17 +13,46 @@
 nv node%0A
+var yargs = require('yargs');
 %0A
-
 var Chal
@@ -191,283 +191,8 @@
 %7D;%0A%0A
-//get arguments%0Avar argv = require('minimist')(process.argv.slice(2));%09%0A%0A//check if help called%0Aif(argv.help)%7B%0A%09var msg = '%5Cn';%0A%09%09%09msg += Chalk.bgBlack.green('OPTIONS')+'%5Cn';%0A%09%09%09msg += '-------%5Cn%5Cn'; %0A%09%09%09msg += '--format (JSON,csv)%5Cn';%0A%09console.log(msg);%0A%09process.exit();%0A%7D%0A%0A
 //no
@@ -341,16 +341,22 @@
 ult%0A%0Aif(
+yargs.
 argv.for
@@ -375,16 +375,22 @@
 ormat = 
+yargs.
 argv.for
@@ -541,267 +541,8 @@
 %0A%7D%0A%0A
-//sometimes users need to speciy delimiter or special options%0Avar formatterOptions = %7B%7D;%0Aif(dataFormat === 'csv')%7B%0A%09Object.keys(argv).forEach(function(key)%7B%0A%09%09if(key.substr(0,3)==='csv')%7B%0A%09%09%09formatterOptions%5Bkey.substr(4,key.length)%5D = argv%5Bkey%5D%09%0A%09%09%7D%0A%09%7D);%0A%7D%0A%0A
 //be
@@ -1494,39 +1494,8 @@
 sg =
- %22JSON parse error.%22;%0A%09%09%09%09msg =
  Cha
@@ -1721,16 +1721,199 @@
 efault:%0A
+%09%09%09var formatterOptions = %7B%7D;%0A%09%09%09Object.keys(yargs.argv).forEach(function(key)%7B%0A%09%09%09%09if(key.slice(0,4) === 'csv-')%7B%0A%09%09%09%09%09formatterOptions%5Bkey.slice(4)%5D = yargs.argv%5Bkey%5D;%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A
 %09%09%09csv.p
@@ -2359,12 +2359,60 @@
 othing%0A%7D);%0A%0A
+//run help only at the end%0A//yargs.argv.help();%0A

Add option to pass csv parser custom args