Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (password): add option to toggle password visibility #1334

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/password/password.test.mts
Expand Up @@ -85,4 +85,80 @@ describe('password prompt', () => {
events.keypress('enter');
await expect(answer).resolves.toEqual('12345678');
});

it('handle show password: false', async () => {
const { answer, events, getScreen } = await render(password, {
message: 'Enter your password',
mask: true,
allowShowPassword: false,
});

expect(getScreen()).toMatchInlineSnapshot('"? Enter your password"');

events.type('J');
expect(getScreen()).toMatchInlineSnapshot('"? Enter your password *"');

events.type('ohn');
expect(getScreen()).toMatchInlineSnapshot('"? Enter your password ****"');

events.keypress({
name: '`',
ctrl: true,
});
expect(getScreen()).toMatchInlineSnapshot('"? Enter your password ****"');

events.keypress('enter');
await expect(answer).resolves.toEqual('John');
expect(getScreen()).toMatchInlineSnapshot('"? Enter your password ****"');
});

it('handle show password: true', async () => {
const { answer, events, getScreen } = await render(password, {
message: 'Enter your password',
mask: true,
allowShowPassword: true,
});

expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password)"',
);

events.type('J');
expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password) *"',
);

events.type('ohn');
expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password) ****"',
);

events.keypress({
name: '`',
ctrl: true,
});
expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password) John"',
);

events.keypress({
name: '`',
ctrl: true,
});
expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password) ****"',
);

events.keypress({
name: '`',
ctrl: true,
});
expect(getScreen()).toMatchInlineSnapshot(
'"? Enter your password (CTRL+Space to show/hide the password) John"',
);

events.keypress('enter');
await expect(answer).resolves.toEqual('John');
expect(getScreen()).toMatchInlineSnapshot('"? Enter your password ****"');
});
});
23 changes: 21 additions & 2 deletions packages/password/src/index.mts
Expand Up @@ -12,13 +12,15 @@ import ansiEscapes from 'ansi-escapes';
type PasswordConfig = PromptConfig<{
mask?: boolean | string;
validate?: (value: string) => boolean | string | Promise<string | boolean>;
allowShowPassword?: boolean;
}>;

export default createPrompt<string, PasswordConfig>((config, done) => {
const { validate = () => true } = config;
const { validate = () => true, allowShowPassword } = config;
const [status, setStatus] = useState<string>('pending');
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [value, setValue] = useState<string>('');
const [isMasked, setIsMasked] = useState<boolean>(true);

const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
Expand All @@ -44,6 +46,12 @@ export default createPrompt<string, PasswordConfig>((config, done) => {
setError(isValid || 'You must provide a valid value');
setStatus('pending');
}
} else if (allowShowPassword && key.ctrl === true && key.name === '`') {
// CTRL + Space
// I only tried on Linux, but the combination on Linux was reported like that
// key.crtl = true
// key.name = '`'
Comment on lines +50 to +53
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sooo, did some more digging into this. And it turned out to be a bit of a headache.

Depending on the environment and the terminal used, there can be a lot of conflict.

For one, CTRL + Space for me open the AI helper sidebar and never reach the node program.

I then started to think of keys; like CTRL+H, but that's a backspace shortcut (and it doesn't even register as the H key.)

So long story short:

  1. I added node tools/keys.mjs to the repo latest version to play with.
  2. Potential solution 1: finding a key combo that'll reasonably work for most environment (terminals, OS & consider non-english keyboards).
  3. Have a basic OS switch and use keys per OS that are reasonably going to work.

setIsMasked(!isMasked);
} else {
setValue(rl.line);
setError(undefined);
Expand All @@ -60,6 +68,10 @@ export default createPrompt<string, PasswordConfig>((config, done) => {
formattedValue = `${chalk.dim('[input is masked]')}${ansiEscapes.cursorHide}`;
}

if (!isMasked && status !== 'done') {
formattedValue = value;
}

if (status === 'done') {
formattedValue = chalk.cyan(formattedValue);
}
Expand All @@ -69,5 +81,12 @@ export default createPrompt<string, PasswordConfig>((config, done) => {
error = chalk.red(`> ${errorMsg}`);
}

return [`${prefix} ${message} ${formattedValue}`, error];
return [
`${prefix} ${message}${
allowShowPassword && status !== 'done'
? chalk.dim(' (CTRL+Space to show/hide the password)')
: ''
} ${formattedValue}`,
error,
];
});