diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/renderkid/lib/layout | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/renderkid/lib/layout')
12 files changed, 644 insertions, 0 deletions
diff --git a/node_modules/renderkid/lib/layout/Block.js b/node_modules/renderkid/lib/layout/Block.js new file mode 100644 index 000000000..e22e800a4 --- /dev/null +++ b/node_modules/renderkid/lib/layout/Block.js @@ -0,0 +1,253 @@ +// Generated by CoffeeScript 1.9.3 +var Block, SpecialString, object, terminalWidth; + +SpecialString = require('./SpecialString'); + +object = require('utila').object; + +terminalWidth = require('../tools').getCols(); + +module.exports = Block = (function() { + var self; + + self = Block; + + Block.defaultConfig = { + blockPrependor: { + fn: require('./block/blockPrependor/Default'), + options: { + amount: 0 + } + }, + blockAppendor: { + fn: require('./block/blockAppendor/Default'), + options: { + amount: 0 + } + }, + linePrependor: { + fn: require('./block/linePrependor/Default'), + options: { + amount: 0 + } + }, + lineAppendor: { + fn: require('./block/lineAppendor/Default'), + options: { + amount: 0 + } + }, + lineWrapper: { + fn: require('./block/lineWrapper/Default'), + options: { + lineWidth: null + } + }, + width: terminalWidth, + prefixRaw: '', + suffixRaw: '' + }; + + function Block(_layout, _parent, config, _name) { + this._layout = _layout; + this._parent = _parent; + if (config == null) { + config = {}; + } + this._name = _name != null ? _name : ''; + this._config = object.append(self.defaultConfig, config); + this._closed = false; + this._wasOpenOnce = false; + this._active = false; + this._buffer = ''; + this._didSeparateBlock = false; + this._linePrependor = new this._config.linePrependor.fn(this._config.linePrependor.options); + this._lineAppendor = new this._config.lineAppendor.fn(this._config.lineAppendor.options); + this._blockPrependor = new this._config.blockPrependor.fn(this._config.blockPrependor.options); + this._blockAppendor = new this._config.blockAppendor.fn(this._config.blockAppendor.options); + } + + Block.prototype._activate = function(deactivateParent) { + if (deactivateParent == null) { + deactivateParent = true; + } + if (this._active) { + throw Error("This block is already active. This is probably a bug in RenderKid itself"); + } + if (this._closed) { + throw Error("This block is closed and cannot be activated. This is probably a bug in RenderKid itself"); + } + this._active = true; + this._layout._activeBlock = this; + if (deactivateParent) { + if (this._parent != null) { + this._parent._deactivate(false); + } + } + return this; + }; + + Block.prototype._deactivate = function(activateParent) { + if (activateParent == null) { + activateParent = true; + } + this._ensureActive(); + this._flushBuffer(); + if (activateParent) { + if (this._parent != null) { + this._parent._activate(false); + } + } + this._active = false; + return this; + }; + + Block.prototype._ensureActive = function() { + if (!this._wasOpenOnce) { + throw Error("This block has never been open before. This is probably a bug in RenderKid itself."); + } + if (!this._active) { + throw Error("This block is not active. This is probably a bug in RenderKid itself."); + } + if (this._closed) { + throw Error("This block is already closed. This is probably a bug in RenderKid itself."); + } + }; + + Block.prototype._open = function() { + if (this._wasOpenOnce) { + throw Error("Block._open() has been called twice. This is probably a RenderKid bug."); + } + this._wasOpenOnce = true; + if (this._parent != null) { + this._parent.write(this._whatToPrependToBlock()); + } + this._activate(); + return this; + }; + + Block.prototype.close = function() { + this._deactivate(); + this._closed = true; + if (this._parent != null) { + this._parent.write(this._whatToAppendToBlock()); + } + return this; + }; + + Block.prototype.isOpen = function() { + return this._wasOpenOnce && !this._closed; + }; + + Block.prototype.write = function(str) { + this._ensureActive(); + if (str === '') { + return; + } + str = String(str); + this._buffer += str; + return this; + }; + + Block.prototype.openBlock = function(config, name) { + var block; + this._ensureActive(); + block = new Block(this._layout, this, config, name); + block._open(); + return block; + }; + + Block.prototype._flushBuffer = function() { + var str; + if (this._buffer === '') { + return; + } + str = this._buffer; + this._buffer = ''; + this._writeInline(str); + }; + + Block.prototype._toPrependToLine = function() { + var fromParent; + fromParent = ''; + if (this._parent != null) { + fromParent = this._parent._toPrependToLine(); + } + return this._linePrependor.render(fromParent); + }; + + Block.prototype._toAppendToLine = function() { + var fromParent; + fromParent = ''; + if (this._parent != null) { + fromParent = this._parent._toAppendToLine(); + } + return this._lineAppendor.render(fromParent); + }; + + Block.prototype._whatToPrependToBlock = function() { + return this._blockPrependor.render(); + }; + + Block.prototype._whatToAppendToBlock = function() { + return this._blockAppendor.render(); + }; + + Block.prototype._writeInline = function(str) { + var i, j, k, l, lineBreaksToAppend, m, ref, ref1, ref2, remaining; + if (SpecialString(str).isOnlySpecialChars()) { + this._layout._append(str); + return; + } + remaining = str; + lineBreaksToAppend = 0; + if (m = remaining.match(/^\n+/)) { + for (i = j = 1, ref = m[0].length; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) { + this._writeLine(''); + } + remaining = remaining.substr(m[0].length, remaining.length); + } + if (m = remaining.match(/\n+$/)) { + lineBreaksToAppend = m[0].length; + remaining = remaining.substr(0, remaining.length - m[0].length); + } + while (remaining.length > 0) { + if (m = remaining.match(/^[^\n]+/)) { + this._writeLine(m[0]); + remaining = remaining.substr(m[0].length, remaining.length); + } else if (m = remaining.match(/^\n+/)) { + for (i = k = 1, ref1 = m[0].length; 1 <= ref1 ? k < ref1 : k > ref1; i = 1 <= ref1 ? ++k : --k) { + this._writeLine(''); + } + remaining = remaining.substr(m[0].length, remaining.length); + } + } + if (lineBreaksToAppend > 0) { + for (i = l = 1, ref2 = lineBreaksToAppend; 1 <= ref2 ? l <= ref2 : l >= ref2; i = 1 <= ref2 ? ++l : --l) { + this._writeLine(''); + } + } + }; + + Block.prototype._writeLine = function(str) { + var line, lineContent, lineContentLength, remaining, roomLeft, toAppend, toAppendLength, toPrepend, toPrependLength; + remaining = SpecialString(str); + while (true) { + toPrepend = this._toPrependToLine(); + toPrependLength = SpecialString(toPrepend).length; + toAppend = this._toAppendToLine(); + toAppendLength = SpecialString(toAppend).length; + roomLeft = this._layout._config.terminalWidth - (toPrependLength + toAppendLength); + lineContentLength = Math.min(this._config.width, roomLeft); + lineContent = remaining.cut(0, lineContentLength, true); + line = toPrepend + lineContent.str + toAppend; + this._layout._appendLine(line); + if (remaining.isEmpty()) { + break; + } + } + }; + + return Block; + +})(); diff --git a/node_modules/renderkid/lib/layout/SpecialString.js b/node_modules/renderkid/lib/layout/SpecialString.js new file mode 100644 index 000000000..f106b2cee --- /dev/null +++ b/node_modules/renderkid/lib/layout/SpecialString.js @@ -0,0 +1,176 @@ +// Generated by CoffeeScript 1.9.3 +var SpecialString, fn, i, len, prop, ref; + +module.exports = SpecialString = (function() { + var self; + + self = SpecialString; + + SpecialString._tabRx = /^\t/; + + SpecialString._tagRx = /^<[^>]+>/; + + SpecialString._quotedHtmlRx = /^&(gt|lt|quot|amp|apos|sp);/; + + function SpecialString(str) { + if (!(this instanceof self)) { + return new self(str); + } + this._str = String(str); + this._len = 0; + } + + SpecialString.prototype._getStr = function() { + return this._str; + }; + + SpecialString.prototype.set = function(str) { + this._str = String(str); + return this; + }; + + SpecialString.prototype.clone = function() { + return new SpecialString(this._str); + }; + + SpecialString.prototype.isEmpty = function() { + return this._str === ''; + }; + + SpecialString.prototype.isOnlySpecialChars = function() { + return !this.isEmpty() && this.length === 0; + }; + + SpecialString.prototype._reset = function() { + return this._len = 0; + }; + + SpecialString.prototype.splitIn = function(limit, trimLeftEachLine) { + var buffer, bufferLength, justSkippedSkipChar, lines; + if (trimLeftEachLine == null) { + trimLeftEachLine = false; + } + buffer = ''; + bufferLength = 0; + lines = []; + justSkippedSkipChar = false; + self._countChars(this._str, function(char, charLength) { + if (bufferLength > limit || bufferLength + charLength > limit) { + lines.push(buffer); + buffer = ''; + bufferLength = 0; + } + if (bufferLength === 0 && char === ' ' && !justSkippedSkipChar && trimLeftEachLine) { + return justSkippedSkipChar = true; + } else { + buffer += char; + bufferLength += charLength; + return justSkippedSkipChar = false; + } + }); + if (buffer.length > 0) { + lines.push(buffer); + } + return lines; + }; + + SpecialString.prototype.trim = function() { + return new SpecialString(this.str.trim()); + }; + + SpecialString.prototype.trimLeft = function() { + return new SpecialString(this.str.replace(/^\s+/, '')); + }; + + SpecialString.prototype.trimRight = function() { + return new SpecialString(this.str.replace(/\s+$/, '')); + }; + + SpecialString.prototype._getLength = function() { + var sum; + sum = 0; + self._countChars(this._str, function(char, charLength) { + sum += charLength; + }); + return sum; + }; + + SpecialString.prototype.cut = function(from, to, trimLeft) { + var after, before, cur, cut; + if (trimLeft == null) { + trimLeft = false; + } + if (to == null) { + to = this.length; + } + from = parseInt(from); + if (from >= to) { + throw Error("`from` shouldn't be larger than `to`"); + } + before = ''; + after = ''; + cut = ''; + cur = 0; + self._countChars(this._str, (function(_this) { + return function(char, charLength) { + if (_this.str === 'ab<tag>') { + console.log(charLength, char); + } + if (cur === from && char.match(/^\s+$/) && trimLeft) { + return; + } + if (cur < from) { + before += char; + } else if (cur < to || cur + charLength <= to) { + cut += char; + } else { + after += char; + } + cur += charLength; + }; + })(this)); + this._str = before + after; + this._reset(); + return SpecialString(cut); + }; + + SpecialString._countChars = function(text, cb) { + var char, charLength, m; + while (text.length !== 0) { + if (m = text.match(self._tagRx)) { + char = m[0]; + charLength = 0; + text = text.substr(char.length, text.length); + } else if (m = text.match(self._quotedHtmlRx)) { + char = m[0]; + charLength = 1; + text = text.substr(char.length, text.length); + } else if (text.match(self._tabRx)) { + char = "\t"; + charLength = 8; + text = text.substr(1, text.length); + } else { + char = text[0]; + charLength = 1; + text = text.substr(1, text.length); + } + cb.call(null, char, charLength); + } + }; + + return SpecialString; + +})(); + +ref = ['str', 'length']; +fn = function() { + var methodName; + methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length); + return SpecialString.prototype.__defineGetter__(prop, function() { + return this[methodName](); + }); +}; +for (i = 0, len = ref.length; i < len; i++) { + prop = ref[i]; + fn(); +} diff --git a/node_modules/renderkid/lib/layout/block/blockAppendor/Default.js b/node_modules/renderkid/lib/layout/block/blockAppendor/Default.js new file mode 100644 index 000000000..c4d76854e --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/blockAppendor/Default.js @@ -0,0 +1,21 @@ +// Generated by CoffeeScript 1.9.3 +var DefaultBlockAppendor, tools, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +tools = require('../../../tools'); + +module.exports = DefaultBlockAppendor = (function(superClass) { + extend(DefaultBlockAppendor, superClass); + + function DefaultBlockAppendor() { + return DefaultBlockAppendor.__super__.constructor.apply(this, arguments); + } + + DefaultBlockAppendor.prototype._render = function(options) { + return tools.repeatString("\n", this._config.amount); + }; + + return DefaultBlockAppendor; + +})(require('./_BlockAppendor')); diff --git a/node_modules/renderkid/lib/layout/block/blockAppendor/_BlockAppendor.js b/node_modules/renderkid/lib/layout/block/blockAppendor/_BlockAppendor.js new file mode 100644 index 000000000..fda9d76a7 --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/blockAppendor/_BlockAppendor.js @@ -0,0 +1,15 @@ +// Generated by CoffeeScript 1.9.3 +var _BlockAppendor; + +module.exports = _BlockAppendor = (function() { + function _BlockAppendor(_config) { + this._config = _config; + } + + _BlockAppendor.prototype.render = function(options) { + return this._render(options); + }; + + return _BlockAppendor; + +})(); diff --git a/node_modules/renderkid/lib/layout/block/blockPrependor/Default.js b/node_modules/renderkid/lib/layout/block/blockPrependor/Default.js new file mode 100644 index 000000000..27d7353dd --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/blockPrependor/Default.js @@ -0,0 +1,21 @@ +// Generated by CoffeeScript 1.9.3 +var DefaultBlockPrependor, tools, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +tools = require('../../../tools'); + +module.exports = DefaultBlockPrependor = (function(superClass) { + extend(DefaultBlockPrependor, superClass); + + function DefaultBlockPrependor() { + return DefaultBlockPrependor.__super__.constructor.apply(this, arguments); + } + + DefaultBlockPrependor.prototype._render = function(options) { + return tools.repeatString("\n", this._config.amount); + }; + + return DefaultBlockPrependor; + +})(require('./_BlockPrependor')); diff --git a/node_modules/renderkid/lib/layout/block/blockPrependor/_BlockPrependor.js b/node_modules/renderkid/lib/layout/block/blockPrependor/_BlockPrependor.js new file mode 100644 index 000000000..737719155 --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/blockPrependor/_BlockPrependor.js @@ -0,0 +1,15 @@ +// Generated by CoffeeScript 1.9.3 +var _BlockPrependor; + +module.exports = _BlockPrependor = (function() { + function _BlockPrependor(_config) { + this._config = _config; + } + + _BlockPrependor.prototype.render = function(options) { + return this._render(options); + }; + + return _BlockPrependor; + +})(); diff --git a/node_modules/renderkid/lib/layout/block/lineAppendor/Default.js b/node_modules/renderkid/lib/layout/block/lineAppendor/Default.js new file mode 100644 index 000000000..4da2c30ad --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/lineAppendor/Default.js @@ -0,0 +1,21 @@ +// Generated by CoffeeScript 1.9.3 +var DefaultLineAppendor, tools, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +tools = require('../../../tools'); + +module.exports = DefaultLineAppendor = (function(superClass) { + extend(DefaultLineAppendor, superClass); + + function DefaultLineAppendor() { + return DefaultLineAppendor.__super__.constructor.apply(this, arguments); + } + + DefaultLineAppendor.prototype._render = function(inherited, options) { + return inherited + tools.repeatString(" ", this._config.amount); + }; + + return DefaultLineAppendor; + +})(require('./_LineAppendor')); diff --git a/node_modules/renderkid/lib/layout/block/lineAppendor/_LineAppendor.js b/node_modules/renderkid/lib/layout/block/lineAppendor/_LineAppendor.js new file mode 100644 index 000000000..75829099b --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/lineAppendor/_LineAppendor.js @@ -0,0 +1,17 @@ +// Generated by CoffeeScript 1.9.3 +var _LineAppendor; + +module.exports = _LineAppendor = (function() { + function _LineAppendor(_config) { + this._config = _config; + this._lineNo = 0; + } + + _LineAppendor.prototype.render = function(inherited, options) { + this._lineNo++; + return '<none>' + this._render(inherited, options) + '</none>'; + }; + + return _LineAppendor; + +})(); diff --git a/node_modules/renderkid/lib/layout/block/linePrependor/Default.js b/node_modules/renderkid/lib/layout/block/linePrependor/Default.js new file mode 100644 index 000000000..891ffcb8f --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/linePrependor/Default.js @@ -0,0 +1,58 @@ +// Generated by CoffeeScript 1.9.3 +var DefaultLinePrependor, SpecialString, tools, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +tools = require('../../../tools'); + +SpecialString = require('../../SpecialString'); + +module.exports = DefaultLinePrependor = (function(superClass) { + var self; + + extend(DefaultLinePrependor, superClass); + + function DefaultLinePrependor() { + return DefaultLinePrependor.__super__.constructor.apply(this, arguments); + } + + self = DefaultLinePrependor; + + DefaultLinePrependor.pad = function(howMuch) { + return tools.repeatString(" ", howMuch); + }; + + DefaultLinePrependor.prototype._render = function(inherited, options) { + var addToLeft, addToRight, alignment, bullet, char, charLen, diff, left, output, space, toWrite; + if (this._lineNo === 0 && (bullet = this._config.bullet)) { + char = bullet.char; + charLen = SpecialString(char).length; + alignment = bullet.alignment; + space = this._config.amount; + toWrite = char; + addToLeft = ''; + addToRight = ''; + if (space > charLen) { + diff = space - charLen; + if (alignment === 'right') { + addToLeft = self.pad(diff); + } else if (alignment === 'left') { + addToRight = self.pad(diff); + } else if (alignment === 'center') { + left = Math.round(diff / 2); + addToLeft = self.pad(left); + addToRight = self.pad(diff - left); + } else { + throw Error("Unkown alignment `" + alignment + "`"); + } + } + output = addToLeft + char + addToRight; + } else { + output = self.pad(this._config.amount); + } + return inherited + output; + }; + + return DefaultLinePrependor; + +})(require('./_LinePrependor')); diff --git a/node_modules/renderkid/lib/layout/block/linePrependor/_LinePrependor.js b/node_modules/renderkid/lib/layout/block/linePrependor/_LinePrependor.js new file mode 100644 index 000000000..cd6821af9 --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/linePrependor/_LinePrependor.js @@ -0,0 +1,17 @@ +// Generated by CoffeeScript 1.9.3 +var _LinePrependor; + +module.exports = _LinePrependor = (function() { + function _LinePrependor(_config) { + this._config = _config; + this._lineNo = -1; + } + + _LinePrependor.prototype.render = function(inherited, options) { + this._lineNo++; + return '<none>' + this._render(inherited, options) + '</none>'; + }; + + return _LinePrependor; + +})(); diff --git a/node_modules/renderkid/lib/layout/block/lineWrapper/Default.js b/node_modules/renderkid/lib/layout/block/lineWrapper/Default.js new file mode 100644 index 000000000..9fbe5097d --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/lineWrapper/Default.js @@ -0,0 +1,17 @@ +// Generated by CoffeeScript 1.9.3 +var DefaultLineWrapper, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + +module.exports = DefaultLineWrapper = (function(superClass) { + extend(DefaultLineWrapper, superClass); + + function DefaultLineWrapper() { + return DefaultLineWrapper.__super__.constructor.apply(this, arguments); + } + + DefaultLineWrapper.prototype._render = function() {}; + + return DefaultLineWrapper; + +})(require('./_LineWrapper')); diff --git a/node_modules/renderkid/lib/layout/block/lineWrapper/_LineWrapper.js b/node_modules/renderkid/lib/layout/block/lineWrapper/_LineWrapper.js new file mode 100644 index 000000000..c3abcc3dd --- /dev/null +++ b/node_modules/renderkid/lib/layout/block/lineWrapper/_LineWrapper.js @@ -0,0 +1,13 @@ +// Generated by CoffeeScript 1.9.3 +var _LineWrapper; + +module.exports = _LineWrapper = (function() { + function _LineWrapper() {} + + _LineWrapper.prototype.render = function(str, options) { + return this._render(str, options); + }; + + return _LineWrapper; + +})(); |