@inquirer/password 5.1.2 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,12 +60,13 @@ const answer = await password({ message: 'Enter your name' });
60
60
 
61
61
  ## Options
62
62
 
63
- | Property | Type | Required | Description |
64
- | -------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
65
- | message | `string` | yes | The question to ask |
66
- | mask | `boolean` | no | Show a `*` mask over the input or keep it transparent |
67
- | validate | `string => boolean \| string \| Promise<boolean \| string>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |
68
- | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
63
+ | Property | Type | Required | Description |
64
+ | ---------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
65
+ | message | `string` | yes | The question to ask |
66
+ | mask | `boolean \| string` | no | Show a `*` mask over the input, use a custom mask character, or keep it transparent. |
67
+ | toggleMask | `boolean` | no | Allow the user to press `ctrl+t` to temporarily reveal the typed value. Defaults to `true`; set to `false` to disable both the shortcut and its help line. |
68
+ | validate | `string => boolean \| string \| Promise<boolean \| string>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |
69
+ | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
69
70
 
70
71
  ## Theming
71
72
 
@@ -83,10 +84,27 @@ type Theme = {
83
84
  message: (text: string, status: 'idle' | 'done' | 'loading') => string;
84
85
  error: (text: string) => string;
85
86
  help: (text: string) => string;
87
+ maskedText: string;
88
+ keysHelpTip: (keys: [key: string, action: string][]) => string | undefined;
86
89
  };
87
90
  };
