diff --git a/prompt-sync-history/prompt-sync-history-tests.ts b/prompt-sync-history/prompt-sync-history-tests.ts new file mode 100644 index 0000000000..2132f8166f --- /dev/null +++ b/prompt-sync-history/prompt-sync-history-tests.ts @@ -0,0 +1,12 @@ +/// +/// +'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); diff --git a/prompt-sync-history/prompt-sync-history.d.ts b/prompt-sync-history/prompt-sync-history.d.ts new file mode 100644 index 0000000000..6b343fbb51 --- /dev/null +++ b/prompt-sync-history/prompt-sync-history.d.ts @@ -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 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'prompt-sync-history' { + import {History} from 'prompt-sync'; + + namespace history {} + function history(file?:string, max?:number): History; + + export = history; +} diff --git a/prompt-sync/prompt-sync-tests.ts b/prompt-sync/prompt-sync-tests.ts new file mode 100644 index 0000000000..fbe2e120e7 --- /dev/null +++ b/prompt-sync/prompt-sync-tests.ts @@ -0,0 +1,55 @@ +/// +'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(); diff --git a/prompt-sync/prompt-sync.d.ts b/prompt-sync/prompt-sync.d.ts new file mode 100644 index 0000000000..3b997c93ca --- /dev/null +++ b/prompt-sync/prompt-sync.d.ts @@ -0,0 +1,75 @@ +// Type definitions for prompt-sync 4.1.4 +// Project: https://github.com/0x00A/prompt-sync +// Definitions by: TANAKA Koichi +// 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; +}