Comparing version 0.1.2 to 0.2.0
1289
magicpen.js
@@ -1,644 +0,669 @@ | ||
(function () { | ||
// Copyright (c) 2014 Sune Simonsen <sune@we-knowhow.dk> | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the 'Software'), to deal in the Software without | ||
// restriction, including without limitation the rights to use, copy, | ||
// modify, merge, publish, distribute, sublicense, and/or sell copies | ||
// of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
var namespace = {}; | ||
(function () { | ||
namespace.shim = { | ||
getKeys: function (obj) { | ||
var result = []; | ||
for (var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
result.push(i); | ||
} | ||
} | ||
return result; | ||
}, | ||
forEach: function (arr, callback, that) { | ||
for (var i = 0, n = arr.length; i < n; i += 1) | ||
if (i in arr) | ||
callback.call(that, arr[i], i, arr); | ||
}, | ||
map: function (arr, mapper, that) { | ||
var other = new Array(arr.length); | ||
for (var i = 0, n = arr.length; i < n; i += 1) | ||
if (i in arr) | ||
other[i] = mapper.call(that, arr[i], i, arr); | ||
return other; | ||
}, | ||
filter: function (arr, predicate) { | ||
var length = +arr.length; | ||
var result = []; | ||
if (typeof predicate !== "function") | ||
throw new TypeError(); | ||
for (var i = 0; i < length; i += 1) { | ||
var value = arr[i]; | ||
if (predicate(value)) { | ||
result.push(value); | ||
} | ||
} | ||
return result; | ||
/*! | ||
* Copyright (c) 2014 Sune Simonsen <sune@we-knowhow.dk> | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the 'Software'), to deal in the Software without | ||
* restriction, including without limitation the rights to use, copy, | ||
* modify, merge, publish, distribute, sublicense, and/or sell copies | ||
* of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),(o.weknowhow||(o.weknowhow={})).MagicPen=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var TextSerializer = require(4); | ||
var forEach = require(7).forEach; | ||
var ansiStyles = require(9); | ||
function AnsiSerializer() {} | ||
AnsiSerializer.prototype = new TextSerializer(); | ||
AnsiSerializer.prototype.text = function (content) { | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = stylesString.split(/\s*,\s*/); | ||
forEach(styles, function (style) { | ||
if (ansiStyles[style]) { | ||
content = ansiStyles[style].open + content + ansiStyles[style].close; | ||
} | ||
}; | ||
}()); | ||
(function () { | ||
namespace.shim = namespace.shim || {}; | ||
var shim = namespace.shim; | ||
var prototypes = { | ||
forEach: Array.prototype.forEach, | ||
map: Array.prototype.map, | ||
filter: Array.prototype.filter | ||
}; | ||
function createShimMethod(key) { | ||
shim[key] = function (obj) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return prototypes[key].apply(obj, args); | ||
}; | ||
}); | ||
} | ||
return content; | ||
}; | ||
module.exports = AnsiSerializer; | ||
},{}],2:[function(require,module,exports){ | ||
var shim = require(7); | ||
var forEach = shim.forEach; | ||
var map = shim.map; | ||
var filter = shim.filter; | ||
var htmlStyles = { | ||
bold: 'font-weight: bold', | ||
dim: 'opacity: 0.7', | ||
italic: 'font-style: italic', | ||
underline: 'text-decoration: underline', | ||
inverse: '-webkit-filter: invert(%100); filter: invert(100%)', | ||
hidden: 'visibility: hidden', | ||
strikeThrough: 'text-decoration: line-through', | ||
black: 'color: black', | ||
red: 'color: red', | ||
green: 'color: green', | ||
yellow: 'color: yellow', | ||
blue: 'color: blue', | ||
magenta: 'color: magenta', | ||
cyan: 'color: cyan', | ||
white: 'color: white', | ||
gray: 'color: gray', | ||
bgBlack: 'background-color: black', | ||
bgRed: 'background-color: red', | ||
bgGreen: 'background-color: green', | ||
bgYellow: 'background-color: yellow', | ||
bgBlue: 'background-color: blue', | ||
bgMagenta: 'background-color: magenta', | ||
bgCyan: 'background-color: cyan', | ||
bgWhite: 'background-color: white' | ||
}; | ||
function HtmlSerializer() {} | ||
HtmlSerializer.prototype.serialize = function (lines) { | ||
return '<code>\n' + this.serializeLines(lines) + '\n</code>'; | ||
}; | ||
HtmlSerializer.prototype.serializeLines = function (lines) { | ||
return map(lines, function (line) { | ||
return ' <div>' + this.serializeLine(line).join('') + '</div>'; | ||
}, this).join('\n'); | ||
}; | ||
HtmlSerializer.prototype.serializeLine = function (line) { | ||
return map(line, function (outputEntry) { | ||
return this[outputEntry.style] ? | ||
this[outputEntry.style].apply(this, outputEntry.args) : | ||
''; | ||
}, this); | ||
}; | ||
HtmlSerializer.prototype.block = function (content) { | ||
return '<div style="display: inline-block; vertical-align: top">' + | ||
this.serialize(content) + | ||
'</div>'; | ||
}; | ||
HtmlSerializer.prototype.text = function (content) { | ||
content = String(content) | ||
.replace(/&/g, '&') | ||
.replace(/ /g, ' ') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"'); | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = filter(stylesString.split(/\s*,\s*/), function (styleName) { | ||
return htmlStyles[styleName]; | ||
}); | ||
content = '<span style="' + map(styles, function (styleName) { | ||
return htmlStyles[styleName]; | ||
}).join('; ') + '">' + content + '</span>'; | ||
} | ||
return content; | ||
}; | ||
module.exports = HtmlSerializer; | ||
},{}],3:[function(require,module,exports){ | ||
var shim = require(7); | ||
var map = shim.map; | ||
var forEach = shim.forEach; | ||
var utils = require(8); | ||
var extend = utils.extend; | ||
var duplicateText = require(5); | ||
function isOutputEntry(obj) { | ||
return typeof obj === 'object' && 'style' in obj && 'args' in obj; | ||
} | ||
function MagicPen(options) { | ||
if (!(this instanceof MagicPen)) { | ||
return new MagicPen(options); | ||
} | ||
options = options || {}; | ||
var indentationWidth = 'indentationWidth' in options ? | ||
options.indentationWidth : 2; | ||
this.indentationWidth = Math.max(indentationWidth, 0); | ||
this.indentationLevel = 0; | ||
this.output = []; | ||
this.styles = {}; | ||
} | ||
if (typeof this.window !== 'undefined' && typeof this.window.navigator !== 'undefined') { | ||
MagicPen.defaultFormat = 'html'; // Browser | ||
} else if (require(11)) { | ||
MagicPen.defaultFormat = 'ansi'; // colored console | ||
} else { | ||
MagicPen.defaultFormat = 'text'; // Plain text | ||
} | ||
MagicPen.prototype.newline = MagicPen.prototype.nl = function (count) { | ||
if (typeof count === 'undefined') { | ||
count = 1; | ||
} | ||
if (count === 0) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
for (var i = 0; i < count; i += 1) { | ||
this.output.push([]); | ||
} | ||
return this; | ||
}; | ||
MagicPen.serializers = { | ||
text: require(4), | ||
html: require(2), | ||
ansi: require(1) | ||
}; | ||
MagicPen.prototype.write = function () { | ||
if (arguments.length === 1 && isOutputEntry(arguments[0])) { | ||
var options = arguments[0]; | ||
if (this.styles[options.style]) { | ||
this.styles[options.style].apply(this, options.args); | ||
return this; | ||
} | ||
for (var key in prototypes) { | ||
if (prototypes.hasOwnProperty(key) && prototypes[key]) { | ||
createShimMethod(key); | ||
} | ||
this.output[0] = this.output[0] || []; | ||
this.output[this.output.length - 1].push(options); | ||
return this; | ||
} else { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} | ||
}; | ||
MagicPen.prototype.indentLines = function () { | ||
this.indentationLevel += 1; | ||
return this; | ||
}; | ||
MagicPen.prototype.indent = MagicPen.prototype.i = function () { | ||
for (var i = 0; i < this.indentationLevel; i += 1) { | ||
this.space(this.indentationWidth); | ||
} | ||
return this; | ||
}; | ||
MagicPen.prototype.outdentLines = function () { | ||
this.indentationLevel = Math.max(0, this.indentationLevel - 1); | ||
return this; | ||
}; | ||
MagicPen.prototype.addStyle = function (style, handler) { | ||
if (this[style]) { | ||
throw new Error('"' + style + '" style is already defined'); | ||
} | ||
this.styles[style] = handler; | ||
this[style] = function () { | ||
handler.apply(this, arguments); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
MagicPen.prototype.toString = function (format) { | ||
format = format || 'text'; | ||
if (format === 'auto') { | ||
format = MagicPen.defaultFormat(); | ||
} | ||
var serializer = new MagicPen.serializers[format](); | ||
return serializer.serialize(this.output); | ||
}; | ||
MagicPen.prototype.text = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
return this.write({ style: 'text', args: args }); | ||
}; | ||
MagicPen.prototype.block = function (pen) { | ||
var blockOutput = map(pen.output, function (line) { | ||
return [].concat(line); | ||
}); | ||
return this.write('block', blockOutput); | ||
}; | ||
MagicPen.prototype.append = function (pen) { | ||
if (pen.output.length === 0) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
Array.prototype.push.apply(lastLine, pen.output[0]); | ||
this.output.push.apply(this.output, pen.output.slice(1)); | ||
return this; | ||
}; | ||
MagicPen.prototype.prependLinesWith = function (pen) { | ||
if (pen.output.length === 0 || this.output.length === 0) { | ||
return this; | ||
} | ||
if (pen.output.length > 1) { | ||
throw new Error('PrependLinesWith only supports a pen with single line content'); | ||
} | ||
var outputToPrepend = [].concat(pen.output[0]); | ||
this.output = map(this.output, function (line) { | ||
return outputToPrepend.concat(line); | ||
}); | ||
return this; | ||
}; | ||
MagicPen.prototype.space = MagicPen.prototype.sp = function (count) { | ||
if (count === 0) { | ||
return this; | ||
} | ||
if (typeof count === 'undefined') { | ||
count = 1; | ||
} | ||
this.text(duplicateText(' ', count)); | ||
return this; | ||
}; | ||
forEach([ | ||
'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden', | ||
'strikeThrough', 'black', 'red', 'green', 'yellow', 'blue', | ||
'magenta', 'cyan', 'white', 'gray', 'bgBlack', 'bgRed', | ||
'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan', | ||
'bgWhite' | ||
], function (textStyle) { | ||
MagicPen.prototype[textStyle] = function (content) { | ||
return this.text.call(this, content, textStyle); | ||
}; | ||
}); | ||
MagicPen.prototype.clone = function () { | ||
function MagicPenClone() {} | ||
MagicPenClone.prototype = this; | ||
var clonedPen = new MagicPenClone(); | ||
clonedPen.styles = extend({}, this.styles); | ||
clonedPen.indentationLevel = 0; | ||
clonedPen.output = []; | ||
return clonedPen; | ||
}; | ||
function callculateLineSize(line) { | ||
var size = { height: 1, width: 0 }; | ||
forEach(line, function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
size.width += String(outputEntry.args[0]).length; | ||
break; | ||
case 'block': | ||
var blockSize = calculateSize(outputEntry.args[0]); | ||
size.width += blockSize.width; | ||
size.height = Math.max(blockSize.height, size.height); | ||
break; | ||
} | ||
if (Object.keys) { | ||
shim['getKeys'] = Object.keys; | ||
} | ||
}()); | ||
(function () { | ||
var shim = namespace.shim; | ||
var forEach = shim.forEach; | ||
var getKeys = shim.getKeys; | ||
var utils = { | ||
extend: function (target) { | ||
var sources = Array.prototype.slice.call(arguments, 1); | ||
forEach(sources, function (source) { | ||
forEach(getKeys(source), function (key) { | ||
target[key] = source[key]; | ||
}); | ||
}); | ||
return target; | ||
}); | ||
return size; | ||
} | ||
function calculateSize(lines) { | ||
var size = { height: 0, width: 0 }; | ||
forEach(lines, function (line) { | ||
var lineSize = callculateLineSize(line); | ||
size.height += lineSize.height; | ||
size.width = Math.max(size.width, lineSize.width); | ||
}); | ||
return size; | ||
} | ||
MagicPen.prototype.size = function () { | ||
return calculateSize(this.output); | ||
}; | ||
module.exports = MagicPen; | ||
},{}],4:[function(require,module,exports){ | ||
var shim = require(7); | ||
var map = shim.map; | ||
var forEach = shim.forEach; | ||
var duplicateText = require(5); | ||
// 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) { | ||
return text.replace(ansiRegex, ''); | ||
} | ||
function TextSerializer() {} | ||
TextSerializer.prototype.serialize = function (lines) { | ||
return map(lines, this.serializeLine, this).join('\n'); | ||
}; | ||
TextSerializer.prototype.serializeLine = function (line) { | ||
var serializedLines = ['']; | ||
var startIndex = 0; | ||
forEach(line, function (outputEntry, blockIndex) { | ||
var inlineBlock = this[outputEntry.style] ? | ||
this[outputEntry.style].apply(this, outputEntry.args) : | ||
''; | ||
var blockLines = map(String(inlineBlock).split('\n'), function (serializedBlockLine) { | ||
return { | ||
content: serializedBlockLine, | ||
length: stripAnsi(serializedBlockLine).length | ||
}; | ||
}); | ||
var longestBlockLine = 0; | ||
forEach(blockLines, function (blockLine) { | ||
longestBlockLine = Math.max(longestBlockLine, blockLine.length); | ||
}); | ||
forEach(blockLines, function (blockLine, index) { | ||
serializedLines[index] = serializedLines[index] || ''; | ||
var padding = duplicateText(' ', startIndex - stripAnsi(serializedLines[index]).length); | ||
serializedLines[index] += padding + blockLine.content; | ||
}); | ||
startIndex += longestBlockLine; | ||
}, this); | ||
return serializedLines.join('\n'); | ||
}; | ||
TextSerializer.prototype.text = function (content) { | ||
return content; | ||
}; | ||
TextSerializer.prototype.block = function (content) { | ||
return this.serialize(content); | ||
}; | ||
module.exports = TextSerializer; | ||
},{}],5:[function(require,module,exports){ | ||
function duplicateText(content, times) { | ||
var result = ''; | ||
for (var i = 0; i < times; i += 1) { | ||
result += content; | ||
} | ||
return result; | ||
} | ||
module.exports = duplicateText; | ||
},{}],6:[function(require,module,exports){ | ||
module.exports = { | ||
getKeys: function (obj) { | ||
var result = []; | ||
for (var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
result.push(i); | ||
} | ||
}; | ||
namespace.utils = utils; | ||
}()); | ||
(function () { | ||
var global = this; | ||
function supportsColors() { | ||
// Copied from https://github.com/sindresorhus/supports-color/ | ||
// License: https://raw.githubusercontent.com/sindresorhus/supports-color/master/license | ||
if (typeof process === 'undefined') { | ||
return false; | ||
} | ||
if (process.argv.indexOf('--no-color') !== -1) { | ||
return false; | ||
} | ||
if (process.argv.indexOf('--color') !== -1) { | ||
return true; | ||
} | ||
if (process.stdout && !process.stdout.isTTY) { | ||
return false; | ||
} | ||
if (process.platform === 'win32') { | ||
return true; | ||
} | ||
if ('COLORTERM' in process.env) { | ||
return true; | ||
} | ||
if (process.env.TERM === 'dumb') { | ||
return false; | ||
} | ||
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function defaultFormat() { | ||
if (typeof global.window !== 'undefined' && typeof global.window.navigator !== 'undefined') { | ||
return 'html'; // Browser | ||
} else if (supportsColors()) { | ||
return 'ansi'; // colored console | ||
} else { | ||
return 'text'; // Plain text | ||
return result; | ||
}, | ||
forEach: function (arr, callback, that) { | ||
for (var i = 0, n = arr.length; i < n; i += 1) | ||
if (i in arr) | ||
callback.call(that, arr[i], i, arr); | ||
}, | ||
map: function (arr, mapper, that) { | ||
var other = new Array(arr.length); | ||
for (var i = 0, n = arr.length; i < n; i += 1) | ||
if (i in arr) | ||
other[i] = mapper.call(that, arr[i], i, arr); | ||
return other; | ||
}, | ||
filter: function (arr, predicate) { | ||
var length = +arr.length; | ||
var result = []; | ||
if (typeof predicate !== "function") | ||
throw new TypeError(); | ||
for (var i = 0; i < length; i += 1) { | ||
var value = arr[i]; | ||
if (predicate(value)) { | ||
result.push(value); | ||
} | ||
} | ||
namespace.defaultFormat = defaultFormat; | ||
}()); | ||
(function () { | ||
var shim = namespace.shim; | ||
var map = shim.map; | ||
var forEach = shim.forEach; | ||
var utils = namespace.utils; | ||
var extend = utils.extend; | ||
function duplicateText(content, times) { | ||
var result = ''; | ||
for (var i = 0; i < times; i += 1) { | ||
result += content; | ||
} | ||
return result; | ||
} | ||
namespace.duplicateText = duplicateText; | ||
function isOutputEntry(obj) { | ||
return typeof obj === 'object' && 'style' in obj && 'args' in obj; | ||
} | ||
function MagicPen(options) { | ||
if (!(this instanceof MagicPen)) { | ||
return new MagicPen(options); | ||
} | ||
options = options || {}; | ||
var indentationWidth = 'indentationWidth' in options ? | ||
options.indentationWidth : 2; | ||
this.indentationWidth = Math.max(indentationWidth, 0); | ||
this.indentationLevel = 0; | ||
this.output = []; | ||
this.styles = {}; | ||
} | ||
MagicPen.prototype.newline = MagicPen.prototype.nl = function (count) { | ||
if (typeof count === 'undefined') { | ||
count = 1; | ||
} | ||
if (count === 0) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
for (var i = 0; i < count; i += 1) { | ||
this.output.push([]); | ||
} | ||
return this; | ||
}; | ||
MagicPen.serializers = {}; | ||
MagicPen.prototype.write = function () { | ||
if (arguments.length === 1 && isOutputEntry(arguments[0])) { | ||
var options = arguments[0]; | ||
if (this.styles[options.style]) { | ||
this.styles[options.style].apply(this, options.args); | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
this.output[this.output.length - 1].push(options); | ||
return this; | ||
} else { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return this.write({ style: arguments[0], args: args }); | ||
} | ||
}; | ||
MagicPen.prototype.indentLines = function () { | ||
this.indentationLevel += 1; | ||
return this; | ||
}; | ||
MagicPen.prototype.indent = MagicPen.prototype.i = function () { | ||
for (var i = 0; i < this.indentationLevel; i += 1) { | ||
this.space(this.indentationWidth); | ||
} | ||
return this; | ||
}; | ||
MagicPen.prototype.outdentLines = function () { | ||
this.indentationLevel = Math.max(0, this.indentationLevel - 1); | ||
return this; | ||
}; | ||
MagicPen.prototype.addStyle = function (style, handler) { | ||
if (this[style]) { | ||
throw new Error('"' + style + '" style is already defined'); | ||
} | ||
this.styles[style] = handler; | ||
this[style] = function () { | ||
handler.apply(this, arguments); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
MagicPen.prototype.toString = function (format) { | ||
format = format || 'text'; | ||
if (format === 'auto') { | ||
format = namespace.defaultFormat(); | ||
} | ||
var serializer = new MagicPen.serializers[format](); | ||
return serializer.serialize(this.output); | ||
}; | ||
MagicPen.prototype.text = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
return this.write({ style: 'text', args: args }); | ||
}; | ||
MagicPen.prototype.block = function (pen) { | ||
var blockOutput = map(pen.output, function (line) { | ||
return [].concat(line); | ||
return result; | ||
} | ||
}; | ||
},{}],7:[function(require,module,exports){ | ||
var shim = require(6); | ||
var prototypes = { | ||
forEach: Array.prototype.forEach, | ||
map: Array.prototype.map, | ||
filter: Array.prototype.filter | ||
}; | ||
function createShimMethod(key) { | ||
shim[key] = function (obj) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return prototypes[key].apply(obj, args); | ||
}; | ||
} | ||
for (var key in prototypes) { | ||
if (prototypes.hasOwnProperty(key) && prototypes[key]) { | ||
createShimMethod(key); | ||
} | ||
} | ||
if (Object.keys) { | ||
shim['getKeys'] = Object.keys; | ||
} | ||
module.exports = shim; | ||
},{}],8:[function(require,module,exports){ | ||
var shim = require(7); | ||
var forEach = shim.forEach; | ||
var getKeys = shim.getKeys; | ||
module.exports = { | ||
extend: function (target) { | ||
var sources = Array.prototype.slice.call(arguments, 1); | ||
forEach(sources, function (source) { | ||
forEach(getKeys(source), function (key) { | ||
target[key] = source[key]; | ||
}); | ||
return this.write('block', blockOutput); | ||
}; | ||
MagicPen.prototype.append = function (pen) { | ||
if (pen.output.length === 0) { | ||
return this; | ||
} | ||
this.output[0] = this.output[0] || []; | ||
var lastLine = this.output[this.output.length - 1]; | ||
Array.prototype.push.apply(lastLine, pen.output[0]); | ||
this.output.push.apply(this.output, pen.output.slice(1)); | ||
return this; | ||
}; | ||
MagicPen.prototype.prependLinesWith = function (pen) { | ||
if (pen.output.length === 0 || this.output.length === 0) { | ||
return this; | ||
} | ||
if (pen.output.length > 1) { | ||
throw new Error('PrependLinesWith only supports a pen with single line content'); | ||
} | ||
var outputToPrepend = [].concat(pen.output[0]); | ||
this.output = map(this.output, function (line) { | ||
return outputToPrepend.concat(line); | ||
}); | ||
return this; | ||
}; | ||
MagicPen.prototype.space = MagicPen.prototype.sp = function (count) { | ||
if (count === 0) { | ||
return this; | ||
} | ||
if (typeof count === 'undefined') { | ||
count = 1; | ||
} | ||
this.text(duplicateText(' ', count)); | ||
return this; | ||
}; | ||
forEach([ | ||
'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden', | ||
'strikeThrough', 'black', 'red', 'green', 'yellow', 'blue', | ||
'magenta', 'cyan', 'white', 'gray', 'bgBlack', 'bgRed', | ||
'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan', | ||
'bgWhite' | ||
], function (textStyle) { | ||
MagicPen.prototype[textStyle] = function (content) { | ||
return this.text.call(this, content, textStyle); | ||
}; | ||
}); | ||
MagicPen.prototype.clone = function () { | ||
function MagicPenClone() {} | ||
MagicPenClone.prototype = this; | ||
var clonedPen = new MagicPenClone(); | ||
clonedPen.styles = extend({}, this.styles); | ||
clonedPen.indentationLevel = 0; | ||
clonedPen.output = []; | ||
return clonedPen; | ||
}; | ||
function callculateLineSize(line) { | ||
var size = { height: 1, width: 0 }; | ||
forEach(line, function (outputEntry) { | ||
switch (outputEntry.style) { | ||
case 'text': | ||
size.width += String(outputEntry.args[0]).length; | ||
break; | ||
case 'block': | ||
var blockSize = calculateSize(outputEntry.args[0]); | ||
size.width += blockSize.width; | ||
size.height = Math.max(blockSize.height, size.height); | ||
break; | ||
return target; | ||
} | ||
}; | ||
},{}],9:[function(require,module,exports){ | ||
'use strict'; | ||
var styles = module.exports; | ||
var codes = { | ||
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], | ||
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], | ||
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(codes).forEach(function (key) { | ||
var val = codes[key]; | ||
var style = styles[key] = {}; | ||
style.open = '\u001b[' + val[0] + 'm'; | ||
style.close = '\u001b[' + val[1] + 'm'; | ||
}); | ||
},{}],10:[function(require,module,exports){ | ||
// shim for using process in browser | ||
var process = module.exports = {}; | ||
process.nextTick = (function () { | ||
var canSetImmediate = typeof window !== 'undefined' | ||
&& window.setImmediate; | ||
var canPost = typeof window !== 'undefined' | ||
&& window.postMessage && window.addEventListener | ||
; | ||
if (canSetImmediate) { | ||
return function (f) { return window.setImmediate(f) }; | ||
} | ||
if (canPost) { | ||
var queue = []; | ||
window.addEventListener('message', function (ev) { | ||
var source = ev.source; | ||
if ((source === window || source === null) && ev.data === 'process-tick') { | ||
ev.stopPropagation(); | ||
if (queue.length > 0) { | ||
var fn = queue.shift(); | ||
fn(); | ||
} | ||
}); | ||
return size; | ||
} | ||
function calculateSize(lines) { | ||
var size = { height: 0, width: 0 }; | ||
forEach(lines, function (line) { | ||
var lineSize = callculateLineSize(line); | ||
size.height += lineSize.height; | ||
size.width = Math.max(size.width, lineSize.width); | ||
}); | ||
return size; | ||
} | ||
MagicPen.prototype.size = function () { | ||
return calculateSize(this.output); | ||
}; | ||
namespace.MagicPen = MagicPen; | ||
namespace.magicpen = MagicPen; | ||
}()); | ||
(function () { | ||
var shim = namespace.shim; | ||
var map = shim.map; | ||
var forEach = shim.forEach; | ||
var duplicateText = namespace.duplicateText; | ||
// 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) { | ||
return text.replace(ansiRegex, ''); | ||
} | ||
function TextSerializer() {} | ||
TextSerializer.prototype.serialize = function (lines) { | ||
return map(lines, this.serializeLine, this).join('\n'); | ||
}; | ||
TextSerializer.prototype.serializeLine = function (line) { | ||
var serializedLines = ['']; | ||
var startIndex = 0; | ||
forEach(line, function (outputEntry, blockIndex) { | ||
var inlineBlock = this[outputEntry.style] ? | ||
this[outputEntry.style].apply(this, outputEntry.args) : | ||
''; | ||
var blockLines = map(String(inlineBlock).split('\n'), function (serializedBlockLine) { | ||
return { | ||
content: serializedBlockLine, | ||
length: stripAnsi(serializedBlockLine).length | ||
}; | ||
}); | ||
var longestBlockLine = 0; | ||
forEach(blockLines, function (blockLine) { | ||
longestBlockLine = Math.max(longestBlockLine, blockLine.length); | ||
}); | ||
forEach(blockLines, function (blockLine, index) { | ||
serializedLines[index] = serializedLines[index] || ''; | ||
var padding = duplicateText(' ', startIndex - stripAnsi(serializedLines[index]).length); | ||
serializedLines[index] += padding + blockLine.content; | ||
}); | ||
startIndex += longestBlockLine; | ||
}, this); | ||
return serializedLines.join('\n'); | ||
}; | ||
TextSerializer.prototype.text = function (content) { | ||
return content; | ||
}; | ||
TextSerializer.prototype.block = function (content) { | ||
return this.serialize(content); | ||
}; | ||
namespace.magicpen.serializers.text = namespace.TextSerializer = TextSerializer; | ||
}()); | ||
(function () { | ||
var TextSerializer = namespace.TextSerializer; | ||
var forEach = namespace.shim.forEach; | ||
var ansiStyles = (function () { | ||
// Copied from https://github.com/sindresorhus/ansi-styles/ | ||
// License: raw.githubusercontent.com/sindresorhus/ansi-styles/master/license | ||
var styles = {}; | ||
var codes = { | ||
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], | ||
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], | ||
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(codes).forEach(function (key) { | ||
var val = codes[key]; | ||
var style = styles[key] = {}; | ||
style.open = '\x1B[' + val[0] + 'm'; | ||
style.close = '\x1B[' + val[1] + 'm'; | ||
}); | ||
return styles; | ||
}()); | ||
function AnsiSerializer() {} | ||
AnsiSerializer.prototype = new TextSerializer(); | ||
AnsiSerializer.prototype.text = function (content) { | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = stylesString.split(/\s*,\s*/); | ||
forEach(styles, function (style) { | ||
if (ansiStyles[style]) { | ||
content = ansiStyles[style].open + content + ansiStyles[style].close; | ||
} | ||
}); | ||
} | ||
return content; | ||
}, true); | ||
return function nextTick(fn) { | ||
queue.push(fn); | ||
window.postMessage('process-tick', '*'); | ||
}; | ||
namespace.magicpen.serializers.ansi = namespace.AnsiSerializer = AnsiSerializer; | ||
}()); | ||
(function () { | ||
var shim = namespace.shim; | ||
var forEach = shim.forEach; | ||
var map = shim.map; | ||
var filter = shim.filter; | ||
var htmlStyles = { | ||
bold: 'font-weight: bold', | ||
dim: 'opacity: 0.7', | ||
italic: 'font-style: italic', | ||
underline: 'text-decoration: underline', | ||
inverse: '-webkit-filter: invert(%100); filter: invert(100%)', | ||
hidden: 'visibility: hidden', | ||
strikeThrough: 'text-decoration: line-through', | ||
black: 'color: black', | ||
red: 'color: red', | ||
green: 'color: green', | ||
yellow: 'color: yellow', | ||
blue: 'color: blue', | ||
magenta: 'color: magenta', | ||
cyan: 'color: cyan', | ||
white: 'color: white', | ||
gray: 'color: gray', | ||
bgBlack: 'background-color: black', | ||
bgRed: 'background-color: red', | ||
bgGreen: 'background-color: green', | ||
bgYellow: 'background-color: yellow', | ||
bgBlue: 'background-color: blue', | ||
bgMagenta: 'background-color: magenta', | ||
bgCyan: 'background-color: cyan', | ||
bgWhite: 'background-color: white' | ||
}; | ||
function HtmlSerializer() {} | ||
HtmlSerializer.prototype.serialize = function (lines) { | ||
return '<code>\n' + this.serializeLines(lines) + '\n</code>'; | ||
}; | ||
HtmlSerializer.prototype.serializeLines = function (lines) { | ||
return map(lines, function (line) { | ||
return ' <div>' + this.serializeLine(line).join('') + '</div>'; | ||
}, this).join('\n'); | ||
}; | ||
HtmlSerializer.prototype.serializeLine = function (line) { | ||
return map(line, function (outputEntry) { | ||
return this[outputEntry.style] ? | ||
this[outputEntry.style].apply(this, outputEntry.args) : | ||
''; | ||
}, this); | ||
}; | ||
HtmlSerializer.prototype.block = function (content) { | ||
return '<div style="display: inline-block; vertical-align: top">' + | ||
this.serialize(content) + | ||
'</div>'; | ||
}; | ||
HtmlSerializer.prototype.text = function (content) { | ||
content = String(content) | ||
.replace(/&/g, '&') | ||
.replace(/ /g, ' ') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"'); | ||
if (arguments.length > 1) { | ||
var stylesString = Array.prototype.slice.call(arguments, 1).join(','); | ||
var styles = filter(stylesString.split(/\s*,\s*/), function (styleName) { | ||
return htmlStyles[styleName]; | ||
}); | ||
content = '<span style="' + map(styles, function (styleName) { | ||
return htmlStyles[styleName]; | ||
}).join('; ') + '">' + content + '</span>'; | ||
} | ||
return content; | ||
}; | ||
namespace.magicpen.serializers.html = namespace.HtmlSerializer = HtmlSerializer; | ||
}()); | ||
(function () { | ||
var global = this; | ||
var magicpen = namespace.magicpen; | ||
// Support three module loading scenarios | ||
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { | ||
// CommonJS/Node.js | ||
module.exports = magicpen; | ||
} else if (typeof define === 'function' && define.amd) { | ||
// AMD anonymous module | ||
define(function () { | ||
return magicpen; | ||
}); | ||
} else { | ||
// No module loader (plain <script> tag) - put directly in global namespace | ||
global.weknowhow = global.weknowhow || {}; | ||
global.weknowhow.magicpen = magicpen; | ||
} | ||
}()); | ||
}()); | ||
} | ||
return function nextTick(fn) { | ||
setTimeout(fn, 0); | ||
}; | ||
})(); | ||
process.title = 'browser'; | ||
process.browser = true; | ||
process.env = {}; | ||
process.argv = []; | ||
function noop() {} | ||
process.on = noop; | ||
process.addListener = noop; | ||
process.once = noop; | ||
process.off = noop; | ||
process.removeListener = noop; | ||
process.removeAllListeners = noop; | ||
process.emit = noop; | ||
process.binding = function (name) { | ||
throw new Error('process.binding is not supported'); | ||
} | ||
// TODO(shtylman) | ||
process.cwd = function () { return '/' }; | ||
process.chdir = function (dir) { | ||
throw new Error('process.chdir is not supported'); | ||
}; | ||
},{}],11:[function(require,module,exports){ | ||
(function (process){ | ||
'use strict'; | ||
module.exports = (function () { | ||
if (process.argv.indexOf('--no-color') !== -1) { | ||
return false; | ||
} | ||
if (process.argv.indexOf('--color') !== -1) { | ||
return true; | ||
} | ||
if (process.stdout && !process.stdout.isTTY) { | ||
return false; | ||
} | ||
if (process.platform === 'win32') { | ||
return true; | ||
} | ||
if ('COLORTERM' in process.env) { | ||
return true; | ||
} | ||
if (process.env.TERM === 'dumb') { | ||
return false; | ||
} | ||
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) { | ||
return true; | ||
} | ||
return false; | ||
})(); | ||
}).call(this,require(10)) | ||
},{}]},{},[3])(3) | ||
}); |
{ | ||
"name": "magicpen", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Styled output in both consoles and browsers", | ||
"main": "./lib/index.js", | ||
"main": "./lib/MagicPen.js", | ||
"directories": { | ||
@@ -15,12 +15,11 @@ "test": "test" | ||
"test": "gulp test", | ||
"prepublish": "gulp scripts" | ||
"prepublish": "(echo '/*!' && <LICENSE sed -e's/^/ * /' | sed -e's/\\s+$//' && echo ' */' && browserify -p bundle-collapser/plugin -e lib/MagicPen -s weknowhow.MagicPen) > magicpen.js", | ||
"coverage": "NODE_ENV=development ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- --reporter dot" | ||
}, | ||
"author": "Sune Simonsen", | ||
"devDependencies": { | ||
"browserify": "^5.9.1", | ||
"bundle-collapser": "^1.1.0", | ||
"del": "^0.1.1", | ||
"gulp": "^3.8.6", | ||
"gulp-concat": "^2.3.4", | ||
"gulp-insert": "^0.4.0", | ||
"gulp-istanbul": "^0.2.1", | ||
"gulp-mocha": "^0.5.1", | ||
"istanbul": "^0.3.0", | ||
"mocha": "^1.20.1", | ||
@@ -30,3 +29,8 @@ "sinon": "=1.9.1", | ||
"unexpected-sinon": "^3.0.2" | ||
} | ||
}, | ||
"dependencies": { | ||
"ansi-styles": "^1.1.0", | ||
"supports-color": "^0.2.0" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -376,1 +376,5 @@ # MagicPen | ||
Alias for `text(content, 'bgWhite')`. | ||
## License | ||
MIT, see the `LICENSE` file for details. |
/*global describe, it, beforeEach, before*/ | ||
var isCoverage = process.env["ISCOVERAGE"]; | ||
var magicpen = isCoverage ? require('../magicpen') : require('../lib/'); | ||
var magicpen = require('..'); | ||
var expect = require('unexpected'); | ||
@@ -5,0 +4,0 @@ var sinon = require('sinon'); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances 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
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
8
379
88328
2
19
1461
6
1
+ Addedansi-styles@^1.1.0
+ Addedsupports-color@^0.2.0
+ Addedansi-styles@1.1.0(transitive)
+ Addedsupports-color@0.2.0(transitive)