From c1201dbdcf1684648584009efbc3c9d907ba62c8 Mon Sep 17 00:00:00 2001 From: Farzad Majidfayyaz Date: Mon, 18 Mar 2019 22:49:03 -0400 Subject: [PATCH] Add types for 'clui' package --- types/clui/clui-tests.ts | 96 ++++++++++++++++++++++++ types/clui/index.d.ts | 157 +++++++++++++++++++++++++++++++++++++++ types/clui/tsconfig.json | 24 ++++++ types/clui/tslint.json | 1 + 4 files changed, 278 insertions(+) create mode 100644 types/clui/clui-tests.ts create mode 100644 types/clui/index.d.ts create mode 100644 types/clui/tsconfig.json create mode 100644 types/clui/tslint.json diff --git a/types/clui/clui-tests.ts b/types/clui/clui-tests.ts new file mode 100644 index 0000000000..1968b10cba --- /dev/null +++ b/types/clui/clui-tests.ts @@ -0,0 +1,96 @@ +import { Gauge, Line, LineBuffer, Progress, Sparkline, Spinner } from 'clui'; +import * as clc from 'cli-color'; + +// LineBuffer +const outputBuffer = new LineBuffer({ + x: 0, + y: 0, + width: 'console', + height: 'console' +}); + +new Line(outputBuffer) + .column('Title Placehole', 20, [clc.green]) + .fill() + .store(); + +new Line(outputBuffer) + .fill() + .store(); + +new Line(outputBuffer) + .column('Suscipit', 20, [clc.cyan]) + .column('Voluptatem', 20, [clc.cyan]) + .column('Nesciunt', 20, [clc.cyan]) + .column('Laudantium', 11, [clc.cyan]) + .fill() + .store(); + +for (let l = 0; l < 20; l++) { + new Line(outputBuffer) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 11) + .fill() + .store(); +} + +outputBuffer.output(); + +// Line +new Line() + .padding(2) + .column('Column One', 20, [clc.cyan]) + .column('Column Two', 20, [clc.cyan]) + .column('Column Three', 20, [clc.cyan]) + .column('Column Four', 20, [clc.cyan]) + .fill() + .output(); + +new Line() + .padding(2) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 20) + .column((Math.random() * 100).toFixed(3), 20) + .fill() + .output(); + +// Gauge +const total = 33660133376; +const free = 17763860480; +const used = total - free; +const human = Math.ceil(used / 1000000) + ' MB'; + +console.log(Gauge(used, total, 20, total * 0.8, human)); + +// Sparkline +const reqsPerSec = [10, 12, 3, 7, 12, 9, 23, 10, 9, 19, 16, 18, 12, 12]; + +console.log(Sparkline(reqsPerSec, 'reqs/sec')); + +// Progress +const thisProgressBar = new Progress(20); +console.log(thisProgressBar.update(10, 30)); + +// or + +const thisPercentBar = new Progress(20); +console.log(thisPercentBar.update(0.4)); + +// Spinner +const countdown = new Spinner('Exiting in 10 seconds... ', ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷']); + +countdown.start(); + +let n = 10; +const interval = setInterval(() => { + n--; + countdown.message(`Exiting in ${n} seconds...`); + if (n === 0) { + console.log('\n'); + countdown.stop(); + clearInterval(interval); + } +}, 1000); diff --git a/types/clui/index.d.ts b/types/clui/index.d.ts new file mode 100644 index 0000000000..339ac031d9 --- /dev/null +++ b/types/clui/index.d.ts @@ -0,0 +1,157 @@ +// Type definitions for clui 0.3 +// Project: https://github.com/nathanpeck/clui#readme +// Definitions by: Farzad Majidfayyaz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as clc from 'cli-color'; + +export interface LineBufferOptions { + x?: number; + y?: number; + width?: number | 'console'; + height?: number | 'console'; + scroll?: number; +} + +export class LineBuffer { + /** + * Creates an object for buffering a group of text lines and then outputting them + * @param options Values to build the buffer + */ + constructor(options: LineBufferOptions); + + /** + * Return the height of the `LineBuffer`, when specified as `console` + */ + height(): number; + + /** + * Return the width of the `LineBuffer`, when specified as `console` + */ + width(): number; + + /** + * Put a `Line` object into the `LineBuffer` + * @param line The line object to put into the buffer + */ + addLine(line: Line): void; + + /** + * If you don't have enough lines in the buffer, this will fill the reset of + * the lines with empty spaces + */ + fill(): void; + + /** + * Draw the `LineBuffer` to screen + */ + output(): void; +} + +/** + * This chainable object can be used to generate a line of text with columns, padding, and fill + */ +export class Line { + /** + * Create a new instance of Line object + * @param buffer Object to be used as buffer + */ + constructor(buffer?: LineBuffer); + + /** + * Output `width` characters of blank space + * @param width Number of characters to print + */ + padding(width: number): Line; + + /** + * Output text within a column of the specified width + * @param text Text to print + * @param width Width of the column + * @param styles List of `cli-color` styles to apply + */ + column(text: string, width: number, styles?: clc.Format[]): Line; + + /** + * At the end of a line, fill the rest of the columns to the right edge + */ + fill(): Line; + + /** + * Print the generated line of text to the console + */ + output(): Line; + + /** + * Return the contents of this line as a string + */ + contents(): string; + + /** + * Store this line into the buffer + */ + store(): void; +} + +/** + * Creates a basic horizontal gauge to the screen + * @param value The current value of the metric being displayed by this gauge + * @param maxValue The highest possible value of the metric being displayed + * @param guageWidth How many columns widt to draw the gauge + * @param dangerZone The point after which the value will be drawn in red because it's too high + * @param suffix A value to output after the gauge itself + */ +export function Gauge( + value: number, + maxValue: number, + guageWidth: number, + dangerZone: number, + suffix: string, +): string; + +/** + * A simple command line sparkline that draws a series of values, and highlights the peak for the period + * @param values An array of values to go into the sparkline + * @param suffix A suffix to use when drawing the current and max values at the end of the sparkline + */ +export function Sparkline(values: number[], suffix: string): string; + +export class Progress { + /** + * Creates a progress bar + * @param length The desired length of the progress bar in characters + */ + constructor(length: number); + + /** + * Returns the progress bar min/max context to write to stdout + * @param currentValueOrPercent Current value (or percent) of the progress bar + * @param maxValue Maximum value of the progress bar + */ + update(currentValueOrPercent: number, maxValue?: number): string; +} + +export class Spinner { + /** + * Creates a new spinner + * @param statusText The default text to display while the spinner is spinning + * @param style Array of graphical characters used to draw the spinner + */ + constructor(statusText: string, style?: string[]); + + /** + * Show the spinner on the screen + */ + start(): void; + + /** + * Update the status message that follows the spinner + * @param statusMessage Message to be displayed + */ + message(statusMessage: string): void; + + /** + * Erase the spinner from the screen + */ + stop(): void; +} diff --git a/types/clui/tsconfig.json b/types/clui/tsconfig.json new file mode 100644 index 0000000000..05235b1de8 --- /dev/null +++ b/types/clui/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "dom", + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "clui-tests.ts" + ] +} diff --git a/types/clui/tslint.json b/types/clui/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/clui/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }