Merge pull request #33991 from farzadmf/master

Add types for 'clui' package
This commit is contained in:
Sheetal Nandi
2019-03-19 11:30:26 -07:00
committed by GitHub
4 changed files with 278 additions and 0 deletions

96
types/clui/clui-tests.ts Normal file
View File

@@ -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);

157
types/clui/index.d.ts vendored Normal file
View File

@@ -0,0 +1,157 @@
// Type definitions for clui 0.3
// Project: https://github.com/nathanpeck/clui#readme
// Definitions by: Farzad Majidfayyaz <https://github.com/farzadmf>
// 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;
}

24
types/clui/tsconfig.json Normal file
View File

@@ -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"
]
}

1
types/clui/tslint.json Normal file
View File

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