dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
92 lines • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
const terminal_1 = require("./terminal");
class Table {
constructor(columns, data, options = {}) {
var _a, _b, _c;
this.columns = columns;
this.data = data;
this.options = {
padding: (_a = options.padding) !== null && _a !== void 0 ? _a : 1,
borderColor: (_b = options.borderColor) !== null && _b !== void 0 ? _b : 'muted',
headerColor: (_c = options.headerColor) !== null && _c !== void 0 ? _c : 'cyan'
};
}
render() {
// Calculate total width
const totalWidth = this.columns.reduce((sum, col) => sum + col.width + (this.options.padding * 2), this.columns.length + 1 // Add space for borders
);
// Render top border
this.renderBorder('top', totalWidth);
// Render headers
this.renderHeaders();
// Render separator
this.renderBorder('mid', totalWidth);
// Render data
this.renderRows();
// Render bottom border
this.renderBorder('bottom', totalWidth);
}
renderBorder(type, width) {
const chars = {
top: ['┌', '┐', '─'],
mid: ['├', '┤', '─'],
bottom: ['└', '┘', '─']
};
const line = `${terminal_1.Terminal.colors[this.options.borderColor]}` +
chars[type][0] +
'─'.repeat(width - 2) +
chars[type][1] +
`${terminal_1.Terminal.colors.reset}`;
terminal_1.Terminal.write(line + '\n');
}
renderHeaders() {
const headerRow = this.columns.map(col => {
const header = `${terminal_1.Terminal.colors[this.options.headerColor]}` +
this.padText(col.header, col.width, col.align) +
`${terminal_1.Terminal.colors.reset}`;
return header;
});
terminal_1.Terminal.write(`${terminal_1.Terminal.colors[this.options.borderColor]}│` +
headerRow.join(`${terminal_1.Terminal.colors[this.options.borderColor]}│`) +
`${terminal_1.Terminal.colors[this.options.borderColor]}│${terminal_1.Terminal.colors.reset}\n`);
}
renderRows() {
this.data.forEach(row => {
const formattedRow = row.map((cell, index) => {
const column = this.columns[index];
return this.padText(cell, column.width, column.align);
});
terminal_1.Terminal.write(`${terminal_1.Terminal.colors[this.options.borderColor]}│` +
formattedRow.join(`${terminal_1.Terminal.colors[this.options.borderColor]}│`) +
`${terminal_1.Terminal.colors[this.options.borderColor]}│${terminal_1.Terminal.colors.reset}\n`);
});
}
padText(text, width, align = 'left') {
const padding = this.options.padding;
const innerWidth = width - (padding * 2);
// Truncate if too long
let processed = text.length > innerWidth
? text.slice(0, innerWidth - 1) + '…'
: text;
// Add spacing based on alignment
const remaining = innerWidth - processed.length;
switch (align) {
case 'right':
processed = ' '.repeat(remaining) + processed;
break;
case 'center':
const left = Math.floor(remaining / 2);
const right = remaining - left;
processed = ' '.repeat(left) + processed + ' '.repeat(right);
break;
default: // left
processed = processed + ' '.repeat(remaining);
}
// Add padding
return ' '.repeat(padding) + processed + ' '.repeat(padding);
}
}
exports.Table = Table;
//# sourceMappingURL=table.js.map