[READ-ONLY] Mirror of https://github.com/bombshell-dev/clack. Effortlessly build beautiful command-line apps bomb.sh/docs/clack/basics/getting-started/
cli command-line command-line-app node prompt prompts
5

Configure Feed

Select the types of activity you want to include in your feed.

feat: add password prompt to @clack/prompts

beerose (Feb 17, 2023, 5:26 PM +0100) 97c52ba0 c2dbd27c

+43 -1
+9
examples/basic/index.ts
··· 20 20 if (value[0] !== '.') return 'Please enter a relative path.'; 21 21 }, 22 22 }), 23 + password: () => 24 + p.password({ 25 + message: 'Provide a password', 26 + mask: '🧹', 27 + validate: (value) => { 28 + if (!value) return 'Please enter a password.'; 29 + if (value.length < 5) return 'Password should have at least 5 characters.'; 30 + }, 31 + }), 23 32 type: ({ results }) => 24 33 p.select({ 25 34 message: `Pick a project type within "${results.path}"`,
+1 -1
packages/core/src/prompts/password.ts
··· 10 10 get cursor() { 11 11 return this._cursor; 12 12 } 13 - private get masked() { 13 + get masked() { 14 14 return this.value 15 15 .split('') 16 16 .map(() => this._mask)
+33
packages/prompts/src/index.ts
··· 6 6 SelectPrompt, 7 7 State, 8 8 TextPrompt, 9 + PasswordPrompt, 9 10 } from '@clack/core'; 10 11 import isUnicodeSupported from 'is-unicode-supported'; 11 12 import color from 'picocolors'; ··· 80 81 return `${title}${color.gray(S_BAR)} ${color.strikethrough( 81 82 color.dim(this.value ?? '') 82 83 )}${this.value?.trim() ? '\n' + color.gray(S_BAR) : ''}`; 84 + default: 85 + return `${title}${color.cyan(S_BAR)} ${value}\n${color.cyan(S_BAR_END)}\n`; 86 + } 87 + }, 88 + }).prompt() as Promise<string | symbol>; 89 + }; 90 + 91 + export interface PasswordOptions { 92 + message: string; 93 + mask?: string; 94 + validate?: (value: string) => string | void; 95 + } 96 + export const password = (opts: PasswordOptions) => { 97 + return new PasswordPrompt({ 98 + validate: opts.validate, 99 + mask: opts.mask, 100 + render() { 101 + const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`; 102 + const value = this.valueWithCursor; 103 + const masked = this.masked; 104 + 105 + switch (this.state) { 106 + case 'error': 107 + return `${title.trim()}\n${color.yellow(S_BAR)} ${masked}\n${color.yellow( 108 + S_BAR_END 109 + )} ${color.yellow(this.error)}\n`; 110 + case 'submit': 111 + return `${title}${color.gray(S_BAR)} ${color.dim(masked)}`; 112 + case 'cancel': 113 + return `${title}${color.gray(S_BAR)} ${color.strikethrough(color.dim(masked ?? ''))}${ 114 + masked?.trim() ? '\n' + color.gray(S_BAR) : '' 115 + }`; 83 116 default: 84 117 return `${title}${color.cyan(S_BAR)} ${value}\n${color.cyan(S_BAR_END)}\n`; 85 118 }