Merge pull request #223 from Marak/develop

v1.2.2 Release
This commit is contained in:
DABH 2018-04-30 11:41:50 -07:00 committed by GitHub
commit 97cc55e8ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**/*.sw*

View File

@ -7,10 +7,10 @@ Here we describe upcoming releases and the key features/fixes they include. Don
### 1.3.0 ### 1.3.0
* Support custom colors * Support custom colors
### 1.2.1 ### 1.2.2
* Refactor tests to use a testing library like jest (only affects dev/testing) * Refactor tests to use a testing library like jest (only affects dev/testing)
### 1.2.0 (release date: about 3/5/18, barring any new issues) ### ~~1.2.0 (release date: about 3/5/18, barring any new issues)~~
* Built-in Typescript definitions * ~~Built-in Typescript definitions~~
* Key bug fixes for webpack/bundlers, webstorm, etc. * ~~Key bug fixes for webpack/bundlers, webstorm, etc.~~

2
index.d.ts vendored
View File

@ -46,6 +46,8 @@ export interface Color {
zalgo: Color; zalgo: Color;
} }
export function enable(): void;
export function disable(): void;
export function setTheme(theme: any): void; export function setTheme(theme: any): void;
export let enabled: boolean; export let enabled: boolean;

View File

@ -2,7 +2,7 @@
The MIT License (MIT) The MIT License (MIT)
Original Library Original Library
- Copyright (c) Marak Squires - Copyright (c) Marak Squires
Additional functionality Additional functionality
@ -33,8 +33,10 @@ module['exports'] = colors;
colors.themes = {}; colors.themes = {};
var util = require('util');
var ansiStyles = colors.styles = require('./styles'); var ansiStyles = colors.styles = require('./styles');
var defineProps = Object.defineProperties; var defineProps = Object.defineProperties;
var newLineRegex = new RegExp(/[\r\n]+/g);
colors.supportsColor = require('./system/supports-colors').supportsColor; colors.supportsColor = require('./system/supports-colors').supportsColor;
@ -42,6 +44,14 @@ if (typeof colors.enabled === "undefined") {
colors.enabled = colors.supportsColor() !== false; colors.enabled = colors.supportsColor() !== false;
} }
colors.enable = function () {
colors.enabled = true;
};
colors.disable = function () {
colors.enabled = false;
};
colors.stripColors = colors.strip = function(str){ colors.stripColors = colors.strip = function(str){
return ("" + str).replace(/\x1B\[\d+m/g, ''); return ("" + str).replace(/\x1B\[\d+m/g, '');
}; };
@ -91,25 +101,27 @@ var styles = (function () {
var proto = defineProps(function colors() {}, styles); var proto = defineProps(function colors() {}, styles);
function applyStyle() { function applyStyle() {
var args = arguments; var args = Array.prototype.slice.call(arguments);
var argsLen = args.length;
var str = argsLen !== 0 && String(arguments[0]); var str = args.map(function(arg) {
if (argsLen > 1) { return typeof arg === 'object' ? util.inspect(arg) : arg;
for (var a = 1; a < argsLen; a++) { }).join(' ');
str += ' ' + args[a];
}
}
if (!colors.enabled || !str) { if (!colors.enabled || !str) {
return str; return str;
} }
var newLinesPresent = str.indexOf('\n') != -1;
var nestedStyles = this._styles; var nestedStyles = this._styles;
var i = nestedStyles.length; var i = nestedStyles.length;
while (i--) { while (i--) {
var code = ansiStyles[nestedStyles[i]]; var code = ansiStyles[nestedStyles[i]];
str = code.open + str.replace(code.closeRe, code.open) + code.close; str = code.open + str.replace(code.closeRe, code.open) + code.close;
if (newLinesPresent){
str = str.replace(newLineRegex, code.close + '\n' + code.open);
}
} }
return str; return str;

View File

@ -9,14 +9,6 @@ module['exports'] = function () {
String.prototype.__defineGetter__(color, func); String.prototype.__defineGetter__(color, func);
}; };
var sequencer = function sequencer (map, str) {
return function () {
var exploded = this.split(""), i = 0;
exploded = exploded.map(map);
return exploded.join("");
}
};
addProperty('strip', function () { addProperty('strip', function () {
return colors.strip(this); return colors.strip(this);
}); });
@ -67,7 +59,7 @@ module['exports'] = function () {
var stringPrototypeBlacklist = [ var stringPrototypeBlacklist = [
'__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor', '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'repeat', 'replace', 'search', 'slice', 'split', 'substring', 'indexOf', 'lastIndexOf', 'length', 'localeCompare', 'match', 'repeat', 'replace', 'search', 'slice', 'split', 'substring',
'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight' 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
]; ];

View File

@ -1,7 +1,7 @@
{ {
"name": "colors", "name": "colors",
"description": "get colors in your node.js console", "description": "get colors in your node.js console",
"version": "1.2.1", "version": "1.2.2",
"author": "Marak Squires", "author": "Marak Squires",
"homepage": "https://github.com/Marak/colors.js", "homepage": "https://github.com/Marak/colors.js",
"bugs": "https://github.com/Marak/colors.js/issues", "bugs": "https://github.com/Marak/colors.js/issues",

3
safe.d.ts vendored
View File

@ -4,6 +4,9 @@
// Definitions: https://github.com/Marak/colors.js // Definitions: https://github.com/Marak/colors.js
export const enabled: boolean; export const enabled: boolean;
export function enable(): void;
export function disable(): void;
export function setTheme(theme: any): void;
export function strip(str: string): string; export function strip(str: string): string;
export function stripColors(str: string): string; export function stripColors(str: string): string;

View File

@ -43,8 +43,19 @@ aE(s, 'yellow', 33);
assert.equal(s, 'string'); assert.equal(s, 'string');
colors.setTheme({error:'red'}); var testStringWithNewLines = s + '\n' + s;
assert.equal(typeof("astring".red),'string'); // single style
assert.equal(typeof("astring".error),'string'); assert.equal(testStringWithNewLines.red, '\x1b[31m' + s + '\n' + s + '\x1b[39m');
var testStringWithNewLinesStyled = s.underline + '\n' + s.bold;
// nested styles
assert.equal(testStringWithNewLinesStyled.red, '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\n' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m');
colors.setTheme({ error: 'red' });
assert.equal(typeof ("astring".red), 'string');
assert.equal(typeof ("astring".error), 'string');
assert.equal(s, 'string');

View File

@ -39,7 +39,19 @@ aE(s, 'red', 31);
aE(s, 'yellow', 33); aE(s, 'yellow', 33);
assert.equal(s, 'string'); assert.equal(s, 'string');
colors.setTheme({error:'red'});
assert.equal(typeof(colors.red("astring")), 'string'); var testStringWithNewLines = s + '\n' + s;
assert.equal(typeof(colors.error("astring")), 'string');
// single style
assert.equal(colors.red(testStringWithNewLines), '\x1b[31m' + s + '\x1b[39m' + '\n' + '\x1b[31m' + s + '\x1b[39m');
var testStringWithNewLinesStyled = colors.underline(s) + '\n' + colors.bold(s);
// nested styles
assert.equal(colors.red(testStringWithNewLinesStyled), '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\x1b[39m' + '\n' + '\x1b[31m' + '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m');
colors.setTheme({ error: 'red' });
assert.equal(typeof (colors.red("astring")), 'string');
assert.equal(typeof (colors.error("astring")), 'string');