Skip to content

Commit 160027b

Browse files
committed
refactor: remove redundances
1 parent 8449d6a commit 160027b

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

packages/prompts/src/index.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -602,47 +602,43 @@ export const log = {
602602
},
603603
};
604604

605-
const frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'];
606-
607605
export const spinner = () => {
608606
let unblock: () => void;
609607
let loop: NodeJS.Timer;
610608
let isSpinnerActive: boolean = false;
609+
const frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'];
611610
const delay = unicode ? 80 : 120;
612611

613612
const start = (message: string = ''): void => {
614613
isSpinnerActive = true;
615-
message = message.replace(/\.?\.?\.$/, '');
616614
unblock = block();
617-
process.stdout.write(`${color.gray(S_BAR)}\n${color.magenta('○')} ${message}\n`);
618-
let i = 0;
619-
let dot = 0;
615+
message = message.replace(/\.+$/, '');
616+
process.stdout.write(`${color.gray(S_BAR)}\n`);
617+
let frameIndex = 0;
618+
let dotsTimer = 0;
620619
loop = setInterval(() => {
621-
let frame = frames[i];
622-
process.stdout.write(cursor.move(-999, -1));
623-
process.stdout.write(
624-
`${color.magenta(frame)} ${message}${
625-
Math.floor(dot) >= 1 ? '.'.repeat(Math.floor(dot)).slice(0, 3) : ''
626-
} \n`
627-
);
628-
i = i === frames.length - 1 ? 0 : i + 1;
629-
dot = dot === frames.length ? 0 : dot + 0.125;
620+
const frame = color.magenta(frames[frameIndex]);
621+
const loadingDots = '.'.repeat(Math.floor(dotsTimer)).slice(0, 3);
622+
process.stdout.write(cursor.move(-999, 0));
623+
process.stdout.write(erase.down(1));
624+
process.stdout.write(`${frame} ${message}${loadingDots}`);
625+
frameIndex = frameIndex + 1 < frames.length ? frameIndex + 1 : 0;
626+
dotsTimer = dotsTimer < frames.length ? dotsTimer + 0.125 : 0;
630627
}, delay);
631628
};
632629

633630
const stop = (message: string = '', code: number = 0): void => {
634631
isSpinnerActive = false;
635-
process.stdout.write(cursor.move(-999, -2));
636-
process.stdout.write(erase.down(2));
637632
clearInterval(loop);
638-
const bar = color.gray(S_BAR);
639633
const step =
640634
code === 0
641635
? color.green(S_STEP_SUBMIT)
642636
: code === 1
643637
? color.red(S_STEP_CANCEL)
644638
: color.red(S_STEP_ERROR);
645-
process.stdout.write(`${bar}\n${step} ${message}\n`);
639+
process.stdout.write(cursor.move(-999, 0));
640+
process.stdout.write(erase.down(1));
641+
process.stdout.write(`${step} ${message}\n\n`);
646642
unblock();
647643
};
648644

@@ -652,15 +648,18 @@ export const spinner = () => {
652648
}
653649
};
654650

655-
// Trigger on uncaught code exception
651+
/**
652+
* Signal Events: https://nodejs.org/api/process.html#signal-events
653+
* `uncaughtException`: Trigger on uncaught code exception
654+
* `unhandledRejection`: Trigger on unhandled promise rejection
655+
* `SIGINT`: Trigger on Ctrl + C. PS: it will not trigger while terminal raw mode is enabled
656+
* `SIGTERM`: Trigger on kill process
657+
* `exit`: Trigger on exit
658+
*/
656659
process.on('uncaughtException', () => handleExit(2));
657-
// Trigger on unhandled promise rejection
658660
process.on('unhandledRejection', () => handleExit(2));
659-
// Trigger on Ctrl + C -> multi platform
660661
process.on('SIGINT', () => handleExit(1));
661-
// Trigger on kill process
662662
process.on('SIGTERM', () => handleExit(1));
663-
// Trigger on system shutdown
664663
process.on('exit', handleExit);
665664

666665
return {

0 commit comments

Comments
 (0)