gulp-messenger
Version:
Console/Gulp and File Logger
130 lines (104 loc) • 3.71 kB
JavaScript
/* jshint -W040 */
/* jshint -W003 */
/* jshint -W103 */
;
var escapeStringRegexp = require('escape-string-regexp');
var defineProps = Object.defineProperties;
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
var ansiStyles = require('ansi-styles');
var chalk = require('chalk');
var block = '\u2588';
var columns = 80;
var defOptions = {};
if (process.stdout.isTTY && process.stdout.columns) {
columns = process.stdout.columns;
}
var str = new Array(columns + 1).join(block);
var styles = (function () {
var ret = {};
Object.keys(ansiStyles).forEach(function (key) {
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
ret[key] = {
get: function () {
return build.call(this, this._styles.concat(key));
}
};
});
return ret;
})();
var proto = defineProps(function chalk() {}, styles);
function Chalkline(options) {
this.options = function (options) {
if (typeof (options) === 'object') {
defOptions = options;
if (options.hasOwnProperty('block')) {
block = defOptions.block;
}
}
};
this.enabled = !options || options.enabled === undefined ? chalk.supportsColor : options.enabled;
}
function init() {
var chalkline = {};
Object.keys(chalk.styles).forEach(function (name) {
chalkline[name] = {
get: function () {
return build.call(this, [name]);
}
};
});
return chalkline;
}
function applyStyle() {
// support varags, but simply cast to string in case there's only one arg
var args = arguments;
var argsLen = args.length;
var str = argsLen !== 0 && String(arguments[0]);
if (argsLen > 1) {
// don't slice `arguments`, it prevents v8 optimizations
for (var a = 1; a < argsLen; a++) {
str += ' ' + args[a];
}
}
if (!this.enabled || !str) {
return str;
}
var nestedStyles = this._styles;
var i = nestedStyles.length;
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
// see https://github.com/chalk/chalk/issues/58
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
var originalDim = ansiStyles.dim.open;
if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
ansiStyles.dim.open = '';
}
while (i--) {
var code = ansiStyles[nestedStyles[i]];
// Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code
// will be colored, and the rest will simply be 'plain'.
str = code.open + str.replace(code.closeRe, code.open) + code.close;
}
// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
ansiStyles.dim.open = originalDim;
return str;
}
function build(_styles) {
// This is where the magic happens by using `.call(builder, str)`
// instead of calling `applyStyle.apply(builder, arguments)`
// like `chalk` does, we simply pass it the argument of str
// maybe we can rewrite this a better way! PR's welcome
var builder = function () {
var str = new Array(columns + 1).join(block);
return console.log(applyStyle.call(builder, str));
};
builder._styles = _styles;
builder.enabled = this.enabled;
// __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype.
/* eslint-disable no-proto */
builder.__proto__ = proto;
return builder;
}
defineProps(Chalkline.prototype, init());
module.exports = new Chalkline();