fix(editor): Added support for pipes

Builds off of 59216ca2d2 to implement accepting
input from the unix pipe (eg. `netstat | slap`). Currently, the editor only
supports pipe input if there are no arguments (by choice to mimic behavior of
running `netstat | less some_file.txt`).
This commit is contained in:
Terence Sun 2015-12-08 02:12:30 -05:00
parent 2cc7511678
commit 7d2bfc316e
2 changed files with 17 additions and 12 deletions

View File

@ -3,6 +3,7 @@ var path = require('path');
var rc = require('rc');
var util = require('slap-util');
var ttys = require('ttys');
var package = require('../package');
var baseDir = path.join(__dirname, '..');
var configFile = path.join(baseDir, package.name + '.ini');
@ -60,11 +61,15 @@ if (opts.perf.profile && process.execArgv.indexOf('--prof') === -1) {
function readAsync (stream) {
return new Promise(function (resolve, reject) {
var chunks = [];
stream
.on('error', reject)
.on('data', chunks.push.bind(chunks))
.on('end', function () { resolve(Buffer.concat(chunks)); });
if(!stream.isTTY) { // Checks if there is a pipe
var chunks = [];
stream
.on('error', reject)
.on('data', chunks.push.bind(chunks))
.on('end', function () { resolve(Buffer.concat(chunks)); });
}else{
resolve();
}
});
}
@ -83,8 +88,8 @@ module.exports = function (options) {
]).spread(function (userDir, stdin) {
if (userDir) opts = _.merge({logger: {file: path.resolve(userDir, package.name+'.log')}}, opts);
opts = _.merge({editor: {logger: opts.logger}}, opts);
opts.screenOpts.input = ttys.stdin; // Uses ttys to read from /dev/tty
util.logger(opts.logger);
util.logger.info("loading...");
util.logger.verbose("configuration:", opts);
@ -95,13 +100,12 @@ module.exports = function (options) {
Promise.all(opts._.map(function (path, i) {
return slap.open(path.toString(), !i);
})).done();
if (stdin) {
var pane = new Pane({parent: self});
pane.editor.textBuf.setText(stdin.toString());
if (!opts._.length) { // if no files are passed
var pane = new Pane({parent: slap});
// Sets stdin from pipe if it exists
if (stdin) pane.editor.textBuf.setText(stdin.toString());
pane.setCurrent();
} else if (!opts._.length) { // if no files are passed
new Pane({parent: slap}).setCurrent(); // open a new empty file
if (!opts.config) { // first run without a file passed
slap.help().done();
fs.createReadStream(path.join(__dirname, '..', 'default-config.ini')).pipe(fs.createWriteStream(path.join(userDir, 'config')));

View File

@ -30,6 +30,7 @@
"node-clap": "0.0.5",
"rc": "1.1.5",
"slap-util": "1.0.5",
"ttys": "0.0.3",
"update-notifier": "0.6.0"
},
"devDependencies": {