Add typings for cli-table2 (#16641)

* Add typings for cli-table2

* Process review comments
This commit is contained in:
Melvin Groenhoff
2017-06-02 17:41:52 +02:00
committed by Andy
parent f34617c7e6
commit 421222d812
4 changed files with 358 additions and 0 deletions

View File

@@ -0,0 +1,232 @@
import * as Table from "cli-table2";
// Code snippets from README.md
// Horizontal table
const table1 = new Table({ head: ['TH 1 label', 'TH 2 label'], colWidths: [100, 200] }) as Table.HorizontalTable;
table1.push(
['First value', 'Second value'],
['First value', 'Second value']
);
// Vertical table
const table2 = new Table() as Table.VerticalTable;
table2.push(
{ 'Some key': 'Some value' },
{ 'Another key': 'Another value' }
);
// Cross table
const table3 = new Table({ head: ["", "Top Header 1", "Top Header 2"] }) as Table.CrossTable;
table3.push(
{ 'Left Header 1': ['Value Row 1 Col 1', 'Value Row 1 Col 2'] },
{ 'Left Header 2': ['Value Row 2 Col 1', 'Value Row 2 Col 2'] }
);
// Custom styles
const table4 = new Table({
chars: {
top: '═', 'top-mid': '╤', 'top-left': '╔', 'top-right': '╗',
bottom: '═', 'bottom-mid': '╧', 'bottom-left': '╚', 'bottom-right': '╝',
left: '║', 'left-mid': '╟', mid: '─', 'mid-mid': '┼',
right: '║', 'right-mid': '╢', middle: '│'
}
}) as Table.HorizontalTable;
table4.push(
['foo', 'bar', 'baz'],
['frob', 'bar', 'quuz']
);
const table5 = new Table({ chars: { mid: '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' } }) as Table.HorizontalTable;
table5.push(
['foo', 'bar', 'baz'],
['frobnicate', 'bar', 'quuz']
);
const table6 = new Table({
chars: {
top: '', 'top-mid': '', 'top-left': '', 'top-right': '',
bottom: '', 'bottom-mid': '', 'bottom-left': '', 'bottom-right': '',
left: '', 'left-mid': '', mid: '', 'mid-mid': '',
right: '', 'right-mid': '', middle: ' '
},
style: { 'padding-left': 0, 'padding-right': 0 }
}) as Table.HorizontalTable;
table6.push(
['foo', 'bar', 'baz'],
['frobnicate', 'bar', 'quuz']
);
// Code snippets from basic-usage.md
// Basic Usage
const table7 = new Table({ head: ['a', 'b'] }) as Table.HorizontalTable;
table7.push(['c', 'd']);
// Basic Usage - disable colors - (used often in the examples and tests)
const table8 = new Table({
head: ['Rel', 'Change', 'By', 'When'],
style: { head: [], border: [] },
colWidths: [6, 21, 25, 17]
}) as Table.HorizontalTable;
table8.push(
['v0.1', 'Testing something cool', 'rauchg@gmail.com', '7 minutes ago'],
['v0.1', 'Testing something cool', 'rauchg@gmail.com', '8 minutes ago']
);
// Create vertical tables by adding objects a that specify key-value pairs
const table9 = new Table({ style: { 'padding-left': 0, 'padding-right': 0, head: [], border: [] } }) as Table.VerticalTable;
table9.push(
{ 'v0.1': 'Testing something cool' },
{ 'v0.1': 'Testing something cool' }
);
// Cross tables are similar to vertical tables, but include an empty string for the first header
const table10 = new Table({
head: ["", "Header 1", "Header 2"],
style: { 'padding-left': 0, 'padding-right': 0, head: [], border: [] }
}) as Table.CrossTable;
table10.push(
{ "Header 3": ['v0.1', 'Testing something cool'] },
{ "Header 4": ['v0.1', 'Testing something cool'] }
);
// Stylize the table with custom border characters
const table11 = new Table({
chars: {
top: '═',
'top-mid': '╤',
'top-left': '╔',
'top-right': '╗',
bottom: '═',
'bottom-mid': '╧',
'bottom-left': '╚',
'bottom-right': '╝',
left: '║',
'left-mid': '╟',
right: '║',
'right-mid': '╢'
},
style: { head: [], border: [] }
}) as Table.HorizontalTable;
table11.push(
['foo', 'bar', 'baz'],
['frob', 'bar', 'quuz']
);
// Use ansi colors (i.e. colors.js) to style text within the cells at will, even across multiple lines
const table12 = new Table({ style: { border: [], head: [] } }) as Table.HorizontalTable;
table12.push([
/*colors.red(*/'Hello\nhow\nare\nyou?'/*)*/,
/*colors.blue(*/'I\nam\nfine\nthanks!'/*)*/
]);
// Set wordWrap to true to make lines of text wrap instead of being truncated
const table13 = new Table({
style: { border: [], head: [] },
colWidths: [7, 9],
wordWrap: true
}) as Table.HorizontalTable;
table13.push([
'Hello how are you?',
'I am fine thanks!'
]);
// Code snippets from advanced-usage.md
// use colSpan to span columns - (colSpan above normal cell)
const table14 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table14.push(
[{ colSpan: 2, content: 'greetings' }],
[{ colSpan: 2, content: 'greetings' }],
['hello', 'howdy']
);
// use colSpan to span columns - (colSpan below normal cell)
const table15 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table15.push(
['hello', 'howdy'],
[{ colSpan: 2, content: 'greetings' }],
[{ colSpan: 2, content: 'greetings' }]
);
// use rowSpan to span rows - (rowSpan on the left side)
const table16 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table16.push(
[{ rowSpan: 2, content: 'greetings' }, { rowSpan: 2, content: 'greetings', vAlign: 'center' }, 'hello'],
['howdy']
);
// use rowSpan to span rows - (rowSpan on the right side)
const table17 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table17.push(
['hello', { rowSpan: 2, content: 'greetings' }, { rowSpan: 2, content: 'greetings', vAlign: 'bottom' }],
['howdy']
);
// mix rowSpan and colSpan together for complex table layouts
const table18 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table18.push(
[{ content: 'hello', colSpan: 2 }, { rowSpan: 2, colSpan: 2, content: 'sup' }, { rowSpan: 3, content: 'hi' }],
[{ content: 'howdy', colSpan: 2 }],
['o', 'k', '', '']
);
// multi-line content will flow across rows in rowSpan cells
const table19 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table19.push(
['hello', { rowSpan: 2, content: 'greetings\nfriends' }, { rowSpan: 2, content: 'greetings\nfriends' }],
['howdy']
);
// multi-line content will flow across rows in rowSpan cells - (complex layout)
const table20 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table20.push(
[{ content: 'hello', colSpan: 2 }, { rowSpan: 2, colSpan: 2, content: 'sup\nman\nhey' }, { rowSpan: 3, content: 'hi\nyo' }],
[{ content: 'howdy', colSpan: 2 }],
['o', 'k', '', '']
);
// rowSpan cells can have a staggered layout
const table21 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table21.push(
[{ content: 'a', rowSpan: 2 }, 'b'],
[{ content: 'c', rowSpan: 2 }],
['d']
);
// the layout manager automatically create empty cells to fill in the table
const table22 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table22.push(
[{ content: 'a', rowSpan: 3, colSpan: 2 }, 'b'],
[],
[{ content: 'c', rowSpan: 2, colSpan: 2 }],
[]
);
// use table rowHeights option to fix row height. The truncation symbol will be shown on the last line.
const table23 = new Table({ rowHeights: [2], style: { head: [], border: [] } }) as Table.HorizontalTable;
table23.push(['hello\nhi\nsup']);
// if colWidths is not specified, the layout manager will automatically widen rows to fit the content
const table24 = new Table({ style: { head: [], border: [] } }) as Table.HorizontalTable;
table24.push(
[{ colSpan: 2, content: 'hello there' }],
['hi', 'hi']
);
// you can specify a column width for only the first row, other rows will be automatically widened to fit content
const table25 = new Table({ colWidths: [4], style: { head: [], border: [] } }) as Table.HorizontalTable;
table25.push(
[{ colSpan: 2, content: 'hello there' }],
['hi', { hAlign: 'center', content: 'hi' }]
);
// a column with a null column width will be automatically widened to fit content
const table26 = new Table({ colWidths: [null, 4], style: { head: [], border: [] } }) as Table.HorizontalTable;
table26.push(
[{ colSpan: 2, content: 'hello there' }],
[{ hAlign: 'right', content: 'hi' }, 'hi']
);
// feel free to use colors in your content strings, column widths will be calculated correctly
const table27 = new Table({ colWidths: [5], style: { head: [], border: [] } }) as Table.HorizontalTable;
table27.push([/*colors.red(*/'hello'/*)*/]);

101
types/cli-table2/index.d.ts vendored Normal file
View File

@@ -0,0 +1,101 @@
// Type definitions for cli-table2 0.2
// Project: https://github.com/jamestalmage/cli-table2
// Definitions by: Melvin Groenhoff <https://github.com/mgroenhoff>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
declare namespace CliTable2 {
type CharName =
"top" |
"top-mid" |
"top-left" |
"top-right" |
"bottom" |
"bottom-mid" |
"bottom-left" |
"bottom-right" |
"left" |
"left-mid" |
"mid" |
"mid-mid" |
"right" |
"right-mid" |
"middle";
type HorizontalAlignment = "left" | "center" | "right";
type VerticalAlignment = "top" | "center" | "bottom";
interface TableOptions {
truncate: string;
colWidths: Array<number | null>;
rowHeights: Array<number | null>;
colAligns: HorizontalAlignment[];
rowAligns: VerticalAlignment[];
head: string[];
wordWrap: boolean;
}
interface TableInstanceOptions extends TableOptions {
chars: Record<CharName, string>;
style: {
"padding-left": number;
"padding-right": number;
head: string[];
border: string[];
compact: boolean;
};
}
interface TableConstructorOptions extends Partial<TableOptions> {
chars?: Partial<Record<CharName, string>>;
style?: Partial<TableInstanceOptions["style"]>;
}
type CellValue = boolean | number | string | null | undefined;
interface CellOptions {
content: CellValue;
chars?: Partial<Record<CharName, string>>;
truncate?: string;
colSpan?: number;
rowSpan?: number;
hAlign?: HorizontalAlignment;
vAlign?: VerticalAlignment;
style?: {
"padding-left"?: number;
"padding-right"?: number;
head?: string[];
border?: string[];
};
}
interface GenericTable<T> extends Array<T> {
options: CliTable2.TableInstanceOptions;
readonly width: number;
}
type Table = HorizontalTable | VerticalTable | CrossTable;
type Cell = CellValue | CellOptions;
type HorizontalTable = GenericTable<HorizontalTableRow>;
type HorizontalTableRow = Cell[];
type VerticalTable = GenericTable<VerticalTableRow>;
interface VerticalTableRow {
[name: string]: Cell;
}
type CrossTable = GenericTable<CrossTableRow>;
interface CrossTableRow {
[name: string]: Cell[];
}
}
interface CliTable2 {
new (options?: CliTable2.TableConstructorOptions): CliTable2.Table;
readonly prototype: CliTable2.Table;
}
declare const CliTable2: CliTable2;
export = CliTable2;

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"cli-table2-tests.ts"
]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}