Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions packages/cli-kit/src/public/common/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,34 @@ export function tryParseInt(maybeInt: string | undefined): number | undefined {
* @returns A string with the columns aligned.
*/
export function linesToColumns(lines: string[][]): string {
const widths: number[] = []
for (let i = 0; lines[0] && i < lines[0].length; i++) {
const columnRows = lines.map((line) => line[i]!)
widths.push(Math.max(...columnRows.map((row) => unstyled(row).length)))
}
if (lines.length === 0) return ''

const numColumns = lines[0]!.length
const widths: number[] = new Array(numColumns).fill(0)

// Optimization: Calculate max widths for all columns while caching unstyled lengths.
// This reduces regex-based 'unstyled' calls by 50% and avoids redundant O(Cols * Rows) array mapping.
const unstyledLengths = lines.map((line) => {
const lineLengths = new Array(line.length)
for (let i = 0; i < line.length; i++) {
const length = unstyled(line[i]!).length
lineLengths[i] = length
if (i < numColumns && length > widths[i]!) {
widths[i] = length
}
}
return lineLengths
})

const paddedLines = lines
.map((line) => {
.map((line, rowIndex) => {
const rowLengths = unstyledLengths[rowIndex]!
return line
.map((col, index) => {
return `${col}${' '.repeat(widths[index]! - unstyled(col).length)}`
.map((col, colIndex) => {
const cellWidth = rowLengths[colIndex]!
const columnWidth = widths[colIndex] ?? cellWidth
const padding = ' '.repeat(columnWidth - cellWidth)
return `${col}${padding}`
})
.join(' ')
.trimEnd()
Expand Down
Loading