mirror of
https://github.com/flynx/colors.js.git
synced 2025-10-29 19:10:10 +00:00
Merge pull request #220 from streaka/master
Add multiline handling functionality
This commit is contained in:
commit
c13ff984bb
@ -36,6 +36,7 @@ colors.themes = {};
|
|||||||
var util = require('util');
|
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;
|
||||||
|
|
||||||
@ -110,12 +111,17 @@ function applyStyle() {
|
|||||||
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;
|
||||||
|
|||||||
@ -43,8 +43,19 @@ aE(s, 'yellow', 33);
|
|||||||
|
|
||||||
assert.equal(s, 'string');
|
assert.equal(s, 'string');
|
||||||
|
|
||||||
|
var testStringWithNewLines = s + '\n' + s;
|
||||||
|
|
||||||
|
// 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' });
|
colors.setTheme({ error: 'red' });
|
||||||
|
|
||||||
assert.equal(typeof ("astring".red), 'string');
|
assert.equal(typeof ("astring".red), 'string');
|
||||||
assert.equal(typeof ("astring".error), 'string');
|
assert.equal(typeof ("astring".error), 'string');
|
||||||
|
|
||||||
|
assert.equal(s, 'string');
|
||||||
|
|||||||
@ -39,6 +39,17 @@ aE(s, 'red', 31);
|
|||||||
aE(s, 'yellow', 33);
|
aE(s, 'yellow', 33);
|
||||||
|
|
||||||
assert.equal(s, 'string');
|
assert.equal(s, 'string');
|
||||||
|
|
||||||
|
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' });
|
colors.setTheme({ error: 'red' });
|
||||||
|
|
||||||
assert.equal(typeof (colors.red("astring")), 'string');
|
assert.equal(typeof (colors.red("astring")), 'string');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user