({
token,
highlight,
forceWidth
}: Props)
| 70 | * 4. Properly aligning multi-line rows with borders |
| 71 | */ |
| 72 | export function MarkdownTable({ |
| 73 | token, |
| 74 | highlight, |
| 75 | forceWidth |
| 76 | }: Props): React.ReactNode { |
| 77 | const [theme] = useTheme(); |
| 78 | const { |
| 79 | columns: actualTerminalWidth |
| 80 | } = useTerminalSize(); |
| 81 | const terminalWidth = forceWidth ?? actualTerminalWidth; |
| 82 | |
| 83 | // Format cell content to ANSI string |
| 84 | function formatCell(tokens: Token[] | undefined): string { |
| 85 | return tokens?.map(_ => formatToken(_, theme, 0, null, null, highlight)).join('') ?? ''; |
| 86 | } |
| 87 | |
| 88 | // Get plain text (stripped of ANSI codes) |
| 89 | function getPlainText(tokens_0: Token[] | undefined): string { |
| 90 | return stripAnsi(formatCell(tokens_0)); |
| 91 | } |
| 92 | |
| 93 | // Get the longest word width in a cell (minimum width to avoid breaking words) |
| 94 | function getMinWidth(tokens_1: Token[] | undefined): number { |
| 95 | const text = getPlainText(tokens_1); |
| 96 | const words = text.split(/\s+/).filter(w => w.length > 0); |
| 97 | if (words.length === 0) return MIN_COLUMN_WIDTH; |
| 98 | return Math.max(...words.map(w_0 => stringWidth(w_0)), MIN_COLUMN_WIDTH); |
| 99 | } |
| 100 | |
| 101 | // Get ideal width (full content without wrapping) |
| 102 | function getIdealWidth(tokens_2: Token[] | undefined): number { |
| 103 | return Math.max(stringWidth(getPlainText(tokens_2)), MIN_COLUMN_WIDTH); |
| 104 | } |
| 105 | |
| 106 | // Calculate column widths |
| 107 | // Step 1: Get minimum (longest word) and ideal (full content) widths |
| 108 | const minWidths = token.header.map((header, colIndex) => { |
| 109 | let maxMinWidth = getMinWidth(header.tokens); |
| 110 | for (const row of token.rows) { |
| 111 | maxMinWidth = Math.max(maxMinWidth, getMinWidth(row[colIndex]?.tokens)); |
| 112 | } |
| 113 | return maxMinWidth; |
| 114 | }); |
| 115 | const idealWidths = token.header.map((header_0, colIndex_0) => { |
| 116 | let maxIdeal = getIdealWidth(header_0.tokens); |
| 117 | for (const row_0 of token.rows) { |
| 118 | maxIdeal = Math.max(maxIdeal, getIdealWidth(row_0[colIndex_0]?.tokens)); |
| 119 | } |
| 120 | return maxIdeal; |
| 121 | }); |
| 122 | |
| 123 | // Step 2: Calculate available space |
| 124 | // Border overhead: │ content │ content │ = 1 + (width + 3) per column |
| 125 | const numCols = token.header.length; |
| 126 | const borderOverhead = 1 + numCols * 3; // │ + (2 padding + 1 border) per col |
| 127 | // Account for SAFETY_MARGIN to avoid triggering the fallback safety check |
| 128 | const availableWidth = Math.max(terminalWidth - borderOverhead - SAFETY_MARGIN, numCols * MIN_COLUMN_WIDTH); |
| 129 |
nothing calls this directly
no test coverage detected