(opts: TextOptions)
| 56 | * ``` |
| 57 | */ |
| 58 | export const text = (opts: TextOptions) => { |
| 59 | return new TextPrompt({ |
| 60 | validate: opts.validate, |
| 61 | placeholder: opts.placeholder, |
| 62 | defaultValue: opts.defaultValue, |
| 63 | initialValue: opts.initialValue, |
| 64 | output: opts.output, |
| 65 | signal: opts.signal, |
| 66 | input: opts.input, |
| 67 | render() { |
| 68 | const hasGuide = opts?.withGuide ?? settings.withGuide; |
| 69 | const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `; |
| 70 | const title = `${titlePrefix}${opts.message}\n`; |
| 71 | const placeholder = opts.placeholder |
| 72 | ? styleText('inverse', opts.placeholder[0]) + styleText('dim', opts.placeholder.slice(1)) |
| 73 | : styleText(['inverse', 'hidden'], '_'); |
| 74 | const userInput = !this.userInput ? placeholder : this.userInputWithCursor; |
| 75 | const value = this.value ?? ''; |
| 76 | |
| 77 | switch (this.state) { |
| 78 | case 'error': { |
| 79 | const errorText = this.error ? ` ${styleText('yellow', this.error)}` : ''; |
| 80 | const errorPrefix = hasGuide ? `${styleText('yellow', S_BAR)} ` : ''; |
| 81 | const errorPrefixEnd = hasGuide ? styleText('yellow', S_BAR_END) : ''; |
| 82 | return `${title.trim()}\n${errorPrefix}${userInput}\n${errorPrefixEnd}${errorText}\n`; |
| 83 | } |
| 84 | case 'submit': { |
| 85 | const valueText = value ? ` ${styleText('dim', value)}` : ''; |
| 86 | const submitPrefix = hasGuide ? styleText('gray', S_BAR) : ''; |
| 87 | return `${title}${submitPrefix}${valueText}`; |
| 88 | } |
| 89 | case 'cancel': { |
| 90 | const valueText = value ? ` ${styleText(['strikethrough', 'dim'], value)}` : ''; |
| 91 | const cancelPrefix = hasGuide ? styleText('gray', S_BAR) : ''; |
| 92 | return `${title}${cancelPrefix}${valueText}${value.trim() ? `\n${cancelPrefix}` : ''}`; |
| 93 | } |
| 94 | default: { |
| 95 | const defaultPrefix = hasGuide ? `${styleText('cyan', S_BAR)} ` : ''; |
| 96 | const defaultPrefixEnd = hasGuide ? styleText('cyan', S_BAR_END) : ''; |
| 97 | return `${title}${defaultPrefix}${userInput}\n${defaultPrefixEnd}\n`; |
| 98 | } |
| 99 | } |
| 100 | }, |
| 101 | }).prompt() as Promise<string | symbol>; |
| 102 | }; |
no test coverage detected