[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: spinner multiline support

Bruno Rocha (Aug 13, 2023, 10:55 AM -0300) 2ada17d7 d7d2b3ea

+22 -8
+22 -8
packages/prompts/src/index.ts
··· 807 807 let loop: NodeJS.Timer; 808 808 let isSpinnerActive: boolean = false; 809 809 let _message: string = ''; 810 + let _prevMessage: string = ''; 811 + 812 + const formatMessage = (symbol: string, msg: string): string => { 813 + return formatTextWithMaxWidth(msg, { 814 + initialSymbol: symbol, 815 + defaultSymbol: '', 816 + }); 817 + }; 818 + 819 + const clearPrevMessage = (): void => { 820 + const linesCounter = _prevMessage.split(/\n/g).length; 821 + process.stdout.write(cursor.move(-999, (linesCounter - 1) * -1)); 822 + process.stdout.write(erase.down(linesCounter)); 823 + }; 810 824 811 825 const start = (msg: string = ''): void => { 812 826 isSpinnerActive = true; ··· 816 830 let frameIndex = 0; 817 831 let dotsTimer = 0; 818 832 loop = setInterval(() => { 833 + clearPrevMessage(); 819 834 const frame = color.magenta(frames[frameIndex]); 820 835 const loadingDots = '.'.repeat(Math.floor(dotsTimer)).slice(0, 3); 821 - process.stdout.write(cursor.move(-999, 0)); 822 - process.stdout.write(erase.down(1)); 823 - process.stdout.write(`${frame} ${_message}${loadingDots}`); 836 + const newMessage = formatMessage(frame, _message + loadingDots); 837 + _prevMessage = newMessage; 838 + process.stdout.write(newMessage); 824 839 frameIndex = frameIndex + 1 < frames.length ? frameIndex + 1 : 0; 825 840 dotsTimer = dotsTimer < frames.length ? dotsTimer + 0.125 : 0; 826 841 }, delay); 827 842 }; 828 843 829 844 const stop = (msg: string = '', code: number = 0): void => { 830 - _message = msg ?? _message; 831 845 isSpinnerActive = false; 832 846 clearInterval(loop); 847 + clearPrevMessage(); 833 848 const step = 834 849 code === 0 835 850 ? color.green(S_STEP_SUBMIT) 836 851 : code === 1 837 852 ? color.red(S_STEP_CANCEL) 838 853 : color.red(S_STEP_ERROR); 839 - process.stdout.write(cursor.move(-999, 0)); 840 - process.stdout.write(erase.down(1)); 841 - process.stdout.write(`${step} ${_message}\n`); 854 + _message = formatMessage(step, msg || _message); 855 + process.stdout.write(_message + '\n'); 842 856 unblock(); 843 857 }; 844 858 845 859 const message = (msg: string = ''): void => { 846 - _message = msg ?? _message; 860 + _message = msg || _message; 847 861 }; 848 862 849 863 const handleExit = (code: number) => {