asciitorium
Version:
an ASCII CLUI framework
80 lines (79 loc) • 2.38 kB
JavaScript
import { Component } from '../core/Component.js';
// Base pattern to rotate from
const basePattern = [
'╭─╮╭─────',
'│╭────',
'╰─│╯',
'╭│╯',
'││',
'││',
'│',
'│',
'│'
];
// Rotation functions
function rotatePattern90(pattern) {
const maxLen = Math.max(...pattern.map(line => line.length));
const result = [];
for (let x = 0; x < maxLen; x++) {
let newLine = '';
for (let y = pattern.length - 1; y >= 0; y--) {
const char = pattern[y][x] || ' ';
newLine += rotateChar90(char);
}
result.push(newLine);
}
// Remove trailing empty/whitespace-only lines
while (result.length > 0 && result[result.length - 1].trim() === '') {
result.pop();
}
return result;
}
function rotateChar90(char) {
const rotationMap = {
'╭': '╮',
'╮': '╯',
'╯': '╰',
'╰': '╭',
'─': '│',
'│': '─',
' ': ' '
};
return rotationMap[char] || char;
}
// Generate all patterns from base
const topRight = rotatePattern90(basePattern);
const bottomRight = rotatePattern90(rotatePattern90(basePattern));
const bottomLeft = rotatePattern90(rotatePattern90(rotatePattern90(basePattern)));
const borderPatterns = {
'top-left': basePattern,
'top-right': topRight,
'bottom-right': bottomRight,
'bottom-left': bottomLeft,
};
export class CelticBorder extends Component {
constructor({ edge, ...options }) {
// Use edge if provided
let selectedEdge;
if (edge) {
selectedEdge = edge;
}
else {
throw new Error('CelticBorder requires an "edge" property that is one of: top-left, top-right, bottom-left, bottom-right');
}
const pattern = borderPatterns[selectedEdge];
const width = Math.max(...pattern.map((line) => line.length));
const height = pattern.length;
super({
width,
height,
border: false,
...options,
});
this.lines = pattern.map((line) => Array.from(line));
}
draw() {
this.buffer = Array.from({ length: this.height }, (_, y) => Array.from({ length: this.width }, (_, x) => this.lines[y]?.[x] ?? ' '));
return this.buffer;
}
}