import { Border, border } from "./border.ts";import { Cell, Direction } from "./cell.ts";import { Column, ColumnOptions } from "./column.ts";import { TableLayout } from "./_layout.ts";import { DataRow, Row, RowType } from "./row.ts";
export type BorderOptions = Partial<Border>;
export interface TableSettings { indent: number; border: boolean; minColWidth: number | Array<number>; maxColWidth: number | Array<number>; padding: number | Array<number>; chars: Border; align?: Direction; columns: Array<Column>;}
export type TableType<TRow extends RowType = RowType> = | Array<TRow> | Table<TRow>;
export class Table<TRow extends RowType = RowType> extends Array<TRow> { protected static _chars: Border = { ...border }; protected options: TableSettings = { indent: 0, border: false, maxColWidth: Infinity, minColWidth: 0, padding: 1, chars: { ...Table._chars }, columns: [], }; private headerRow?: Row;
public static from<TRow extends RowType>(rows: TableType<TRow>): Table<TRow> { const table = new this(...rows); if (rows instanceof Table) { table.options = { ...(rows as Table).options }; table.headerRow = rows.headerRow ? Row.from(rows.headerRow) : undefined; } return table; }
public static fromJson(rows: Array<DataRow>): Table { return new this().fromJson(rows); }
public static chars(chars: BorderOptions): typeof Table { Object.assign(this._chars, chars); return this; }
public static render<TRow extends RowType>(rows: TableType<TRow>): void { Table.from(rows).render(); }
public fromJson(rows: Array<DataRow>): this { this.header(Object.keys(rows[0])); this.body(rows.map((row) => Object.values(row) as TRow)); return this; }
public columns(columns: Array<Column | ColumnOptions>): this { this.options.columns = columns.map((column) => column instanceof Column ? column : Column.from(column) ); return this; }
public column( index: number, column: Column | ColumnOptions, ): this { if (column instanceof Column) { this.options.columns[index] = column; } else if (this.options.columns[index]) { this.options.columns[index].options(column); } else { this.options.columns[index] = Column.from(column); } return this; }
public header(header: RowType): this { this.headerRow = header instanceof Row ? header : Row.from(header); return this; }
public body(rows: Array<TRow>): this { this.length = 0; this.push(...rows); return this; }
public clone(): Table { const table = new Table( ...this.map((row: TRow) => row instanceof Row ? row.clone() : Row.from(row).clone() ), ); table.options = { ...this.options }; table.headerRow = this.headerRow?.clone(); return table; }
public toString(): string { return new TableLayout(this, this.options).toString(); }
public render(): this { console.log(this.toString()); return this; }
public maxColWidth(width: number | Array<number>, override = true): this { if (override || typeof this.options.maxColWidth === "undefined") { this.options.maxColWidth = width; } return this; }
public minColWidth(width: number | Array<number>, override = true): this { if (override || typeof this.options.minColWidth === "undefined") { this.options.minColWidth = width; } return this; }
public indent(width: number, override = true): this { if (override || typeof this.options.indent === "undefined") { this.options.indent = width; } return this; }
public padding(padding: number | Array<number>, override = true): this { if (override || typeof this.options.padding === "undefined") { this.options.padding = padding; } return this; }
public border(enable = true, override = true): this { if (override || typeof this.options.border === "undefined") { this.options.border = enable; } return this; }
public align(direction: Direction, override = true): this { if (override || typeof this.options.align === "undefined") { this.options.align = direction; } return this; }
public chars(chars: BorderOptions): this { Object.assign(this.options.chars, chars); return this; }
public getHeader(): Row | undefined { return this.headerRow; }
public getBody(): Array<TRow> { return [...this]; }
public getMaxColWidth(): number | Array<number> { return this.options.maxColWidth; }
public getMinColWidth(): number | Array<number> { return this.options.minColWidth; }
public getIndent(): number { return this.options.indent; }
public getPadding(): number | Array<number> { return this.options.padding; }
public getBorder(): boolean { return this.options.border === true; }
public hasHeaderBorder(): boolean { const hasBorder = this.headerRow?.hasBorder(); return hasBorder === true || (this.getBorder() && hasBorder !== false); }
public hasBodyBorder(): boolean { return this.getBorder() || this.options.columns.some((column) => column.getBorder()) || this.some((row) => row instanceof Row ? row.hasBorder() : row.some((cell) => cell instanceof Cell ? cell.getBorder() : false) ); }
public hasBorder(): boolean { return this.hasHeaderBorder() || this.hasBodyBorder(); }
public getAlign(): Direction { return this.options.align ?? "left"; }
public getColumns(): Array<Column> { return this.options.columns; }
public getColumn(index: number): Column { return this.options.columns[index] ??= new Column(); }}
export type IBorderOptions = BorderOptions;
export type ITable = TableType;