Comparing version 3.0.3 to 3.0.4
var duplicateText = require('./duplicateText'); | ||
var utils = require('./utils'); | ||
var ansiRegex = require('ansi-regex'); | ||
// copied from https://github.com/sindresorhus/ansi-regex | ||
// License https://raw.githubusercontent.com/sindresorhus/ansi-regex/master/license | ||
var ansiRegex = /\u001b\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]/g; | ||
function stripAnsi(text) { | ||
@@ -21,2 +20,3 @@ return text.replace(ansiRegex, ''); | ||
line.forEach(function (outputEntry, blockIndex) { | ||
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry); | ||
var inlineBlock = this[outputEntry.style] ? | ||
@@ -26,19 +26,26 @@ this[outputEntry.style].apply(this, outputEntry.args) : | ||
var blockLines = String(inlineBlock).split('\n').map(function (serializedBlockLine) { | ||
return { | ||
content: serializedBlockLine, | ||
length: stripAnsi(serializedBlockLine).length | ||
}; | ||
}); | ||
var longestBlockLine = 0; | ||
blockLines.forEach(function (blockLine) { | ||
longestBlockLine = Math.max(longestBlockLine, blockLine.length); | ||
}); | ||
var blockLines = String(inlineBlock).split('\n'); | ||
var padLines = blockIndex < line.length - 1; | ||
blockLines.forEach(function (blockLine, index) { | ||
serializedLines[index] = serializedLines[index] || ''; | ||
var padding = duplicateText(' ', startIndex - stripAnsi(serializedLines[index]).length); | ||
serializedLines[index] += padding + blockLine.content; | ||
if (!serializedLines[index]) { | ||
serializedLines[index] = duplicateText(' ', startIndex); | ||
} | ||
var padding = ''; | ||
if (padLines) { | ||
padding = duplicateText(' ', outputEntrySize.width - stripAnsi(blockLine).length); | ||
} | ||
serializedLines[index] += blockLine + padding; | ||
}); | ||
startIndex += longestBlockLine; | ||
if (padLines) { | ||
for (var i = blockLines.length; i < serializedLines.length; i += 1) { | ||
var padding = duplicateText(' ', outputEntrySize.width); | ||
serializedLines[i] += padding; | ||
} | ||
} | ||
startIndex += outputEntrySize.width; | ||
}, this); | ||
@@ -45,0 +52,0 @@ |
@@ -12,15 +12,18 @@ var utils = { | ||
calculateOutputEntrySize: function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
return { width: String(outputEntry.args[0]).length, height: 1 }; | ||
case 'block': | ||
return utils.calculateSize(outputEntry.args[0]); | ||
default: return { width: 0, height: 0 }; | ||
} | ||
}, | ||
calculateLineSize: function (line) { | ||
var size = { height: 1, width: 0 }; | ||
line.forEach(function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
size.width += String(outputEntry.args[0]).length; | ||
break; | ||
case 'block': | ||
var blockSize = utils.calculateSize(outputEntry.args[0]); | ||
size.width += blockSize.width; | ||
size.height = Math.max(blockSize.height, size.height); | ||
break; | ||
} | ||
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry); | ||
size.width += outputEntrySize.width; | ||
size.height = Math.max(outputEntrySize.height, size.height); | ||
}); | ||
@@ -27,0 +30,0 @@ return size; |
218
magicpen.js
@@ -27,5 +27,5 @@ /*! | ||
var TextSerializer = require(4); | ||
var colorDiff = require(11); | ||
var colorDiff = require(12); | ||
var ansiStyles = utils.extend({}, require(7)); | ||
var ansiStyles = utils.extend({}, require(8)); | ||
Object.keys(ansiStyles).forEach(function (styleName) { | ||
@@ -268,3 +268,3 @@ ansiStyles[styleName.toLowerCase()] = ansiStyles[styleName]; | ||
MagicPen.defaultFormat = 'html'; // Browser | ||
} else if (require(13)) { | ||
} else if (require(14)) { | ||
MagicPen.defaultFormat = 'ansi'; // colored console | ||
@@ -639,6 +639,5 @@ } else { | ||
var duplicateText = require(5); | ||
var utils = require(6); | ||
var ansiRegex = require(7); | ||
// copied from https://github.com/sindresorhus/ansi-regex | ||
// License https://raw.githubusercontent.com/sindresorhus/ansi-regex/master/license | ||
var ansiRegex = /\u001b\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]/g; | ||
function stripAnsi(text) { | ||
@@ -659,2 +658,3 @@ return text.replace(ansiRegex, ''); | ||
line.forEach(function (outputEntry, blockIndex) { | ||
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry); | ||
var inlineBlock = this[outputEntry.style] ? | ||
@@ -664,19 +664,26 @@ this[outputEntry.style].apply(this, outputEntry.args) : | ||
var blockLines = String(inlineBlock).split('\n').map(function (serializedBlockLine) { | ||
return { | ||
content: serializedBlockLine, | ||
length: stripAnsi(serializedBlockLine).length | ||
}; | ||
}); | ||
var longestBlockLine = 0; | ||
blockLines.forEach(function (blockLine) { | ||
longestBlockLine = Math.max(longestBlockLine, blockLine.length); | ||
}); | ||
var blockLines = String(inlineBlock).split('\n'); | ||
var padLines = blockIndex < line.length - 1; | ||
blockLines.forEach(function (blockLine, index) { | ||
serializedLines[index] = serializedLines[index] || ''; | ||
var padding = duplicateText(' ', startIndex - stripAnsi(serializedLines[index]).length); | ||
serializedLines[index] += padding + blockLine.content; | ||
if (!serializedLines[index]) { | ||
serializedLines[index] = duplicateText(' ', startIndex); | ||
} | ||
var padding = ''; | ||
if (padLines) { | ||
padding = duplicateText(' ', outputEntrySize.width - stripAnsi(blockLine).length); | ||
} | ||
serializedLines[index] += blockLine + padding; | ||
}); | ||
startIndex += longestBlockLine; | ||
if (padLines) { | ||
for (var i = blockLines.length; i < serializedLines.length; i += 1) { | ||
var padding = duplicateText(' ', outputEntrySize.width); | ||
serializedLines[i] += padding; | ||
} | ||
} | ||
startIndex += outputEntrySize.width; | ||
}, this); | ||
@@ -720,15 +727,18 @@ | ||
calculateOutputEntrySize: function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
return { width: String(outputEntry.args[0]).length, height: 1 }; | ||
case 'block': | ||
return utils.calculateSize(outputEntry.args[0]); | ||
default: return { width: 0, height: 0 }; | ||
} | ||
}, | ||
calculateLineSize: function (line) { | ||
var size = { height: 1, width: 0 }; | ||
line.forEach(function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
size.width += String(outputEntry.args[0]).length; | ||
break; | ||
case 'block': | ||
var blockSize = utils.calculateSize(outputEntry.args[0]); | ||
size.width += blockSize.width; | ||
size.height = Math.max(blockSize.height, size.height); | ||
break; | ||
} | ||
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry); | ||
size.width += outputEntrySize.width; | ||
size.height = Math.max(outputEntrySize.height, size.height); | ||
}); | ||
@@ -776,43 +786,65 @@ return size; | ||
'use strict'; | ||
var styles = module.exports; | ||
module.exports = function () { | ||
return /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/g; | ||
}; | ||
var codes = { | ||
reset: [0, 0], | ||
},{}],8:[function(require,module,exports){ | ||
'use strict'; | ||
bold: [1, 22], // 21 isn't widely supported and 22 does the same thing | ||
dim: [2, 22], | ||
italic: [3, 23], | ||
underline: [4, 24], | ||
inverse: [7, 27], | ||
hidden: [8, 28], | ||
strikethrough: [9, 29], | ||
var styles = module.exports = { | ||
modifiers: { | ||
reset: [0, 0], | ||
bold: [1, 22], // 21 isn't widely supported and 22 does the same thing | ||
dim: [2, 22], | ||
italic: [3, 23], | ||
underline: [4, 24], | ||
inverse: [7, 27], | ||
hidden: [8, 28], | ||
strikethrough: [9, 29] | ||
}, | ||
colors: { | ||
black: [30, 39], | ||
red: [31, 39], | ||
green: [32, 39], | ||
yellow: [33, 39], | ||
blue: [34, 39], | ||
magenta: [35, 39], | ||
cyan: [36, 39], | ||
white: [37, 39], | ||
gray: [90, 39] | ||
}, | ||
bgColors: { | ||
bgBlack: [40, 49], | ||
bgRed: [41, 49], | ||
bgGreen: [42, 49], | ||
bgYellow: [43, 49], | ||
bgBlue: [44, 49], | ||
bgMagenta: [45, 49], | ||
bgCyan: [46, 49], | ||
bgWhite: [47, 49] | ||
} | ||
}; | ||
black: [30, 39], | ||
red: [31, 39], | ||
green: [32, 39], | ||
yellow: [33, 39], | ||
blue: [34, 39], | ||
magenta: [35, 39], | ||
cyan: [36, 39], | ||
white: [37, 39], | ||
gray: [90, 39], | ||
// fix humans | ||
styles.colors.grey = styles.colors.gray; | ||
bgBlack: [40, 49], | ||
bgRed: [41, 49], | ||
bgGreen: [42, 49], | ||
bgYellow: [43, 49], | ||
bgBlue: [44, 49], | ||
bgMagenta: [45, 49], | ||
bgCyan: [46, 49], | ||
bgWhite: [47, 49] | ||
}; | ||
Object.keys(styles).forEach(function (groupName) { | ||
var group = styles[groupName]; | ||
Object.keys(codes).forEach(function (key) { | ||
var val = codes[key]; | ||
var style = styles[key] = {}; | ||
style.open = '\u001b[' + val[0] + 'm'; | ||
style.close = '\u001b[' + val[1] + 'm'; | ||
Object.keys(group).forEach(function (styleName) { | ||
var style = group[styleName]; | ||
styles[styleName] = group[styleName] = { | ||
open: '\u001b[' + style[0] + 'm', | ||
close: '\u001b[' + style[1] + 'm' | ||
}; | ||
}); | ||
Object.defineProperty(styles, groupName, { | ||
value: group, | ||
enumerable: false | ||
}); | ||
}); | ||
},{}],8:[function(require,module,exports){ | ||
},{}],9:[function(require,module,exports){ | ||
// shim for using process in browser | ||
@@ -882,3 +914,3 @@ | ||
},{}],9:[function(require,module,exports){ | ||
},{}],10:[function(require,module,exports){ | ||
/** | ||
@@ -998,3 +1030,3 @@ * @author Markus Näsman | ||
},{}],10:[function(require,module,exports){ | ||
},{}],11:[function(require,module,exports){ | ||
/** | ||
@@ -1165,8 +1197,8 @@ * @author Markus Näsman | ||
},{}],11:[function(require,module,exports){ | ||
},{}],12:[function(require,module,exports){ | ||
'use strict'; | ||
var diff = require(10); | ||
var convert = require(9); | ||
var palette = require(12); | ||
var diff = require(11); | ||
var convert = require(10); | ||
var palette = require(13); | ||
@@ -1183,7 +1215,16 @@ var color = module.exports = {}; | ||
var result = color.map_palette([target], relative); | ||
var result = color.map_palette([target], relative, 'closest'); | ||
return result[key]; | ||
}; | ||
},{}],12:[function(require,module,exports){ | ||
color.furthest = function(target, relative) { | ||
var key = color.palette_map_key(target); | ||
var result = color.map_palette([target], relative, 'furthest'); | ||
return result[key]; | ||
}; | ||
},{}],13:[function(require,module,exports){ | ||
/** | ||
@@ -1226,4 +1267,4 @@ * @author Markus Näsman | ||
*/ | ||
var color_diff = require(10); | ||
var color_convert = require(9); | ||
var color_diff = require(11); | ||
var color_convert = require(10); | ||
@@ -1248,7 +1289,9 @@ /** | ||
* @param [{rgbcolor}] b each element should have fields R,G,B | ||
* @param 'type' should have field closest or furthest | ||
* @return {palettemap} | ||
*/ | ||
function map_palette(a, b) | ||
function map_palette(a, b, type) | ||
{ | ||
var c = {}; | ||
type = type || 'closest'; | ||
for (var idx1 in a){ | ||
@@ -1262,7 +1305,15 @@ var color1 = a[idx1]; | ||
var current_color_diff = diff(color1,color2); | ||
if((best_color == undefined) || (current_color_diff < best_color_diff)) | ||
if((best_color == undefined) || ((type === 'closest') && (current_color_diff < best_color_diff))) | ||
{ | ||
best_color = color2; | ||
best_color_diff = current_color_diff; | ||
continue; | ||
} | ||
if((type === 'furthest') && (current_color_diff > best_color_diff)) | ||
{ | ||
best_color = color2; | ||
best_color_diff = current_color_diff; | ||
continue; | ||
} | ||
} | ||
@@ -1290,11 +1341,18 @@ c[palette_map_key(color1)] = best_color; | ||
},{}],13:[function(require,module,exports){ | ||
},{}],14:[function(require,module,exports){ | ||
(function (process){ | ||
'use strict'; | ||
var argv = process.argv; | ||
module.exports = (function () { | ||
if (process.argv.indexOf('--no-color') !== -1) { | ||
if (argv.indexOf('--no-color') !== -1 || | ||
argv.indexOf('--no-colors') !== -1 || | ||
argv.indexOf('--color=false') !== -1) { | ||
return false; | ||
} | ||
if (process.argv.indexOf('--color') !== -1) { | ||
if (argv.indexOf('--color') !== -1 || | ||
argv.indexOf('--colors') !== -1 || | ||
argv.indexOf('--color=true') !== -1 || | ||
argv.indexOf('--color=always') !== -1) { | ||
return true; | ||
@@ -1326,4 +1384,4 @@ } | ||
}).call(this,require(8)) | ||
}).call(this,require(9)) | ||
},{}]},{},[3])(3) | ||
}); |
{ | ||
"name": "magicpen", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -34,3 +34,4 @@ "main": "./lib/MagicPen.js", | ||
"dependencies": { | ||
"ansi-styles": "^1.1.0", | ||
"ansi-regex": "^1.1.0", | ||
"ansi-styles": "^2.0.0", | ||
"color-diff": "^0.1.4", | ||
@@ -37,0 +38,0 @@ "supports-color": "^1.2.0" |
@@ -130,2 +130,36 @@ /*global describe, it, beforeEach*/ | ||
}); | ||
it('can be nested abitrarily', function () { | ||
pen.text('level 1').nl().text('=======').block(function () { | ||
this.text('level 2').nl().text('=======').block(function () { | ||
this.text('level 3').nl().text('=======').block(function () { | ||
this.text('level 4').nl().text('======='); | ||
}); | ||
}); | ||
}); | ||
expect(pen.toString(), 'to equal', | ||
'level 1\n' + | ||
'=======level 2\n' + | ||
' =======level 3\n' + | ||
' =======level 4\n' + | ||
' ======='); | ||
}); | ||
it('can be appended after each other', function () { | ||
var spaceships = pen.clone() | ||
.text('>').nl() | ||
.text(' >').nl() | ||
.text(' >').nl() | ||
.text(' >').nl() | ||
.text('>'); | ||
pen.block(spaceships).sp().block(spaceships).sp().block(spaceships).sp().block(spaceships); | ||
expect(pen.toString(), 'to equal', | ||
'> > > >\n' + | ||
' > > > >\n' + | ||
' > > > >\n' + | ||
' > > > >\n' + | ||
'> > > >'); | ||
}); | ||
}); | ||
@@ -298,3 +332,3 @@ | ||
it.skip('works with blocks', function () { | ||
it('works with blocks', function () { | ||
pen.block(function () { | ||
@@ -301,0 +335,0 @@ this.text('Hello\nworld'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
465451
2635
4
+ Addedansi-regex@^1.1.0
+ Addedansi-regex@1.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
Updatedansi-styles@^2.0.0