Move stdout/stderr logic to the renderers to allow for greater flexibility.

This commit is contained in:
André Cruz
2013-05-24 11:14:14 +01:00
parent aea39080d6
commit c4cbfd352e
7 changed files with 104 additions and 133 deletions

View File

@@ -1,7 +1,5 @@
var mout = require('mout');
var colorful;
var colorless;
var paddings = {
tag: 10,
tagPlusLabel: 31
@@ -9,67 +7,23 @@ var paddings = {
var tagColors = {
'warn': 'yellow',
'error': 'red',
'_default': 'cyan',
'default': 'cyan',
};
function renderData(data) {
// Ensure data
data.data = data.data || '';
return 'bower ' + renderTagPlusLabel(data) + ' ' + data.data + '\n';
}
function renderError(err) {
var str;
err.level = 'error';
err.tag = 'error';
str = 'bower ' + renderTagPlusLabel(err) + ' ' + (err.code ? err.code + ' ,' : '') + err.message + '\n';
// Check if additional details were provided
if (err.details) {
str += err.details + '\n';
}
// Print stack
str += '\n' + err.stack + '\n';
return str;
}
function renderEnd() {
return '';
}
function renderCheckout(data) {
if (isCompact()) {
data.data = data.origin + '#' + data.data;
}
return renderData(data);
}
// -------------------------
function empty() {
return '';
function isCompact() {
return process.stdout.columns < 120;
}
function uncolor(str) {
return str.replace(/\x1B\[\d+m/g, '');
}
function isCompact() {
return process.stdout.columns < 120;
}
function renderTagPlusLabel(data) {
var label;
var length;
var nrSpaces;
var tag = data.tag;
var tagColor = tagColors[data.level] || tagColors._default;
var tagColor = tagColors[data.level] || tagColors['default'];
// If there's not enough space, print only the tag
if (isCompact()) {
@@ -80,31 +34,57 @@ function renderTagPlusLabel(data) {
length = tag.length + label.length + 1;
nrSpaces = paddings.tagPlusLabel - length;
// Ensure at least one space
// Ensure at least one space between the label and the tag
if (nrSpaces < 1) {
nrSpaces = 1;
}
return label.green + mout.string.repeat(' ', nrSpaces) + tag[tagColor];
}
// -------------------------
colorful = {
head: empty,
tail: empty,
data: renderData,
error: renderError,
end: renderEnd,
checkout: renderCheckout
var colorful = {
begin: function () {},
end: function () {},
error: function (err) {
var str;
str = 'bower ' + renderTagPlusLabel(err) + ' ' + (err.code ? err.code + ' ,' : '') + err.message + '\n';
// Check if additional details were provided
if (err.details) {
str += err.details + '\n';
}
// Print stack
str += '\n' + err.stack + '\n';
this._write(process.stderr, str);
},
data: function (data) {
data.data = data.data || '';
this._write(process.stdout, 'bower ' + renderTagPlusLabel(data) + ' ' + data.data + '\n');
},
checkout: function (data) {
if (isCompact()) {
data.data = data.origin + '#' + data.data;
}
this.data(data);
},
_write: function (channel, str) {
channel.write(str);
}
};
// The colorless variant simply removes the colors from the colorful methods
colorless = mout.object.map(module.exports.colorful, function (fn) {
return function () {
var str = fn.apply(fn, arguments);
return uncolor(str);
};
// The colorless variant simply removes the colors from the write method
var colorless = mout.object.mixIn({}, colorful, {
_write: function (channel, str) {
channel.write(uncolor(str));
}
});
module.exports.colorful = colorful;

View File

@@ -1,5 +1,5 @@
module.exports = {
cli: require('./cli'),
json: require('./json'),
mute: require('./mute')
silent: require('./silent')
};

View File

@@ -1,27 +1,3 @@
// TODO: take care of trailing ,
function renderHead() {
return '[';
}
function renderTail() {
return ']\n';
}
function renderData(data) {
return stringify(data) + ', ';
}
function renderError(err) {
return stringify(err) + ', ';
}
function renderEnd(data) {
return data ? stringify(data) : '';
}
// -------------------------
function uncolor(str) {
return str.replace(/\x1B\[\d+m/g, '');
}
@@ -32,10 +8,33 @@ function stringify(data) {
// -------------------------
module.exports = {
head: renderHead,
tail: renderTail,
data: renderData,
error: renderError,
end: renderEnd
var nrData = 0;
// In the json output, everything goes to stderr except
// the final command result that goes to stdout.
var json = {
begin: function () {
process.stderr.write('[');
},
end: function (data) {
process.stderr.write(']\n');
if (data) {
process.stdout.write(stringify(data));
}
},
error: function (err) {
this.data(err);
},
data: function (data) {
if (nrData) {
process.stderr.write(', ');
}
process.stderr.write(stringify(data));
nrData++;
},
};
module.exports = json;

View File

@@ -1,13 +0,0 @@
function empty() {
return '';
}
// -------------------------
module.exports = {
head: empty,
tail: empty,
data: empty,
error: empty,
end: empty
};

12
lib/renderers/silent.js Normal file
View File

@@ -0,0 +1,12 @@
function empty() {}
// -------------------------
var silent = {
begin: empty,
end: empty,
error: empty,
data: empty,
};
module.exports = silent;

View File

@@ -25,7 +25,7 @@ function readOptions(argv, options) {
function getRenderer(options) {
if (options.silent) {
return renderers.mute;
return renderers.silent;
}
if (options.json) {
@@ -38,4 +38,4 @@ function getRenderer(options) {
}
module.exports.readOptions = readOptions;
module.exports.createRenderer = getRenderer;
module.exports.getRenderer = getRenderer;