Something went wrong. Try again.
Shared TUI abstractions for Pi extensions
Something went wrong. Try again.
2.7 kB · 99 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import type { Component } from "@earendil-works/pi-tui";import { truncateToWidth } from "@earendil-works/pi-tui";
export type FieldOptions = { label: string | Component; input: Component; description?: string | Component; error?: string | Component; required?: boolean; labelStyle?: (text: string) => string; errorStyle?: (text: string) => string; descriptionStyle?: (text: string) => string; requiredStyle?: (text: string) => string;};
export class Field implements Component { private label: string | Component; private input: Component; private description?: string | Component; private error?: string | Component; private required: boolean; private labelStyle: (text: string) => string; private errorStyle: (text: string) => string; private descriptionStyle: (text: string) => string; private requiredStyle: (text: string) => string;
constructor(options: FieldOptions) { this.label = options.label; this.input = options.input; this.description = options.description; this.error = options.error; this.required = options.required ?? false; this.labelStyle = options.labelStyle ?? ((t: string) => t); this.descriptionStyle = options.descriptionStyle ?? ((t: string) => t); this.errorStyle = options.errorStyle ?? ((t: string) => `\x1b[31m${t}\x1b[0m`); this.requiredStyle = options.requiredStyle ?? ((t: string) => `\x1b[31m${t}\x1b[0m`); }
setError(error?: string | Component): void { this.error = error; }
render(width: number): string[] { const lines: string[] = [];
// Label line const requiredMarker = this.required ? this.requiredStyle(" *") : ""; if (typeof this.label === "string") { lines.push( truncateToWidth( this.labelStyle(this.label) + requiredMarker, width, "", true, ), ); } else { lines.push(...this.label.render(width)); }
// Input lines.push(...this.input.render(width));
// Description if (this.description) { if (typeof this.description === "string") { lines.push( truncateToWidth( this.descriptionStyle(this.description), width, "", true, ), ); } else { lines.push(...this.description.render(width)); } }
// Error if (this.error) { if (typeof this.error === "string") { lines.push( truncateToWidth(this.errorStyle(this.error), width, "", true), ); } else { lines.push(...this.error.render(width)); } }
return lines; }
invalidate(): void { this.input.invalidate(); }}