Something went wrong. Try again.
Shared TUI abstractions for Pi extensions
Something went wrong. Try again.
1.6 kB · 64 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465import type { Component } from "@earendil-works/pi-tui";import { Spacer } from "@earendil-works/pi-tui";
export type StackOptions = { /** Blank lines between children (default: 0) */ gap?: number; /** Rendered between children instead of gap */ separator?: Component;};
export class Stack implements Component { private children: Component[] = []; private gap: number; private separator?: Component;
constructor(options?: StackOptions) { this.gap = options?.gap ?? 0; this.separator = options?.separator; }
addChild(component: Component): void { this.children.push(component); }
removeChild(component: Component): void { const idx = this.children.indexOf(component); if (idx !== -1) this.children.splice(idx, 1); }
clear(): void { this.children = []; }
render(width: number): string[] { if (this.children.length === 0) return [];
const lines: string[] = [];
for (let i = 0; i < this.children.length; i++) { const child = this.children[i]!; const childLines = child.render(width); lines.push(...childLines);
// Add gap or separator between children (not after last) if (i < this.children.length - 1) { if (this.separator) { lines.push(...this.separator.render(width)); } else if (this.gap > 0) { const spacer = new Spacer(this.gap); lines.push(...spacer.render(width)); } } }
return lines; }
invalidate(): void { for (const child of this.children) { child.invalidate(); } this.separator?.invalidate(); }}