Module
Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175import { border, IBorder } from "./border.ts";import { Cell } from "./cell.ts";import { TableLayout } from "./layout.ts";import { IDataRow, IRow, Row } from "./row.ts";
export type IBorderOptions = Partial<IBorder>;
export interface ITableOptions { indent?: number; border?: boolean; maxColWidth?: number | number[]; minColWidth?: number | number[]; padding?: number | number[]; chars?: IBorderOptions;}
export interface ITableSettings extends Required<ITableOptions> { chars: IBorder;}
export type ITable<T extends IRow = IRow> = T[] | Table<T>;
export class Table<T extends IRow = IRow> extends Array<T> { protected options: ITableSettings = { indent: 0, border: false, maxColWidth: Infinity, minColWidth: 0, padding: 1, chars: border, }; private headerRow?: Row;
public static from<T extends IRow>(rows: ITable<T>): Table<T> { const table = new this(...rows); if (rows instanceof Table) { table.options = Object.assign({}, rows.options); table.headerRow = rows.headerRow ? Row.from(rows.headerRow) : undefined; } return table; }
public static fromJson(rows: IDataRow[]): Table { return new this().fromJson(rows); }
public static render<T extends IRow>(rows: ITable<T>): void { Table.from(rows).render(); }
public fromJson(rows: IDataRow[]): this { this.header(Object.keys(rows[0])); this.body(rows.map((row) => Object.values(row) as T)); return this; }
public header(header: IRow): this { this.headerRow = header instanceof Row ? header : Row.from(header); return this; }
public body(rows: T[]): this { this.length = 0; this.push(...rows); return this; }
public clone(): Table { const table = new Table( ...this.map((row: T) => row instanceof Row ? row.clone() : Row.from(row).clone() ), ); table.options = Object.assign({}, this.options); table.headerRow = this.headerRow?.clone(); return table; }
public toString(): string { return new TableLayout(this, this.options).toString(); }
public render(): this { Deno.stdout.writeSync(new TextEncoder().encode(this.toString() + "\n")); return this; }
public maxColWidth(width: number | number[], override = true): this { if (override || typeof this.options.maxColWidth === "undefined") { this.options.maxColWidth = width; } return this; }
public minColWidth(width: number | 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 | number[], override = true): this { if (override || typeof this.options.padding === "undefined") { this.options.padding = padding; } return this; }
public border(enable: boolean, override = true): this { if (override || typeof this.options.border === "undefined") { this.options.border = enable; } return this; }
public chars(chars: IBorderOptions): this { Object.assign(this.options.chars, chars); return this; }
public getHeader(): Row | undefined { return this.headerRow; }
public getBody(): T[] { return this.slice(); }
public getMaxColWidth(): number | number[] { return this.options.maxColWidth; }
public getMinColWidth(): number | number[] { return this.options.minColWidth; }
public getIndent(): number { return this.options.indent; }
public getPadding(): number | number[] { return this.options.padding; }
public getBorder(): boolean { return this.options.border === true; }
public hasHeaderBorder(): boolean { return this.getBorder() || ( this.headerRow instanceof Row && this.headerRow.hasBorder() ); }
public hasBodyBorder(): boolean { return this.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(); }}