88
91
  ```
89
92
 
93
+ `maskedText` is the inline tip shown when `mask` is off and the value is hidden (default: `[input is masked]`).
94
+
95
+ ### `theme.style.keysHelpTip`
96
+
97
+ This function customizes the keyboard shortcut help displayed on the line below the prompt. It receives `[['ctrl+t', 'toggle visibility']]` while the value is hidden. Use it to change the formatting or localize the action, or return `undefined` to hide the help line entirely.
98
+
99
+ ```js
100
+ theme: {
101
+ style: {
102
+ keysHelpTip: (keys) =>
103
+ keys.map(([key, action]) => `${key}: ${action}`).join(' | '),
104
+ },
105
+ }
106
+ ```
107
+
90
108
  # License
91
109
 
92
110
  Copyright (c) 2023 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))<br/>
package/dist/index.d.ts CHANGED
@@ -3,13 +3,21 @@ import type { PartialDeep } from '@inquirer/type';
3
3
  type PasswordTheme = {
4
4
  style: {
5
5
  maskedText: string;
6
+ keysHelpTip: (keys: [key: string, action: string][]) => string | undefined;
6
7
  };
7
8
  };
8
9
  type PasswordConfig = {
9
10
  message: string;
10
11
  mask?: boolean | string;
12
+ toggleMask?: boolean;
11
13
  validate?: (value: string) => boolean | string | Promise<string | boolean>;
12
14
  theme?: PartialDeep<Theme<PasswordTheme>>;
13
15
  };
14
- declare const _default: import("@inquirer/type").Prompt<string, PasswordConfig>;
16
+ declare const _default: import("@inquirer/type").Prompt<string, {
17
+ message: string;
18
+ mask?: boolean | string | undefined;
19
+ toggleMask?: boolean | undefined;
20
+ validate?: ((value: string) => boolean | string | Promise<string | boolean>) | undefined;
21
+ theme?: PartialDeep<Theme<PasswordTheme>> | undefined;
22
+ } & PasswordConfig>;
15
23
  export default _default;
package/dist/index.js CHANGED
@@ -1,16 +1,21 @@
1
1
  import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core';
2
2
  import { cursorHide } from '@inquirer/ansi';
3
+ import { styleText } from 'node:util';
3
4
  const passwordTheme = {
4
5
  style: {
5
6
  maskedText: '[input is masked]',
7
+ keysHelpTip: (keys) => keys
8
+ .map(([key, action]) => `${styleText('bold', key)} ${styleText('dim', action)}`)
9
+ .join(styleText('dim', ' • ')),
6
10
  },
7
11
  };
8
12
  export default createPrompt((config, done) => {
9
- const { validate = () => true } = config;
13
+ const { toggleMask = true, validate = () => true } = config;
10
14
  const theme = makeTheme(passwordTheme, config.theme);
11
15
  const [status, setStatus] = useState('idle');
12
16
  const [errorMsg, setError] = useState();
13
17
  const [value, setValue] = useState('');
18
+ const [revealed, setRevealed] = useState(false);
14
19
  const prefix = usePrefix({ status, theme });
15
20
  useKeypress(async (key, rl) => {
16
21
  // Ignore keypress while our prompt is doing other processing.
@@ -34,27 +39,41 @@ export default createPrompt((config, done) => {
34
39
  setStatus('idle');
35
40
  }
36
41
  }
42
+ else if (toggleMask && key.ctrl && key.name === 't') {
43
+ setRevealed((prev) => !prev);
44
+ }
37
45
  else {
38
46
  setValue(rl.line);
39
47
  setError(undefined);
40
48
  }
41
49
  });
42
50
  const message = theme.style.message(config.message, status);
51
+ const showPlaintext = toggleMask && revealed && status === 'idle';
43
52
  let formattedValue = '';
44
- let helpTip;
45
- if (config.mask) {
53
+ if (showPlaintext) {
54
+ formattedValue = value;
55
+ }
56
+ else if (config.mask) {
46
57
  const maskChar = typeof config.mask === 'string' ? config.mask : '*';
47
58
  formattedValue = maskChar.repeat(value.length);
48
59
  }
49
60
  else if (status !== 'done') {
50
- helpTip = `${theme.style.help(theme.style.maskedText)}${cursorHide}`;
61
+ formattedValue = theme.style.help(theme.style.maskedText);
51
62
  }
52
63
  if (status === 'done') {
53
64
  formattedValue = theme.style.answer(formattedValue);
54
65
  }
55
- let error = '';
56
- if (errorMsg) {
57
- error = theme.style.error(errorMsg);
66
+ else if (!config.mask) {
67
+ formattedValue += cursorHide;
58
68
  }
59
- return [[prefix, message, config.mask ? formattedValue : helpTip].join(' '), error];
69
+ const content = [prefix, message, formattedValue].filter(Boolean).join(' ');
70
+ const bottomContent = [
71
+ errorMsg ? theme.style.error(errorMsg) : '',
72
+ toggleMask && status === 'idle'
73
+ ? theme.style.keysHelpTip([['ctrl+t', 'toggle visibility']])
74
+ : '',
75
+ ]
76
+ .filter(Boolean)
77
+ .join('\n');
78
+ return [content, bottomContent];
60
79
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/password",
3
- "version": "5.1.2",
3
+ "version": "5.2.1",
4
4
  "description": "Inquirer password prompt",
5
5
  "keywords": [
6
6
  "answer",
@@ -67,9 +67,9 @@
67
67
  "tsc": "tsc"
68
68
  },
69
69
  "dependencies": {
70
- "@inquirer/ansi": "^2.0.7",
71
- "@inquirer/core": "^12.0.0",
72
- "@inquirer/type": "^4.0.7"
70
+ "@inquirer/ansi": "^2.0.8",
71
+ "@inquirer/core": "^12.0.2",
72
+ "@inquirer/type": "4.1.1"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@repo/tsconfig": "0.0.0",
@@ -88,5 +88,5 @@
88
88
  },
89
89
  "main": "./dist/index.js",
90
90
  "types": "./dist/index.d.ts",
91
- "gitHead": "999706755afbdcae271f62decbd9bcd05560905b"
91
+ "gitHead": "5748bd9966b5e175ddf5995ee4cdce6f60c4272e"
92
92
  }