layoutz
Version:
Friendly, expressive print-layout DSL for JavaScript/TypeScript
535 lines (533 loc) • 14.9 kB
text/typescript
declare const DIMENSIONS: {
readonly MIN_CONTENT_PADDING: 2;
readonly BORDER_THICKNESS: 2;
readonly SIDE_PADDING: 2;
readonly PROGRESS_BAR_WIDTH: 20;
readonly TREE_INDENTATION: 4;
readonly TREE_CONNECTOR_SPACING: 3;
readonly DEFAULT_RULE_WIDTH: 50;
readonly DEFAULT_CHART_WIDTH: 40;
readonly CHART_LABEL_MAX_WIDTH: 15;
readonly CHART_LABEL_SPACING: 15;
readonly BOX_INNER_PADDING: 4;
readonly BOX_BORDER_WIDTH: 2;
};
declare const GLYPHS: {
readonly TOP_LEFT: "┌";
readonly TOP_RIGHT: "┐";
readonly BOTTOM_LEFT: "└";
readonly BOTTOM_RIGHT: "┘";
readonly HORIZONTAL: "─";
readonly VERTICAL: "│";
readonly CROSS: "┼";
readonly TEE_DOWN: "┬";
readonly TEE_UP: "┴";
readonly TEE_RIGHT: "├";
readonly TEE_LEFT: "┤";
readonly BULLET: "•";
readonly SPACE: " ";
readonly BAR_FILLED: "█";
readonly BAR_EMPTY: "─";
readonly TREE_BRANCH: "├──";
readonly TREE_LAST_BRANCH: "└──";
readonly TREE_VERTICAL: "│";
readonly TREE_INDENT: string;
};
/**
* Strip ANSI escape sequences to get visual width
*/
declare function stripAnsiCodes(text: string): string;
/**
* Core layout element interface
*/
interface Element {
render(): string;
}
/**
* Get the visual width of an element (longest line, excluding ANSI codes)
*/
declare function getWidth(element: Element): number;
/**
* Get the height of an element (number of lines)
*/
declare function getHeight(element: Element): number;
/**
* Simple text element
*/
declare class Text implements Element {
private content;
constructor(content: string);
render(): string;
}
/**
* Line break element
*/
declare class LineBreak implements Element {
render(): string;
}
/**
* Horizontal rule
*/
declare class HorizontalRule implements Element {
private char;
private ruleWidth?;
constructor(char?: string, ruleWidth?: number | undefined);
render(): string;
}
/**
* Border styles for box-like elements
*/
declare enum BorderStyle {
Single = "single",
Double = "double",
Thick = "thick",
Round = "round"
}
/**
* Border enum for fluent API (Scala-style)
*/
declare const Border: {
readonly Single: BorderStyle.Single;
readonly Double: BorderStyle.Double;
readonly Thick: BorderStyle.Thick;
readonly Round: BorderStyle.Round;
};
type BorderType = (typeof Border)[keyof typeof Border];
type BorderChars = {
topLeft: string;
topRight: string;
bottomLeft: string;
bottomRight: string;
horizontal: string;
vertical: string;
};
/**
* Key-value pairs with aligned formatting
*/
declare class KeyValue implements Element {
private pairs;
constructor(pairs: Array<[string, string]>);
render(): string;
}
/**
* Unordered list with bullet points
*/
declare class UnorderedList implements Element {
private items;
private bullet;
private static readonly BULLET_STYLES;
constructor(items: Element[], bullet?: string);
render(): string;
private renderAtLevel;
}
/**
* Tree structure for hierarchical displays
*/
declare class Tree implements Element {
private label;
private children;
constructor(label: string, children?: Tree[]);
render(): string;
private renderAtLevel;
private buildPrefix;
}
/**
* Ordered list with numbered items
*/
declare class OrderedList implements Element {
private items;
constructor(items: Element[]);
render(): string;
private renderAtLevel;
private getNumbering;
private toRomanNumeral;
}
/**
* Box container with optional title and borders
*/
declare class Box implements Element {
private elements;
private title;
private style;
constructor(elements: Element[], title?: string, style?: BorderStyle);
/**
* Set the border style (fluent API)
*/
border(style: BorderType): Box;
render(): string;
}
/**
* Section with title header
*/
declare class Section implements Element {
private title;
private content;
private glyph;
private flankingChars;
constructor(title: string, content: Element, glyph?: string, flankingChars?: number);
render(): string;
}
/**
* Horizontal layout of elements
*/
declare class Row implements Element {
private elements;
constructor(elements: Element[]);
render(): string;
}
/**
* Vertical layout container
*/
declare class Layout implements Element {
private elements;
constructor(elements: Element[]);
render(): string;
}
/**
* Progress bar element
*/
declare class InlineBar implements Element {
private label;
private progress;
constructor(label: Element, progress: number);
render(): string;
}
/**
* Status card widget
*/
declare class StatusCard implements Element {
private label;
private content;
private style;
constructor(label: Element, content: Element, style?: BorderStyle);
/**
* Set the border style (fluent API)
*/
border(style: BorderType): StatusCard;
render(): string;
}
/**
* Table with headers and borders
*/
declare class Table implements Element {
private headers;
private rows;
private style;
constructor(headers: Element[], rows: Element[][], style?: BorderStyle);
/**
* Set the border style (fluent API)
*/
border(style: BorderType): Table;
render(): string;
private calculateColumnWidths;
private createTableBorders;
private getJunctionChars;
private buildMultilineTableRows;
}
/**
* Create a text element
*/
declare function text(content: string): Text;
/**
* Create a vertical layout
*/
declare function layout(...elements: (string | Element)[]): Layout;
/**
* Create a section with title
*/
declare function section(title: string, glyph?: string, flankingChars?: number): (content: string | Element) => Section;
/**
* Create key-value pairs
*/
declare function kv(...pairs: Array<[string, string]>): KeyValue;
/**
* Create an unordered list
*/
declare function ul(...items: (string | Element)[]): UnorderedList;
/**
* Create an ordered list
*/
declare function ol(...items: Element[]): OrderedList;
/**
* Create a box container (non-curried API)
*/
declare function box(title?: string, style?: BorderStyle): (...elements: (string | Element)[]) => Box;
/**
* Create a horizontal row
*/
declare function row(...elements: (string | Element)[]): Row;
/**
* Create a horizontal rule
*/
declare function hr(char?: string, width?: number): HorizontalRule;
/**
* Create an inline bar
*/
declare function inlineBar(label: string | Element, progress: number): InlineBar;
/**
* Create a status card (non-curried API)
*/
declare function statusCard(label: string | Element, content: string | Element, style?: BorderStyle): StatusCard;
/**
* Create a table (non-curried API)
*/
declare function table(headers: (string | Element)[], rows: (string | Element)[][], style?: BorderStyle): Table;
/**
* Line break
*/
declare function br(count?: number): LineBreak | Layout;
/**
* Center element within specified width
*/
declare class Center implements Element {
private element;
private width?;
constructor(element: Element, width?: number | undefined);
render(): string;
}
/**
* Center an element
*/
declare function center(element: string | Element, width?: number): Center;
/**
* Add underline to an element with custom character
*/
declare function underline(char?: string): (element: string | Element) => Underline;
/**
* Add a prefix margin to elements
*/
declare function margin(prefix: string): (...elements: (string | Element)[]) => Margin;
/**
* Create a tree structure - can be used as both a Tree and a function
*/
declare function tree(label: string): any;
/**
* Create a horizontal bar chart
*/
declare function chart(...data: Array<[string | Element, number]>): Chart;
/**
* Create a decorative banner
*/
declare function banner(content?: string | Element): Banner;
/**
* Arrange elements in columns with spacing
*/
declare function columns(...elements: (string | Element)[]): Columns;
declare function columns(spacing: number, ...elements: (string | Element)[]): Columns;
/**
* Add padding around an element
*/
declare function pad(padding: number): (element: string | Element) => Padded;
/**
* Truncate text with ellipsis if it exceeds max width
*/
declare function truncate(maxWidth: number, ellipsis?: string): (element: string | Element) => Truncated;
/**
* Create vertical separator
*/
declare function vr(lineCount: number, char?: string): VerticalRule;
/**
* Wrap text at word boundaries within specified width
*/
declare function wrap(maxWidth: number): (element: string | Element) => Wrapped;
/**
* Justify text to exact width by distributing spaces
*/
declare function justify(targetWidth: number, justifyLastLine?: boolean): (element: string | Element) => Justified;
/**
* Left-align element within specified width
*/
declare function leftAlign(targetWidth: number): (element: string | Element) => LeftAligned;
/**
* Right-align element within specified width
*/
declare function rightAlign(targetWidth: number): (element: string | Element) => RightAligned;
/**
* Auto-center element within layout context
*/
declare function autoCenter(element: string | Element): AutoCentered;
/**
* Empty element for conditional rendering
*/
declare function empty(): Empty;
/**
* Predefined status margins with color coding
*/
declare const margins: {
error: (...elements: (string | Element)[]) => Margin;
warn: (...elements: (string | Element)[]) => Margin;
success: (...elements: (string | Element)[]) => Margin;
info: (...elements: (string | Element)[]) => Margin;
};
/**
* Underline element that adds underline below content
*/
declare class Underline implements Element {
private element;
private char;
constructor(element: Element, char?: string);
render(): string;
}
/**
* Margin element that adds prefix to each line
*/
declare class Margin implements Element {
private prefix;
private elements;
constructor(prefix: string, elements: Element[]);
render(): string;
}
/**
* Chart - horizontal bar chart component
*/
declare class Chart implements Element {
private data;
constructor(data: Array<[Element, number]>);
render(): string;
}
/**
* Banner - decorative text in a box with fluent API
*/
declare class Banner implements Element {
private content;
private style;
constructor(content: Element, style?: BorderStyle);
/**
* Set the border style (fluent API)
*/
border(style: BorderType): Banner;
render(): string;
}
/**
* Columns - arrange elements in columns with spacing
*/
declare class Columns implements Element {
private elements;
private spacing;
constructor(elements: Element[], spacing?: number);
render(): string;
}
/**
* Padded - add padding around an element
*/
declare class Padded implements Element {
private element;
private padding;
constructor(element: Element, padding: number);
render(): string;
}
/**
* Truncated - truncate text with ellipsis if it exceeds max width
*/
declare class Truncated implements Element {
private element;
private maxWidth;
private ellipsis;
constructor(element: Element, maxWidth: number, ellipsis?: string);
render(): string;
}
/**
* VerticalRule - vertical separator line
*/
declare class VerticalRule implements Element {
private char;
private lineCount;
constructor(char: string | undefined, lineCount: number);
render(): string;
}
/**
* Wrapped - text wrapping at word boundaries
*/
declare class Wrapped implements Element {
private element;
private maxWidth;
constructor(element: Element, maxWidth: number);
render(): string;
}
/**
* Justified - justify text to exact width by distributing spaces
*/
declare class Justified implements Element {
private element;
private targetWidth;
private justifyLastLine;
constructor(element: Element, targetWidth: number, justifyLastLine?: boolean);
render(): string;
}
/**
* LeftAligned - left align element within specified width
*/
declare class LeftAligned implements Element {
private element;
private targetWidth;
constructor(element: Element, targetWidth: number);
render(): string;
}
/**
* RightAligned - right align element within specified width
*/
declare class RightAligned implements Element {
private element;
private targetWidth;
constructor(element: Element, targetWidth: number);
render(): string;
}
/**
* AutoCentered - auto center based on layout context
*/
declare class AutoCentered implements Element {
private element;
constructor(element: Element);
render(): string;
}
/**
* Empty - empty element for conditional rendering
*/
declare class Empty implements Element {
render(): string;
}
declare const dsl: {
layout: typeof layout;
text: typeof text;
box: typeof box;
section: typeof section;
banner: typeof banner;
row: typeof row;
center: typeof center;
autoCenter: typeof autoCenter;
margin: typeof margin;
columns: typeof columns;
ul: typeof ul;
ol: typeof ol;
kv: typeof kv;
table: typeof table;
tree: typeof tree;
chart: typeof chart;
statusCard: typeof statusCard;
inlineBar: typeof inlineBar;
hr: typeof hr;
vr: typeof vr;
underline: typeof underline;
br: typeof br;
pad: typeof pad;
truncate: typeof truncate;
wrap: typeof wrap;
justify: typeof justify;
leftAlign: typeof leftAlign;
rightAlign: typeof rightAlign;
empty: typeof empty;
Border: {
readonly Single: BorderStyle.Single;
readonly Double: BorderStyle.Double;
readonly Thick: BorderStyle.Thick;
readonly Round: BorderStyle.Round;
};
BorderStyle: typeof BorderStyle;
margins: {
error: (...elements: (string | Element)[]) => Margin;
warn: (...elements: (string | Element)[]) => Margin;
success: (...elements: (string | Element)[]) => Margin;
info: (...elements: (string | Element)[]) => Margin;
};
};
export { AutoCentered, Banner, Border, type BorderChars, BorderStyle, type BorderType, Box, Center, Chart, Columns, DIMENSIONS, type Element, Empty, GLYPHS, HorizontalRule, InlineBar, Justified, KeyValue, Layout, LeftAligned, LineBreak, Margin, OrderedList, Padded, RightAligned, Row, Section, StatusCard, Table, Text, Tree, Truncated, Underline, UnorderedList, VerticalRule, Wrapped, autoCenter, banner, box, br, center, chart, columns, dsl, empty, getHeight, getWidth, hr, inlineBar, justify, kv, layout, leftAlign, margin, margins, ol, pad, rightAlign, row, section, statusCard, stripAnsiCodes, table, text, tree, truncate, ul, underline, vr, wrap };