diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78fd378 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.sw* diff --git a/ROADMAP.md b/ROADMAP.md index e8f0816..6ae0743 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -7,10 +7,10 @@ Here we describe upcoming releases and the key features/fixes they include. Don ### 1.3.0 * Support custom colors -### 1.2.1 +### 1.2.2 * 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) - * Built-in Typescript definitions - * Key bug fixes for webpack/bundlers, webstorm, etc. +### ~~1.2.0 (release date: about 3/5/18, barring any new issues)~~ + * ~~Built-in Typescript definitions~~ + * ~~Key bug fixes for webpack/bundlers, webstorm, etc.~~ diff --git a/index.d.ts b/index.d.ts index db72fab..faa9ab6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -46,6 +46,8 @@ export interface Color { zalgo: Color; } +export function enable(): void; +export function disable(): void; export function setTheme(theme: any): void; export let enabled: boolean; diff --git a/lib/colors.js b/lib/colors.js index 1fb08b5..fc9cc3d 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -2,7 +2,7 @@ The MIT License (MIT) -Original Library +Original Library - Copyright (c) Marak Squires Additional functionality @@ -33,8 +33,10 @@ module['exports'] = colors; colors.themes = {}; +var util = require('util'); var ansiStyles = colors.styles = require('./styles'); var defineProps = Object.defineProperties; +var newLineRegex = new RegExp(/[\r\n]+/g); colors.supportsColor = require('./system/supports-colors').supportsColor; @@ -42,6 +44,14 @@ if (typeof colors.enabled === "undefined") { colors.enabled = colors.supportsColor() !== false; } +colors.enable = function () { + colors.enabled = true; +}; + +colors.disable = function () { + colors.enabled = false; +}; + colors.stripColors = colors.strip = function(str){ return ("" + str).replace(/\x1B\[\d+m/g, ''); }; @@ -91,25 +101,27 @@ var styles = (function () { var proto = defineProps(function colors() {}, styles); function applyStyle() { - var args = arguments; - var argsLen = args.length; - var str = argsLen !== 0 && String(arguments[0]); - if (argsLen > 1) { - for (var a = 1; a < argsLen; a++) { - str += ' ' + args[a]; - } - } + var args = Array.prototype.slice.call(arguments); + + var str = args.map(function(arg) { + return typeof arg === 'object' ? util.inspect(arg) : arg; + }).join(' '); if (!colors.enabled || !str) { return str; } + var newLinesPresent = str.indexOf('\n') != -1; + var nestedStyles = this._styles; var i = nestedStyles.length; while (i--) { var code = ansiStyles[nestedStyles[i]]; str = code.open + str.replace(code.closeRe, code.open) + code.close; + if (newLinesPresent){ + str = str.replace(newLineRegex, code.close + '\n' + code.open); + } } return str; diff --git a/lib/extendStringPrototype.js b/lib/extendStringPrototype.js index d6a3f63..dbd6c11 100644 --- a/lib/extendStringPrototype.js +++ b/lib/extendStringPrototype.js @@ -9,14 +9,6 @@ module['exports'] = function () { 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 () { return colors.strip(this); }); @@ -67,7 +59,7 @@ module['exports'] = function () { var stringPrototypeBlacklist = [ '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor', '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' ]; diff --git a/package.json b/package.json index a24614a..7c39b8b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "colors", "description": "get colors in your node.js console", - "version": "1.2.1", + "version": "1.2.2", "author": "Marak Squires", "homepage": "https://github.com/Marak/colors.js", "bugs": "https://github.com/Marak/colors.js/issues", diff --git a/safe.d.ts b/safe.d.ts index 87f5ef2..2bafc27 100644 --- a/safe.d.ts +++ b/safe.d.ts @@ -4,6 +4,9 @@ // Definitions: https://github.com/Marak/colors.js 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 stripColors(str: string): string; diff --git a/tests/basic-test.js b/tests/basic-test.js index fda8af4..9c17ed4 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -43,8 +43,19 @@ aE(s, 'yellow', 33); assert.equal(s, 'string'); -colors.setTheme({error:'red'}); +var testStringWithNewLines = s + '\n' + s; -assert.equal(typeof("astring".red),'string'); -assert.equal(typeof("astring".error),'string'); +// single style +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'); diff --git a/tests/safe-test.js b/tests/safe-test.js index daad4f9..c8388f3 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -39,7 +39,19 @@ aE(s, 'red', 31); aE(s, 'yellow', 33); assert.equal(s, 'string'); -colors.setTheme({error:'red'}); -assert.equal(typeof(colors.red("astring")), 'string'); -assert.equal(typeof(colors.error("astring")), 'string'); \ No newline at end of file +var testStringWithNewLines = s + '\n' + s; + +// 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'); +