| 99 | }; |
| 100 | |
| 101 | export function wrapTextWithPrefix( |
| 102 | output: Writable | undefined, |
| 103 | text: string, |
| 104 | prefix: string, |
| 105 | startPrefix: string = prefix, |
| 106 | endPrefix: string = prefix, |
| 107 | lineFormatter?: (line: string, index: number) => string |
| 108 | ): string { |
| 109 | const columns = getColumns(output ?? stdout); |
| 110 | const wrapped = wrapAnsi(text, columns - prefix.length, { |
| 111 | hard: true, |
| 112 | trim: false, |
| 113 | }); |
| 114 | const lines = wrapped |
| 115 | .split('\n') |
| 116 | .map((line, index, arr) => { |
| 117 | const lineString = lineFormatter ? lineFormatter(line, index) : line; |
| 118 | if (index === 0) { |
| 119 | return `${startPrefix}${lineString}`; |
| 120 | } else if (index === arr.length - 1) { |
| 121 | return `${endPrefix}${lineString}`; |
| 122 | } |
| 123 | return `${prefix}${lineString}`; |
| 124 | }) |
| 125 | .join('\n'); |
| 126 | return lines; |
| 127 | } |