UNPKG

picocog-ts

Version:
284 lines 10.8 kB
"use strict"; /* * Copyright 2021, Chris Ainsley * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PicoWriter = void 0; var SEP = "\n"; var DI = " "; var IndentedLine = /** @class */ (function () { function IndentedLine(line, indent) { this._line = line; this._indent = indent; } Object.defineProperty(IndentedLine.prototype, "line", { get: function () { return this._line; }, enumerable: false, configurable: true }); Object.defineProperty(IndentedLine.prototype, "indent", { get: function () { return this._indent; }, enumerable: false, configurable: true }); IndentedLine.prototype.adjustIndent = function (delta) { this._indent += delta; }; return IndentedLine; }()); var StringHolder = /** @class */ (function () { function StringHolder() { this._text = ""; } Object.defineProperty(StringHolder.prototype, "text", { get: function () { return this._text; }, set: function (newText) { this._text = newText; }, enumerable: false, configurable: true }); return StringHolder; }()); var PicoWriter = /** @class */ (function () { function PicoWriter(initialIndent, indentText) { this._indents = -1; this._numLines = 0; this._generateIfEmpty = true; this._generate = true; this._normalizeAdjacentBlankRows = false; this._isDirty = false; this._textBuffer = ""; this._ic = DI; this._rows = []; this._content = []; this._indents = initialIndent < 0 ? 0 : initialIndent; this._ic = indentText == null ? DI : indentText; } PicoWriter.prototype.indentRight = function () { this.flushRows(); this._indents++; }; PicoWriter.prototype.indentLeft = function () { this.flushRows(); this._indents--; if (this._indents < 0) { throw new Error("Local indent cannot be less than zero"); } }; PicoWriter.prototype.append = function (inner) { if (this._textBuffer.length > 0) { this.flush(); this._numLines++; } this.adjustIndents(inner, this._indents, this._ic); this._content.push(inner); this._numLines++; return this; }; PicoWriter.prototype.writeln = function (text) { this._numLines++; this._textBuffer = this._textBuffer.concat(text); this.flush(); return this; }; PicoWriter.prototype.writeln_r = function (text) { this.writeln(text); this.indentRight(); return this; }; PicoWriter.prototype.writeln_l = function (text) { this.flushRows(); this.indentLeft(); this.writeln(text); return this; }; PicoWriter.prototype.writeln_lr = function (text) { this.flushRows(); this.indentLeft(); this.writeln(text); this.indentRight(); return this; }; PicoWriter.prototype.writeRow = function (strings) { this._rows.push(strings); this._isDirty = true; this._numLines++; return this; }; // public createDeferredWriter() { // return this.createWriter(); // } PicoWriter.prototype.createWriter = function () { if (this._textBuffer.length > 0) { this.flush(); this._numLines++; } var inner = new PicoWriter(this._indents, this._ic); this._content.push(inner); this._numLines++; return inner; }; PicoWriter.prototype.createInnerBlockWriter = function (startLine, endLine) { this.writeln(startLine); this.indentRight(); var newWriter = this.createWriter(); // Don't relecate this line this.indentLeft(); this.writeln(endLine); this._isDirty = true; this._numLines += 2; return newWriter; }; PicoWriter.prototype.isEmpty = function () { return this._numLines == 0; }; PicoWriter.prototype.write = function (text) { this._numLines++; this._isDirty = true; this._textBuffer = this._textBuffer.concat(text); }; PicoWriter.prototype.isMethodBodyEmpty = function () { return this._content.length == 0 && this._textBuffer.length == 0; }; PicoWriter.prototype.isGenerateIfEmpty = function () { return this._generateIfEmpty; }; PicoWriter.prototype.setGenerateIfEmpty = function (generateIfEmpty) { this._generateIfEmpty = generateIfEmpty; }; PicoWriter.prototype.isGenerate = function () { return this._generate; }; PicoWriter.prototype.setGenerate = function (generate) { this._generate = generate; }; PicoWriter.prototype.setNormalizeAdjacentBlankRows = function (normalizeAdjacentBlankRows) { this._normalizeAdjacentBlankRows = normalizeAdjacentBlankRows; }; PicoWriter.prototype.toString = function () { return this.toStringWithIndent(0); }; PicoWriter.prototype.adjustIndents = function (inner, indents, ic) { if (inner != null) { for (var _i = 0, _a = inner._content; _i < _a.length; _i++) { var item = _a[_i]; if (item instanceof PicoWriter) { this.adjustIndents(item, indents, ic); } else if (item instanceof IndentedLine) { item.adjustIndent(indents); } } inner._ic = ic; } }; PicoWriter.prototype.toStringWithIndent = function (indentBase) { var stringHolder = new StringHolder(); this.render(stringHolder, indentBase, this._normalizeAdjacentBlankRows, false /* lastRowWasBlank */); return stringHolder.text; }; PicoWriter.prototype.flush = function () { this.flushRows(); this._content.push(new IndentedLine(this._textBuffer.toString(), this._indents)); this._textBuffer = ""; this._isDirty = false; }; PicoWriter.prototype.flushRows = function () { if (this._rows.length > 0) { var maxWidth = []; for (var _i = 0, _a = this._rows; _i < _a.length; _i++) { var columns = _a[_i]; var numColumns_1 = columns.length; for (var i = 0; i < numColumns_1; i++) { var currentColumnStringValue = columns[i]; var currentColumnStringValueLength = currentColumnStringValue == null ? 0 : currentColumnStringValue.length; if (maxWidth.length < i + 1) { maxWidth.push(currentColumnStringValueLength); } else { if (maxWidth[i] < currentColumnStringValueLength) { maxWidth[i] = currentColumnStringValueLength; } } } } var rowSB = ""; for (var _b = 0, _c = this._rows; _b < _c.length; _b++) { var columns = _c[_b]; var numColumns = columns.length; for (var i = 0; i < numColumns; i++) { var currentColumnStringValue = columns[i]; var currentItemWidth = currentColumnStringValue == null ? 0 : currentColumnStringValue.length; var maxWidth1 = maxWidth[i]; rowSB = rowSB.concat(currentColumnStringValue == null ? "" : currentColumnStringValue); if (currentItemWidth < maxWidth1) { for (var j = currentItemWidth; j < maxWidth1; j++) { rowSB = rowSB.concat(" "); } } } this._content.push(new IndentedLine(rowSB.toString(), this._indents)); rowSB = ""; } this._rows = []; } }; PicoWriter.prototype.render = function (holder, indentBase, normalizeAdjacentBlankRows, lastRowWasBlank) { if (this._isDirty) { this.flush(); } // Some methods are flagged not to be generated if there is no body text inside the method, we don't add these to the class narrative if ((!this.isGenerate()) || ((!this.isGenerateIfEmpty()) && this.isMethodBodyEmpty())) { return lastRowWasBlank; } for (var _i = 0, _a = this._content; _i < _a.length; _i++) { var item = _a[_i]; if (item instanceof IndentedLine) { var il = item; var lineText = il.line; var indentLevelHere = indentBase + il.indent; var thisRowIsBlank = lineText.length == 0; if (normalizeAdjacentBlankRows && lastRowWasBlank && thisRowIsBlank) { // Don't write the line if we already had a blank line } else { for (var indentIndex = 0; indentIndex < indentLevelHere; indentIndex++) { holder.text = holder.text.concat(this._ic); } holder.text = holder.text.concat(lineText); holder.text = holder.text.concat(SEP); } lastRowWasBlank = thisRowIsBlank; } else if (item instanceof PicoWriter) { lastRowWasBlank = item.render(holder, indentBase, normalizeAdjacentBlankRows, lastRowWasBlank); } else { var text = item.toString(); holder.text = holder.text.concat(text); } } return lastRowWasBlank; }; return PicoWriter; }()); exports.PicoWriter = PicoWriter; //# sourceMappingURL=index.js.map