Add prompt sync (#12412)

* Add definition for prompt-sync

* Add definition for prompt-sync-history
This commit is contained in:
TANAKA Koichi
2016-11-03 00:21:54 +09:00
committed by Masahiro Wakame
parent 3231685087
commit baadc0b130
4 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
/// <reference path="./prompt-sync-history" />
/// <reference path="../prompt-sync/prompt-sync" />
'use strict';
import * as promptHistory from 'prompt-sync-history';
import * as PromptSync from 'prompt-sync';
let history:PromptSync.History;
history = promptHistory();
history = promptHistory('/path/to/file');
history = promptHistory(null, 1000);

View File

@@ -0,0 +1,15 @@
// Type definitions for prompt-sync-history 1.0.1
// Project: https://github.com/davidmarkclements/prompt-sync-history
// Definitions by: TANAKA Koichi <https://github.com/MugeSo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../prompt-sync/prompt-sync" />
declare module 'prompt-sync-history' {
import {History} from 'prompt-sync';
namespace history {}
function history(file?:string, max?:number): History;
export = history;
}

View File

@@ -0,0 +1,55 @@
/// <reference path="./prompt-sync" />
'use strict';
import * as promptSync from 'prompt-sync';
declare const history: promptSync.History;
let prompt: promptSync.Prompt;
// without config
prompt = promptSync();
// with config
prompt = promptSync({
history: history,
sigint: false,
autocomplete: (input:string) => [input]
});
// with empty config
prompt = promptSync({});
let name:string = prompt('Enter name: ');
let nickname:string = prompt({ask: 'Enter nickname: ', value: 'N/A'});
let gender:string = prompt('Enter gender: ', { autocomplete: complete(['male', 'female']) });
let age:string = prompt('Enter age: ', '18', { echo: '*' });
let password:string = prompt.hide('Enter password: ');
let anotherPassword:string = prompt('Enter another password: ', { echo: '', value: '*password*'});
function complete(commands: string[]) {
return function (str: string) {
const ret:string[] = [];
for (let i=0; i< commands.length; i++) {
if (commands[i].indexOf(str) == 0)
ret.push(commands[i]);
}
return ret;
};
}
// History interface
let bool: boolean;
bool = history.atStart();
bool = history.atPenultimate();
bool = history.pastEnd();
bool = history.atEnd();
let str: string;
str = history.prev();
str = history.next();
history.reset();
history.push('aaa');
history.save();

75
prompt-sync/prompt-sync.d.ts vendored Normal file
View File

@@ -0,0 +1,75 @@
// Type definitions for prompt-sync 4.1.4
// Project: https://github.com/0x00A/prompt-sync
// Definitions by: TANAKA Koichi <https://github.com/MugeSo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module 'prompt-sync' {
namespace PromptSync {
export interface Prompt {
(opts: Option): string;
(ask: string): string;
(ask: string, opts: Option): string;
(ask: string, value: string): string;
/**
* prompt -- sync function for reading user input from stdin
* @param {String} ask opening question/statement to prompt for
* @param {String} value initial value for the prompt
* @param {Object} opts {
* echo: set to a character to be echoed, default is '*'. Use '' for no echo
* value: {String} initial value for the prompt
* ask: {String} opening question/statement to prompt for, does not override ask param
* autocomplete: {StringArray} function({String})
* }
*
* @returns {string} Returns the string input or (if sigint === false)
* null if user terminates with a ^C
*/
(ask: string, value: string, opts: Option): string;
hide(ask: string): string;
}
export interface Option {
ask?: string;
echo?: string;
autocomplete?: AutoCompleteFunction;
value?: string;
}
export interface Config {
sigint?: boolean;
autocomplete?: AutoCompleteFunction;
history?: History;
}
export interface History {
atStart(): boolean;
atPenultimate(): boolean;
pastEnd(): boolean;
atEnd(): boolean;
prev(): string;
next(): string;
reset(): void;
push(str: string): void;
save(): void;
}
export interface AutoCompleteFunction {
(input: string): string[];
}
}
/**
* create -- sync function for reading user input from stdin
* @param {Object} config {
* sigint: {Boolean} exit on ^C
* autocomplete: {StringArray} function({String})
* history: {String} a history control object (see `prompt-sync-history`)
* }
* @returns {Function} prompt function
*/
function PromptSync(config?: PromptSync.Config): PromptSync.Prompt;
export = PromptSync;
}