Merge pull request #13698 from jonestristand/readline-sync

Readline sync
This commit is contained in:
Daniel Rosenwasser
2017-01-03 16:07:24 -05:00
committed by GitHub
4 changed files with 217 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
// Type definitions for readline-sync 1.4
// Project: https://github.com/anseki/readline-sync
// Definitions by: Tristan Jones <https://github.com/jonestristand>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type OptionType = string | number | RegExp | ((input: string) => boolean);
interface BasicOptions {
prompt?: any;
hideEchoBack?: boolean;
mask?: string;
limit?: OptionType | OptionType[];
limitMessage?: string;
defaultInput?: string;
trueValue?: OptionType | OptionType[];
falseValue?: OptionType | OptionType[];
caseSensitive?: boolean;
keepWhitespace?: boolean;
encoding?: string;
bufferSize?: number;
print?: (display: string, encoding: string) => void;
history?: boolean;
cd?: boolean;
charlist?: string;
min?: any;
max?: any;
confirmMessage?: any;
unmatchMessage?: any;
exists?: any;
isFile?: boolean;
isDirectory?: boolean;
validate?: (path: string) => boolean | string;
create?: boolean;
guide?: boolean;
}
// Basic Functions
export function question(query?: any, options?: BasicOptions): string;
export function prompt(options?: BasicOptions): string;
export function keyIn(query?: any, options?: BasicOptions): string;
export function setDefaultOptions(options?: BasicOptions): BasicOptions;
// Utility Functions
export function questionEMail(query?: any, options?: BasicOptions): string;
export function questionNewPassword(query?: any, options?: BasicOptions): string;
export function questionInt(query?: any, options?: BasicOptions): number;
export function questionFloat(query?: any, options?: BasicOptions): number;
export function questionPath(query?: any, options?: BasicOptions): string;
export function promptCL(commandHandler?: { [id: string]: (...args: string[]) => void } | ((command: string, ...args: string[]) => void), options?: BasicOptions): string[];
export function promptLoop(inputHandler: (value: string) => boolean, options?: BasicOptions): void;
export function promptCLLoop(commandHandler?: { [id: string]: (...args: string[]) => boolean | void } | ((command: string, ...args: string[]) => boolean | void), options?: BasicOptions): void;
export function promptSimShell(options?: BasicOptions): string;
export function keyInYN(query?: any, options?: BasicOptions): boolean | string;
export function keyInYNStrict(query?: any, options?: BasicOptions): boolean;
export function keyInPause(query?: any, options?: BasicOptions): void;
export function keyInSelect(items: string[], query?: any, options?: BasicOptions): number;
export function getRawInput(): string;
// Deprecated
/**
* @deprecated Use the bufferSize option instead: readlineSync.setDefaultOptions({bufferSize: value});
*/
export function setBufferSize(value: number): void;
/**
* @deprecated Use the encoding option instead: readlineSync.setDefaultOptions({encoding: value});
*/
export function setEncoding(value: string): void;
/**
* @deprecated Use the mask option instead: readlineSync.setDefaultOptions({mask: value});
*/
export function setMask(value: string): void;
/**
* @deprecated Use the print option instead: readlineSync.setDefaultOptions({print: value});
*/
export function setPrint(value: (display: string, encoding: string) => void): void;
/**
* @deprecated Use the prompt option instead: readlineSync.setDefaultOptions({prompt: value});
*/
export function setPrompt(value: any): void;
+109
View File
@@ -0,0 +1,109 @@
import readlineSync = require('readline-sync');
let result:string = readlineSync.question('Which program starts do you want? ', {
defaultInput: 'firefox'
});
let result2:string = readlineSync.prompt({prompt: '$$'});
let result3:string = readlineSync.keyIn('Press a key', { limit: '$<1-5>' });
let result4:{} = readlineSync.setDefaultOptions({
prompt: '$$',
hideEchoBack: true,
mask: '*',
limit: [ /^a|b$/, /[a-zA-Z]+/],
limitMessage: 'Limit reached',
defaultInput: 'English',
trueValue: 2,
falseValue: (value:string) => { return true; },
caseSensitive: true,
keepWhitespace: true,
encoding: 'utf-8',
bufferSize: 12,
print: (display:string, encoding:string) => { console.log(display)},
history: false,
cd: true,
charlist: 'abc',
min: 0,
max: 10,
confirmMessage: 'Yes',
unmatchMessage: new Date(),
exists: false,
isFile: true,
isDirectory: false,
validate: (path:string) => { return (path === '/usr/local/bin'); },
create: true,
guide: false,
});
let result5:string = readlineSync.questionEMail('Enter email');
let result6:string = readlineSync.questionNewPassword('PASSWORD: ', {charlist: '$<a-z>#$@%'});
let result7:number = readlineSync.questionInt('Enter an integer', { limitMessage: 'Enter a valid integer' });
let result8:number = readlineSync.questionFloat('Enter a float', { limitMessage: 'Enter a valid float' });
let result9:string = readlineSync.questionPath('Save to: ', {
isDirectory: true,
exists: null,
create: true
});
readlineSync.promptCL((command:string, arg1:string, arg2:string) => {
if (command === 'add') {
console.log(arg1 + ' is added.');
} else if (command === 'copy') {
console.log(arg1 + ' is copied to ' + arg2 + '.');
}
});
readlineSync.promptCL({
add: (element:string) => { // It's called by also "ADD", "Add", "aDd", etc..
console.log(element + ' is added.');
},
copy: (from:string, to:string) => {
console.log(from + ' is copied to ' + to + '.');
}
});
readlineSync.promptLoop((input:string) => {
console.log('-- You said "' + input + '"');
return input === 'bye';
});
readlineSync.promptCLLoop({
add: (element:string) => {
console.log(element + ' is added.');
},
copy: (from:string, to:string) => {
console.log(from + ' is copied to ' + to + '.');
},
bye: () => { return true; }
});
let result10:string = readlineSync.promptSimShell();
let result11:(boolean | string) = readlineSync.keyInYN('Do you want to install this?');
let result12:boolean = readlineSync.keyInYNStrict('Do you want to install this?', { guide: true });
readlineSync.keyInPause({ guide:true });
let frameworks = ['Express', 'hapi', 'flatiron', 'MEAN.JS', 'locomotive'];
let result13:number = readlineSync.keyInSelect(frameworks, 'Which framework?');
let result14:string = readlineSync.getRawInput();
readlineSync.setPrint((display:string, encoding:string) => { console.log(display) });
readlineSync.setPrompt(new Date());
readlineSync.setEncoding('utf-16');
readlineSync.setMask('%');
readlineSync.setBufferSize(128);
+20
View File
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"readline-sync-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "../tslint.json" }