dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
104 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Box = void 0;
const terminal_1 = require("./terminal");
class Box {
constructor(content, options = {}) {
var _a, _b, _c, _d, _e, _f;
this.content = Array.isArray(content) ? content : [content];
this.options = {
padding: (_a = options.padding) !== null && _a !== void 0 ? _a : 1,
margin: (_b = options.margin) !== null && _b !== void 0 ? _b : 0,
align: (_c = options.align) !== null && _c !== void 0 ? _c : 'left',
color: (_d = options.color) !== null && _d !== void 0 ? _d : 'muted',
style: (_e = options.style) !== null && _e !== void 0 ? _e : 'single',
title: (_f = options.title) !== null && _f !== void 0 ? _f : ''
};
}
render() {
const chars = Box.styles[this.options.style];
const color = terminal_1.Terminal.colors[this.options.color];
const reset = terminal_1.Terminal.colors.reset;
// Calculate dimensions
const contentWidth = Math.max(...this.content.map(line => terminal_1.Terminal.stripAnsi(line).length), this.options.title ? terminal_1.Terminal.stripAnsi(this.options.title).length + 2 : 0);
const boxWidth = contentWidth + (this.options.padding * 2);
// Add top margin
if (this.options.margin > 0) {
terminal_1.Terminal.write('\n'.repeat(this.options.margin));
}
// Draw top border with title if present
if (this.options.title) {
const titleLine = `${color}${chars.topLeft}${chars.horizontal} ${this.options.title} ${chars.horizontal.repeat(boxWidth - this.options.title.length - 4)}${chars.topRight}${reset}\n`;
terminal_1.Terminal.write(titleLine);
}
else {
terminal_1.Terminal.write(`${color}${chars.topLeft}${chars.horizontal.repeat(boxWidth)}${chars.topRight}${reset}\n`);
}
// Draw content
this.content.forEach(line => {
const paddedLine = this.padLine(line, boxWidth);
terminal_1.Terminal.write(`${color}${chars.vertical}${reset}${paddedLine}${color}${chars.vertical}${reset}\n`);
});
// Draw bottom border
terminal_1.Terminal.write(`${color}${chars.bottomLeft}${chars.horizontal.repeat(boxWidth)}${chars.bottomRight}${reset}\n`);
// Add bottom margin
if (this.options.margin > 0) {
terminal_1.Terminal.write('\n'.repeat(this.options.margin));
}
}
padLine(line, boxWidth) {
const contentSpace = boxWidth - (this.options.padding * 2);
const paddedContent = line.padEnd(contentSpace);
let alignedContent;
switch (this.options.align) {
case 'center':
const leftSpace = Math.floor((contentSpace - line.length) / 2);
const rightSpace = contentSpace - line.length - leftSpace;
alignedContent = ' '.repeat(leftSpace) + line + ' '.repeat(rightSpace);
break;
case 'right':
alignedContent = ' '.repeat(contentSpace - line.length) + line;
break;
default: // left
alignedContent = line + ' '.repeat(contentSpace - line.length);
}
return ' '.repeat(this.options.padding) + alignedContent + ' '.repeat(this.options.padding);
}
static simple(content, color) {
new Box(content, { color, style: 'single', padding: 1 }).render();
}
static double(content, color) {
new Box(content, { color, style: 'double', padding: 1 }).render();
}
static round(content, color) {
new Box(content, { color, style: 'round', padding: 1 }).render();
}
}
exports.Box = Box;
Box.styles = {
single: {
topLeft: '┌',
topRight: '┐',
bottomLeft: '└',
bottomRight: '┘',
horizontal: '─',
vertical: '│'
},
double: {
topLeft: '╔',
topRight: '╗',
bottomLeft: '╚',
bottomRight: '╝',
horizontal: '═',
vertical: '║'
},
round: {
topLeft: '╭',
topRight: '╮',
bottomLeft: '╰',
bottomRight: '╯',
horizontal: '─',
vertical: '│'
}
};
//# sourceMappingURL=box.js.map