[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: log prompt multiline

Bruno Rocha (Aug 12, 2023, 6:57 PM -0300) b7100aac a73d9ecb

+54 -37
+54 -37
packages/prompts/src/index.ts
··· 65 65 initialSymbol?: string; 66 66 newLineSymbol?: string; 67 67 endSymbol?: string; 68 + maxWidth?: number; 68 69 lineWrapper?: (line: string) => string; 69 70 } 70 71 ): string { ··· 74 75 const endSymbol = options?.endSymbol ?? defaultSymbol; 75 76 76 77 const terminalWidth = process.stdout.columns || 80; 77 - const maxWidth = terminalWidth - 2; 78 + const maxWidth = options?.maxWidth ?? terminalWidth - 2; 78 79 79 80 const formattedLines: string[] = []; 80 81 const paragraphs = text.split(/\n/g); ··· 98 99 return formattedLines 99 100 .map((line, i, ar) => { 100 101 const symbol = i === 0 ? initialSymbol : i + 1 === ar.length ? endSymbol : newLineSymbol; 101 - const fullLine = line.padEnd(maxWidth + 1, ' '); 102 - const wrappedLine = options?.lineWrapper ? options.lineWrapper(fullLine) : fullLine; 103 - return `${symbol} ${wrappedLine}`; 102 + const wrappedLine = options?.lineWrapper ? options.lineWrapper(line) : line; 103 + const fullLine = wrappedLine + ' '.repeat(Math.max(maxWidth - strLength(wrappedLine), 0)); 104 + return `${symbol} ${fullLine}`; 104 105 }) 105 106 .join('\n'); 106 107 } ··· 486 487 this.options 487 488 .filter(({ value }) => this.value.includes(value)) 488 489 .map((option) => opt(option, 'submitted')) 489 - .join(color.dim(', ')), 490 + .join(color.dim(', ')) 490 491 ), 491 492 ].join('\n'); 492 493 } ··· 712 713 return len; 713 714 }; 714 715 export const note = (message = '', title = '') => { 715 - const lines = `\n${message}\n`.split('\n'); 716 - const titleLen = strip(title).length; 717 - const len = 718 - Math.max( 719 - lines.reduce((sum, ln) => { 720 - ln = strip(ln); 721 - return ln.length > sum ? ln.length : sum; 722 - }, 0), 723 - titleLen 724 - ) + 2; 725 - const msg = lines 726 - .map( 727 - (ln) => 728 - `${color.gray(S_BAR)} ${color.dim(ln)}${' '.repeat(len - strip(ln).length)}${color.gray( 729 - S_BAR 730 - )}` 731 - ) 732 - .join('\n'); 733 - process.stdout.write( 734 - `${color.gray(S_BAR)}\n${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray( 735 - S_BAR_H.repeat(Math.max(len - titleLen - 1, 1)) + S_CORNER_TOP_RIGHT 736 - )}\n${msg}\n${color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len + 2) + S_CORNER_BOTTOM_RIGHT)}\n` 737 - ); 716 + const len = Math.floor((process.stdout.columns ?? 80) * 0.8); 717 + const messageLen = Math.floor(len * 0.95); 718 + const noteBox = [ 719 + color.gray(S_BAR), 720 + `${color.green(S_STEP_SUBMIT)} ${color.reset(title)} ${color.gray( 721 + S_BAR_H.repeat(len - strLength(title) - 3) + S_CORNER_TOP_RIGHT 722 + )}`, 723 + color.gray(S_BAR + ' '.repeat(len) + S_BAR), 724 + formatTextWithMaxWidth(message, { 725 + defaultSymbol: color.gray(S_BAR), 726 + maxWidth: messageLen, 727 + lineWrapper: (line) => line + ' '.repeat(len - strLength(line) - 1) + color.gray(S_BAR), 728 + }), 729 + color.gray(S_BAR + ' '.repeat(len) + S_BAR), 730 + color.gray(S_CONNECT_LEFT + S_BAR_H.repeat(len) + S_CORNER_BOTTOM_RIGHT), 731 + '', 732 + ].join('\n'); 733 + process.stdout.write(noteBox); 738 734 }; 739 735 740 736 export const cancel = (message = '') => { 741 - process.stdout.write(`${color.gray(S_BAR_END)} ${color.red(message)}\n\n`); 737 + process.stdout.write( 738 + formatTextWithMaxWidth(message, { 739 + defaultSymbol: color.gray(S_BAR), 740 + initialSymbol: color.gray(S_BAR_END), 741 + lineWrapper: color.red, 742 + }) + '\n\n' 743 + ); 742 744 }; 743 745 744 746 export const intro = (title = '') => { 745 - process.stdout.write(`${color.gray(S_BAR_START)} ${title}\n`); 747 + process.stdout.write( 748 + formatTextWithMaxWidth(title, { 749 + initialSymbol: color.gray(S_BAR_START), 750 + defaultSymbol: color.gray(S_BAR), 751 + }) 752 + ); 746 753 }; 747 754 748 755 export const outro = (message = '') => { 749 - process.stdout.write(`${color.gray(S_BAR)}\n${color.gray(S_BAR_END)} ${message}\n\n`); 756 + process.stdout.write( 757 + [ 758 + color.gray(S_BAR), 759 + formatTextWithMaxWidth(message, { 760 + defaultSymbol: color.gray(S_BAR), 761 + endSymbol: color.gray(S_BAR_END), 762 + }), 763 + '', 764 + '', 765 + ].join('\n') 766 + ); 750 767 }; 751 768 752 769 export type LogMessageOptions = { ··· 754 771 }; 755 772 export const log = { 756 773 message: (message = '', { symbol = color.gray(S_BAR) }: LogMessageOptions = {}) => { 757 - const parts = [`${color.gray(S_BAR)}`]; 758 - if (message) { 759 - const [firstLine, ...lines] = message.split('\n'); 760 - parts.push(`${symbol} ${firstLine}`, ...lines.map((ln) => `${color.gray(S_BAR)} ${ln}`)); 761 - } 762 - process.stdout.write(`${parts.join('\n')}\n`); 774 + process.stdout.write( 775 + formatTextWithMaxWidth(message, { 776 + initialSymbol: symbol, 777 + defaultSymbol: color.gray(S_BAR), 778 + }) 779 + ); 763 780 }, 764 781 info: (message: string) => { 765 782 log.message(message, { symbol: color.blue(S_INFO) });