Skip to content

Commit 9054e65

Browse files
committed
feat: Added checking state to password input
1 parent 92b47ca commit 9054e65

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/ui/components/widgets/SudoPasswordInput.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import { Box, Text, useInput } from 'ink';
22
import React, { useState } from 'react';
33

4+
import Spinner from '../progress/spinner.js';
5+
46
export function SudoPasswordInput(props: {
57
hasError: boolean;
68
onSubmit: (password: string) => void;
79
onCancel: () => void;
810
}) {
911
const { hasError, onSubmit, onCancel } = props;
1012
const [value, setValue] = useState('');
13+
const [isChecking, setIsChecking] = useState(false);
1114

1215
const borderColor = hasError ? 'red' : 'cyan';
1316

1417
useInput((input, key) => {
18+
if (isChecking) return;
19+
1520
if (key.escape) {
1621
onCancel();
1722
return;
1823
}
1924

2025
if (key.return) {
26+
setIsChecking(true);
2127
onSubmit(value);
2228
setValue('');
2329
return;
@@ -45,13 +51,17 @@ export function SudoPasswordInput(props: {
4551
borderColor={borderColor}
4652
marginTop={1}
4753
>
48-
<Box gap={1}>
49-
<Text bold color={borderColor}>Sudo Password:</Text>
50-
<Text>{value.replace(/./g, '*')}</Text>
51-
<Text inverse> </Text>
52-
</Box>
54+
{isChecking ? (
55+
<Spinner label="Checking password..." />
56+
) : (
57+
<Box gap={1}>
58+
<Text bold color={borderColor}>Sudo Password:</Text>
59+
<Text>{value.replace(/./g, '*')}</Text>
60+
<Text inverse> </Text>
61+
</Box>
62+
)}
5363
{hasError && <Text color="red">Incorrect password, try again</Text>}
54-
<Text dimColor>Enter to confirm · Esc to cancel</Text>
64+
{!isChecking && <Text dimColor>Enter to confirm · Esc to cancel</Text>}
5565
</Box>
5666
);
57-
}
67+
}

src/ui/reporters/default-reporter.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,21 +326,30 @@ export class DefaultReporter implements Reporter {
326326
private async handleInlineSudoPassword(): Promise<void> {
327327
let attemptCount = 0;
328328

329+
await sleep(50);
330+
this.updateRenderState(RenderStatus.NOTHING);
331+
await sleep(50);
332+
329333
while (attemptCount < 3) {
330-
const result = await (await Promise.all([
334+
const result = (await Promise.all([
331335
this.updateRenderState(RenderStatus.SUDO_PROMPT, attemptCount),
332336
Promise.race([
333337
this.awaitEvent<string>(RenderEvent.SUDO_PROMPT_RESULT),
334-
this.awaitEvent<'cancel'>(RenderEvent.SUDO_PASSWORD_CANCEL),
338+
this.awaitEvent<'cancel'>(RenderEvent.SUDO_PASSWORD_CANCEL).then(() => Symbol.for('cancel')),
335339
]),
336-
])).at(1) as string | 'cancel';
340+
])).at(1) as string | Symbol;
337341

338-
if (result === 'cancel') {
342+
if (result === Symbol.for('cancel')) {
343+
ctx.log('Sudo password cancelled');
339344
break;
345+
} else {
346+
ctx.log('Sudo password attempt');
340347
}
341348

342349
const isValid = this.sudoPasswordSubmittedCallback?.(result) ?? false;
343350
if (isValid) {
351+
ctx.log('Sudo password successful!');
352+
344353
await sleep(50);
345354
this.updateRenderState(RenderStatus.NOTHING, null);
346355
await sleep(50);
@@ -349,6 +358,7 @@ export class DefaultReporter implements Reporter {
349358
return;
350359
}
351360

361+
ctx.log('Sudo password failed');
352362
attemptCount++;
353363
}
354364

0 commit comments

Comments
 (0)