UNPKG

@cz-git/inquirer

Version:

provide cz-git inquirer plugins, include search-list, search-checkbox, complete-input

362 lines (347 loc) 10.5 kB
import { Interface } from 'node:readline'; import Base from 'inquirer/lib/prompts/base'; import { Answers, Question } from 'inquirer'; import Separator from 'inquirer/lib/objects/separator'; interface ChoiceType<T> { name: string; type: string | T; short: string; value: string | boolean; line: string; disabled: boolean; checked: boolean; } interface ChoicesType { getChoice: (pointer: number) => ChoiceType<any>; /** * origin choices */ choices: ChoiceType<Separator['type']>[]; /** * filter Separator choices */ realChoices: ChoiceType<string>[]; } interface BaseOptionType { pageSize: number; default?: any; } interface isFinal { isFinal: boolean; } interface SearchPromptQuestionOptions<T extends Answers = Answers> extends Question<T> { separator: string; /** * support rgb color code. e.g: `38;5;042` * * @default cyan * @tip the rgb color see to check your number: https://github.com/sindresorhus/xterm-colors */ themeColorCode?: string; /** * default checked item's value array on initial */ initialCheckedValue?: string[] | string; /** * Function to determine what options to display to user. * Called with previous answers object and the current user input each time the user types, it must return a promise. */ source: (answersSoFar: T, input: string | undefined) => Promise<any[]>; /** * The number of elements to show on each page. */ pageSize?: number | undefined; /** * Setting it to true turns the input into a normal text input. * * @default false */ isInitDefault?: boolean | undefined; } interface CompletePromptQuestionOptions<T extends Answers = Answers> extends Question<T> { completeValue?: string; transformer?: (input: string, answers: Answers, { isFinal }: isFinal) => string; } interface FilterArrayItemType { value: string; name: string; emoji?: string; index?: number; score?: number; } /** * @description check the term is support unicode */ declare function isUnicodeSupport(): boolean; declare const figures: { pointer: string; radioOn: string; radioOff: string; squareSmallFilled: string; }; /** * Terminal style output colorizen * Inspired by `picocolors` (https://www.npmjs.com/package/picocolors) * @author Zhengqbbb <zhengqbbb@gmail.com> * @license MIT */ /** * Check current is support color command text * * @param colorSupport can force output not colorizen * @param fd Channel. Provide options to allow users to customize the judgment. * e.g, logs and TUI are 2 stderr. In this case, only when the user operates on 2 does the color output need to be disabled. * COMMAND 2 > runtime.log. All logs need to remove colorizen code */ declare function isColorizenSupport(colorSupport?: boolean, fd?: number): boolean; /** * support control isColorizen as param * styleFn generator */ declare function createStyle(enabled?: boolean): { isColorSupported: boolean; reset: (input: string) => string; bold: (input: string) => string; dim: (input: string) => string; italic: (input: string) => string; underline: (input: string) => string; inverse: (input: string) => string; hidden: (input: string) => string; strikethrough: (input: string) => string; black: (input: string) => string; red: (input: string) => string; green: (input: string) => string; yellow: (input: string) => string; blue: (input: string) => string; magenta: (input: string) => string; cyan: (input: string) => string; white: (input: string) => string; gray: (input: string) => string; bgBlack: (input: string) => string; bgRed: (input: string) => string; bgGreen: (input: string) => string; bgYellow: (input: string) => string; bgBlue: (input: string) => string; bgMagenta: (input: string) => string; bgCyan: (input: string) => string; bgWhite: (input: string) => string; rgb: (rgbColor?: string) => (input: string) => string; }; /** * @description commandline style output colorizen. * Automatically determine whether output coloring is required. * * @tip the rgb color see to check your number: https://github.com/sindresorhus/xterm-colors */ declare const style: { isColorSupported: boolean; reset: (input: string) => string; bold: (input: string) => string; dim: (input: string) => string; italic: (input: string) => string; underline: (input: string) => string; inverse: (input: string) => string; hidden: (input: string) => string; strikethrough: (input: string) => string; black: (input: string) => string; red: (input: string) => string; green: (input: string) => string; yellow: (input: string) => string; blue: (input: string) => string; magenta: (input: string) => string; cyan: (input: string) => string; white: (input: string) => string; gray: (input: string) => string; bgBlack: (input: string) => string; bgRed: (input: string) => string; bgGreen: (input: string) => string; bgYellow: (input: string) => string; bgBlue: (input: string) => string; bgMagenta: (input: string) => string; bgCyan: (input: string) => string; bgWhite: (input: string) => string; rgb: (rgbColor?: string) => (input: string) => string; }; /** * @description Provide list and checkBox fuzzy search * @author Zhengqbbb <zhengqbbb@gmail.com> * @license MIT */ /** * input string match target string return match score * * @param {string} input input string * @param {string} target target string * @param {boolean} caseSensitive isCaseSensitive, default: false * @return {number | null} match score. if not match return null */ declare function fuzzyMatch(input: string, target: string, caseSensitive?: boolean): number | null; /** * Array fuzzy filter * * @param {string} input input string * @param {Array<FilterArrayItemType | unknown>} arr target Array * @return {Array<FilterArrayItemType>} filtered array */ declare function fuzzyFilter(input: string, arr: Array<FilterArrayItemType>, targetKey?: 'name' | 'value'): Array<FilterArrayItemType>; /** * @description ANSI escape codes for manipulating the terminal */ declare const ansiEscapes: { /** move cursor to left */ cursorLeft: string; /** move cursor forward count length */ cursorForward: (count?: number) => string; /** move cursor backward count length */ cursorBackward: (count?: number) => string; }; /** * @description inquirer plugin - Search List * @author Zhengqbbb <zhengqbbb@gmail.com> * @license MIT * Powered by `inquirer-autocomplete-prompt` */ declare class SearchList extends Base { private renderChoices; private pointer; private choicesLen; private firstRender; private searching; private haveSearched; private themeColorCode?; private initialValue; private lastSearchInput?; private paginator; private answer?; private done; constructor(questions: Question, readline: Interface, answers: Answers); /** * Start the Inquiry session * * @param {Function} cb Callback when prompt is done */ _run(cb: any): this; /** * render screen */ render(error?: string): void; /** * resolve source to get renderChoices */ search(input?: string): Promise<any>; /** * resovle line Events <Enter> */ onSubmit(): void; onSubmitAfterValidation(line: string): void; /** * keypress handler */ onKeypress(e: { key: { name?: string; ctrl?: boolean; }; value: string; }): void; ensureSelectedInRange(): void; } /** * @description inquirer plugin - Search Checkbox * @author Zhengqbbb <zhengqbbb@gmail.com> * @license MIT */ declare class SearchCheckbox extends Base { private renderChoices; private originChoices; private pointer; private choicesLen; private selection; private firstRender; private searching; private haveSearched; private themeColorCode?; private initialCheckedValue?; private initialValue; private lastSearchInput?; private paginator; private separator; private answer?; private done; constructor(questions: Question, readline: Interface, answers: Answers); /** * Start the Inquiry session * * @param {Function} cb Callback when prompt is done */ _run(cb: any): this; /** * render screen * * @param {string} error output screen footer */ render(error?: string): void; /** * resolve source to get renderChoices * * @param {string} input search input */ search(input?: string): Promise<any>; /** * resolve choice */ onChoice(): void; /** * resovle line <Enter> Events */ onSubmit(): void; onSubmitAfterValidation(choices: ChoiceType<string>[]): void; /** * keypress handler */ onKeypress(e: { key: { name?: string; ctrl?: boolean; }; value: string; }): void; ensureSelectedInRange(): void; } /** * @description inquirer plugin - Support completion of input * @author Zhengqbbb <zhengqbbb@gmail.com> * @license MIT */ declare class CompleteInput extends Base { private completeValue?; private answer?; private state?; private done; constructor(questions: Question, readline: Interface, answers: Answers); /** * Start the Inquiry session * * @param {Function} cb Callback when prompt is done */ _run(cb: any): this; /** * render screen * * @param {string} error output screen footer */ render(error?: string): void; filterInput(input?: string): string; onEnd(state: { value?: string; }): void; onError({ value, isValid }: any): void; onKeypress(e: { key: { name?: string; ctrl?: boolean; }; value: string; }): void; } export { type BaseOptionType, type ChoiceType, type ChoicesType, CompleteInput, type CompletePromptQuestionOptions, type FilterArrayItemType, SearchCheckbox, SearchList, type SearchPromptQuestionOptions, ansiEscapes, createStyle, figures, fuzzyFilter, fuzzyMatch, isColorizenSupport, isUnicodeSupport, style };