From 7f91641db96dc42770525baaf3d3c1856d7f8ccc Mon Sep 17 00:00:00 2001 From: Opportunity Date: Sat, 11 Jan 2020 07:07:03 +0800 Subject: [PATCH] Add types for cli-color 2.0 (#41358) --- types/cli-color/art.d.ts | 5 ++ types/cli-color/bare.d.ts | 55 ++++++++++++ types/cli-color/beep.d.ts | 2 + types/cli-color/cli-color-tests.ts | 102 ++++++++++++++++++----- types/cli-color/columns.d.ts | 29 +++++++ types/cli-color/erase.d.ts | 29 +++++++ types/cli-color/get-stripped-length.d.ts | 5 ++ types/cli-color/index.d.ts | 96 +++++++-------------- types/cli-color/move.d.ts | 56 +++++++++++++ types/cli-color/reset.d.ts | 5 ++ types/cli-color/slice.d.ts | 5 ++ types/cli-color/strip.d.ts | 5 ++ types/cli-color/throbber.d.ts | 15 +++- types/cli-color/trim.d.ts | 2 - types/cli-color/window-size.d.ts | 13 +++ 15 files changed, 331 insertions(+), 93 deletions(-) create mode 100644 types/cli-color/art.d.ts create mode 100644 types/cli-color/bare.d.ts create mode 100644 types/cli-color/beep.d.ts create mode 100644 types/cli-color/columns.d.ts create mode 100644 types/cli-color/erase.d.ts create mode 100644 types/cli-color/get-stripped-length.d.ts create mode 100644 types/cli-color/move.d.ts create mode 100644 types/cli-color/reset.d.ts create mode 100644 types/cli-color/slice.d.ts create mode 100644 types/cli-color/strip.d.ts delete mode 100644 types/cli-color/trim.d.ts create mode 100644 types/cli-color/window-size.d.ts diff --git a/types/cli-color/art.d.ts b/types/cli-color/art.d.ts new file mode 100644 index 0000000000..410dc9f9f5 --- /dev/null +++ b/types/cli-color/art.d.ts @@ -0,0 +1,5 @@ +/** + * Create a text-graphical art. Within *styleConf*, string replacements needs to be defined, which are then used to convert *text* to styled graphical text. + */ +declare function art(text: string, styleConf: Record): string; +export = art; diff --git a/types/cli-color/bare.d.ts b/types/cli-color/bare.d.ts new file mode 100644 index 0000000000..38017765bd --- /dev/null +++ b/types/cli-color/bare.d.ts @@ -0,0 +1,55 @@ + +declare namespace bare { + export interface Format { + (...text: any[]): string; + + readonly bold: Format; + readonly italic: Format; + readonly underline: Format; + readonly blink: Format; + readonly inverse: Format; + readonly strike: Format; + + readonly black: Format; + readonly red: Format; + readonly green: Format; + readonly yellow: Format; + readonly blue: Format; + readonly magenta: Format; + readonly cyan: Format; + readonly white: Format; + + readonly bgBlack: Format; + readonly bgRed: Format; + readonly bgGreen: Format; + readonly bgYellow: Format; + readonly bgBlue: Format; + readonly bgMagenta: Format; + readonly bgCyan: Format; + readonly bgWhite: Format; + + readonly blackBright: Format; + readonly redBright: Format; + readonly greenBright: Format; + readonly yellowBright: Format; + readonly blueBright: Format; + readonly magentaBright: Format; + readonly cyanBright: Format; + readonly whiteBright: Format; + + readonly bgBlackBright: Format; + readonly bgRedBright: Format; + readonly bgGreenBright: Format; + readonly bgYellowBright: Format; + readonly bgBlueBright: Format; + readonly bgMagentaBright: Format; + readonly bgCyanBright: Format; + readonly bgWhiteBright: Format; + + xterm(color: number): Format; + bgXterm(color: number): Format; + readonly xtermSupported: boolean; + } +} +declare const bare: bare.Format; +export = bare; diff --git a/types/cli-color/beep.d.ts b/types/cli-color/beep.d.ts new file mode 100644 index 0000000000..5528f3605e --- /dev/null +++ b/types/cli-color/beep.d.ts @@ -0,0 +1,2 @@ +declare const beep: string; +export = beep; diff --git a/types/cli-color/cli-color-tests.ts b/types/cli-color/cli-color-tests.ts index d6f8897720..b615118884 100644 --- a/types/cli-color/cli-color-tests.ts +++ b/types/cli-color/cli-color-tests.ts @@ -2,54 +2,110 @@ /// import clc = require('cli-color'); -import ansiTrim = require('cli-color/trim'); +import ansiStrip = require('cli-color/strip'); import setupThrobber = require('cli-color/throbber'); +import bare = require('cli-color/bare'); -var text: string; -var color: number; -var x: number; -var y: number; -var n: number; -var period: number; +let text: string; +let period: number; +let format: clc.Format; +format = bare.red.redBright; +text = format(); // Test cli-color text = clc('foo'); text = clc('foo', 42, { toString: () => 'bar' }); +text = clc.red("Text in red"); +text = clc.red.bgWhite.underline("Underlined red text on white background."); +text = clc.red("red") + " plain " + clc.blue("blue"); +text = clc.red("red " + clc.blue("blue") + " red"); text = clc.bold.italic.underline.blink.inverse.strike(text); text = clc.black.red.green.yellow.blue.magenta.cyan.white(text); text = clc.bgBlack.bgRed.bgGreen.bgYellow.bgBlack.bgMagenta.bgCyan.bgWhite(text); text = clc.blackBright.redBright.greenBright.yellowBright.blueBright.magentaBright.cyanBright.whiteBright(text); text = clc.bgBlackBright.bgRedBright.bgGreenBright.bgYellowBright.bgBlueBright.bgMagentaBright.bgCyanBright.bgWhiteBright(text); -text = clc.xterm(color).bgXterm(color)(text); -text = clc.bold.red.bgGreen.yellowBright.bgBlueBright.xterm(color)(text, text, text); -text = clc.move(x, y); -text = clc.moveTo(x, y); -text = clc.bol(); -text = clc.bol(n); -text = clc.bol(n, true); -text = clc.up(n); -text = clc.down(n); -text = clc.left(n); -text = clc.right(n); +const error: clc.Format = clc.red.bold; +const warn: clc.Format = clc.yellow; +const notice: clc.Format = clc.blue; +text = error("Error!"); +text = warn("Warning"); +text = notice("Notice"); + +format = clc.xterm(202).bgXterm(236); +text = format("Orange text on dark gray background"); + text = clc.beep; text = clc.reset; +text = clc.erase.screen; +text = clc.erase.screenLeft; +text = clc.erase.screenRight; +text = clc.erase.line; +text = clc.erase.lineRight; +text = clc.erase.lineLeft; -var width: number = clc.width; -var height: number = clc.height; +text = clc.move(-2, -2); // Move cursors two columns and two rows back +text = clc.move.to(0, 0); // Move cursor to first row and first column in terminal window +text = clc.move.up(2); +text = clc.move.down(2); +text = clc.move.right(2); +text = clc.move.left(2); +text = clc.move.lines(2); +text = clc.move.top; +text = clc.move.bottom; +text = clc.move.lineBegin; +text = clc.move.lineEnd; + +var width: number = clc.windowSize.width; +var height: number = clc.windowSize.height; var support: boolean = clc.xtermSupported; -// Test cli-color/trim -text = ansiTrim(clc.red(text)); +text = clc.bold("foo") + "bar" + clc.red("elo"); +text = clc.slice(text, 1, 7); // Same as: clc.bold('oo') + 'bar' + clc.red('e') + +text = ansiStrip(text); + +text = clc.bold("foo") + "bar" + clc.red("elo"); +var len: number = clc.getStrippedLength(text); // 9 + +text = ".........\n" + ". Hello .\n" + ".........\n"; +var style = { ".": clc.yellowBright("X") }; +text = clc.art(text, style); + +text = + clc.columns([ + [clc.bold("First Name"), clc.bold("Last Name"), clc.bold("Age")], ["John", "Doe", 34], + ["Martha", "Smith", 20], ["Jan", "Kowalski", 30] + ]); + +text = + clc.columns([ + [clc.bold("First Name"), clc.bold("Last Name"), clc.bold("Age")], ["John", "Doe", 34], + ["Martha", "Smith", 20], { [0]: "Jan", [1]: "Kowalski", [2]: 30, length: 3 } + ]); + +text = + clc.columns([ + [clc.bold("First Name"), clc.bold("Last Name"), clc.bold("Age")], ["John", "Doe", 34], + ["Martha", "Smith", 20], ["Jan", "Kowalski", 30] + ], { sep: '|' }); +text = + clc.columns([ + [clc.bold("First Name"), clc.bold("Last Name"), clc.bold("Age")], ["John", "Doe", 34], + ["Martha", "Smith", 20], ["Jan", "Kowalski", 30] + ], { columns: [{ align: 'right' }, null, { align: 'left' }] }); // Test cli-color/throbber var throbber: setupThrobber.Throbber; - throbber = setupThrobber(process.stdout.write.bind(process.stdout), period); throbber = setupThrobber(process.stdout.write.bind(process.stdout), period, clc.red); throbber.start(); throbber.stop(); throbber.restart(); + +var iterator = new clc.throbber.Iterator(); +text = iterator.next(); +text = iterator.reset(); diff --git a/types/cli-color/columns.d.ts b/types/cli-color/columns.d.ts new file mode 100644 index 0000000000..affe2d72f9 --- /dev/null +++ b/types/cli-color/columns.d.ts @@ -0,0 +1,29 @@ +declare namespace columns { + export interface ColumnOptions { + /** + * align: Possible options: `'left'`, `'right'` (defaults to `'left'`) + */ + align?: 'left' | 'right'; + } + + export interface ColumnsOptions { + /** + * Custom colums separator (defaults to `|`) + */ + sep?: string; + /** + * columns: Per column customizations, as e.g. `[{ align: 'right' }, null, { align: 'left' }]` + */ + columns?: Array; + } + + export type Row = Iterable | ArrayLike; + export type Data = Iterable | ArrayLike; +} + +/** + * Outputs aligned table of columns. + */ +declare function columns(data: ReadonlyArray>, options?: columns.ColumnsOptions): string; +declare function columns(data: columns.Data, options?: columns.ColumnsOptions): string; +export = columns; diff --git a/types/cli-color/erase.d.ts b/types/cli-color/erase.d.ts new file mode 100644 index 0000000000..b968a7a42f --- /dev/null +++ b/types/cli-color/erase.d.ts @@ -0,0 +1,29 @@ +interface Erase { + /** + * Entire screen + */ + readonly screen: string; + /** + * Left portion of a screen + */ + readonly screenLeft: string; + /** + * Right portion of a screen + */ + readonly screenRight: string; + /** + * Current line + */ + readonly line: string; + /** + * Right portion of current line + */ + readonly lineLeft: string; + /** + * Left portion of current line + */ + readonly lineRight: string; +} + +declare const erase: Erase; +export = erase; diff --git a/types/cli-color/get-stripped-length.d.ts b/types/cli-color/get-stripped-length.d.ts new file mode 100644 index 0000000000..6783ffeabb --- /dev/null +++ b/types/cli-color/get-stripped-length.d.ts @@ -0,0 +1,5 @@ +/** + * Get actual length of ANSI-formatted string + */ +declare function getStrippedLength(str: string): number; +export = getStrippedLength; diff --git a/types/cli-color/index.d.ts b/types/cli-color/index.d.ts index dc49e8133a..fdfb491c72 100644 --- a/types/cli-color/index.d.ts +++ b/types/cli-color/index.d.ts @@ -1,73 +1,41 @@ -// Type definitions for cli-color 0.3.2 +// Type definitions for cli-color 2.0 // Project: https://github.com/medikoo/cli-color // Definitions by: Joel Spadin +// OpportunityLiu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare namespace m { - export interface Format { - (...text: any[]): string; - bold: Format; - italic: Format; - underline: Format; - blink: Format; - inverse: Format; - strike: Format; +import art = require('./art'); +import bare = require('./bare'); +import beep = require('./beep'); +import columns = require('./columns'); +import erase = require('./erase'); +import move = require('./move'); +import getStrippedLength = require('./get-stripped-length'); +import slice = require('./slice'); +import strip = require('./strip'); +import throbber = require('./throbber'); +import reset = require('./reset'); +import windowSize = require('./window-size'); - black: Format; - red: Format; - green: Format; - yellow: Format; - blue: Format; - magenta: Format; - cyan: Format; - white: Format; +declare namespace clc { + export type Format = bare.Format; + export type ColumnOptions = columns.ColumnOptions; + export type ColumnsOptions = columns.ColumnsOptions; + export interface Color extends Format { - bgBlack: Format; - bgRed: Format; - bgGreen: Format; - bgYellow: Format; - bgBlue: Format; - bgMagenta: Format; - bgCyan: Format; - bgWhite: Format; - - blackBright: Format; - redBright: Format; - greenBright: Format; - yellowBright: Format; - blueBright: Format; - magentaBright: Format; - cyanBright: Format; - whiteBright: Format; - - bgBlackBright: Format; - bgRedBright: Format; - bgGreenBright: Format; - bgYellowBright: Format; - bgBlueBright: Format; - bgMagentaBright: Format; - bgCyanBright: Format; - bgWhiteBright: Format; - - xterm(color: number): Format; - bgXterm(color: number): Format; - - move(x: number, y: number): string; - moveTo(x: number, y: number): string; - bol(n?: number, erase?: boolean): string; - up(n: number): string; - down(n: number): string; - left(n: number): string; - right(n: number): string; - - beep: string; - reset: string; - - width: number; - height: number; - xtermSupported: boolean; + readonly windowSize: typeof windowSize; + readonly erase: typeof erase; + readonly move: typeof move; + readonly beep: typeof beep; + readonly columns: typeof columns; + readonly strip: typeof strip; + readonly getStrippedLength: typeof getStrippedLength; + readonly slice: typeof slice; + readonly throbber: typeof throbber; + readonly reset: typeof reset; + readonly art: typeof art; } } -declare var m: m.Format; -export = m; +declare const clc: clc.Color; +export = clc; diff --git a/types/cli-color/move.d.ts b/types/cli-color/move.d.ts new file mode 100644 index 0000000000..3718b37cc2 --- /dev/null +++ b/types/cli-color/move.d.ts @@ -0,0 +1,56 @@ +type MoveFunction1 = (n?: number) => string; +type MoveFunction2 = (x?: number, y?: number) => string; + +/** + * Move around functions + */ +interface Move { + + /** + * Move cursor x columns and y rows away. Values can be positive or negative + */ + (x?: number, y?: number): string; + /** + * Move cursor up n rows + */ + readonly up: MoveFunction1; + /** + * Move cursor down n rows + */ + readonly down: MoveFunction1; + /** + * Move cursor right n columns + */ + readonly right: MoveFunction1; + /** + * Move cursor left n columns + */ + readonly left: MoveFunction1; + /** + * Absolute move. Sets cursor position at x column and y row + */ + readonly to: MoveFunction2; + /** + * Move cursor n lines forward if n is positive, otherwise n lines backward, and place it at line beginning + */ + readonly lines: MoveFunction1; + /** + * Move cursor to top of a screen + */ + readonly top: string; + /** + * Move cursor to bottom of a screen + */ + readonly bottom: string; + /** + * Move cursor to begin of a line + */ + readonly lineBegin: string; + /** + * Move cursor to end of a line + */ + readonly lineEnd: string; +} + +declare const move: Move; +export = move; diff --git a/types/cli-color/reset.d.ts b/types/cli-color/reset.d.ts new file mode 100644 index 0000000000..fd19d85a34 --- /dev/null +++ b/types/cli-color/reset.d.ts @@ -0,0 +1,5 @@ +/** + * Terminal can be cleared with `clc.reset` + */ +declare const reset: string; +export = reset; diff --git a/types/cli-color/slice.d.ts b/types/cli-color/slice.d.ts new file mode 100644 index 0000000000..dcc71531f7 --- /dev/null +++ b/types/cli-color/slice.d.ts @@ -0,0 +1,5 @@ +/** + * Slice provided string with preservation of eventual ANSI formatting + */ +declare function slice(str: string, begin?: number, end?: number): string; +export = slice; diff --git a/types/cli-color/strip.d.ts b/types/cli-color/strip.d.ts new file mode 100644 index 0000000000..0c6f3b13ce --- /dev/null +++ b/types/cli-color/strip.d.ts @@ -0,0 +1,5 @@ +/** + * Strip ANSI formatting from string + */ +declare function strip(str: string): string; +export = strip; diff --git a/types/cli-color/throbber.d.ts b/types/cli-color/throbber.d.ts index 8d1d34d23d..04fac51619 100644 --- a/types/cli-color/throbber.d.ts +++ b/types/cli-color/throbber.d.ts @@ -1,12 +1,19 @@ -import clc = require('cli-color'); - declare namespace setupThrobber { export interface Throbber { start(): void; stop(): void; restart(): void; } -} -declare function setupThrobber(write: (str: string) => any, period: number, format?: clc.Format): setupThrobber.Throbber; + export class Iterator { + readonly index: number; + readonly running: boolean; + next(): string; + reset(): string; + } +} +/** + * Writes throbber string to *write* function at given *interval*. Optionally throbber output can be formatted with given *format* function + */ +declare function setupThrobber(write: (str: string) => void, interval: number, format?: (throbber: string) => string): setupThrobber.Throbber; export = setupThrobber; diff --git a/types/cli-color/trim.d.ts b/types/cli-color/trim.d.ts deleted file mode 100644 index a0302d5373..0000000000 --- a/types/cli-color/trim.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare function ansiTrim(str: string): string; -export = ansiTrim; diff --git a/types/cli-color/window-size.d.ts b/types/cli-color/window-size.d.ts new file mode 100644 index 0000000000..573e55520b --- /dev/null +++ b/types/cli-color/window-size.d.ts @@ -0,0 +1,13 @@ +interface WindowSize { + /** + * Returns terminal width + */ + readonly width: number, + /** + * Returns terminal height + */ + readonly height: number +} + +declare const windowSize: WindowSize; +export = windowSize;