chalk/chalk.js

63 lines
1.1 KiB
JavaScript
Raw Normal View History

2013-08-03 02:16:26 +02:00
'use strict';
var ansi = require('ansi-styles');
var stripAnsi = require('strip-ansi');
var hasColor = require('has-color');
2013-08-03 02:16:26 +02:00
var defineProps = Object.defineProperties;
2013-08-29 16:14:18 +02:00
ansi.grey = ansi.gray;
2013-08-03 02:16:26 +02:00
var styles = (function () {
var ret = {};
Object.keys(ansi).forEach(function (key) {
ret[key] = {
get: function () {
2013-08-03 18:47:47 +02:00
this._styles.push(key);
2013-08-03 02:16:26 +02:00
return this;
}
};
});
return ret;
})();
var chalk = module.exports = defineProps({}, init());
function init() {
var ret = {};
Object.keys(styles).forEach(function (name) {
ret[name] = {
get: function () {
var obj = defineProps(function self() {
var str = [].slice.call(arguments).join(' ');
2013-08-03 02:16:26 +02:00
if (!chalk.enabled) {
return str;
}
2013-08-03 18:47:47 +02:00
return self._styles.reduce(function (str, name) {
var code = ansi[name];
return str ? code.open + str + code.close : '';
2013-08-03 18:47:47 +02:00
}, str);
2013-08-03 02:16:26 +02:00
}, styles);
obj._styles = [];
return obj[name];
}
}
});
return ret;
}
chalk.styles = ansi;
chalk.stripColor = stripAnsi;
chalk.supportsColor = hasColor;
2013-08-03 02:16:26 +02:00
// detect mode if not set manually
if (chalk.enabled === undefined) {
chalk.enabled = chalk.supportsColor;
}