({
headers,
rows,
terminalWidth,
})
| 21 | * We implement our own instead of using ink-table due to module compatibility issues |
| 22 | */ |
| 23 | export const TableRenderer: React.FC<TableRendererProps> = ({ |
| 24 | headers, |
| 25 | rows, |
| 26 | terminalWidth, |
| 27 | }) => { |
| 28 | // Calculate column widths using actual display width after markdown processing |
| 29 | const columnWidths = headers.map((header, index) => { |
| 30 | const headerWidth = getPlainTextLength(header); |
| 31 | const maxRowWidth = Math.max( |
| 32 | ...rows.map((row) => getPlainTextLength(row[index] || '')), |
| 33 | ); |
| 34 | return Math.max(headerWidth, maxRowWidth) + 2; // Add padding |
| 35 | }); |
| 36 | |
| 37 | // Ensure table fits within terminal width |
| 38 | const totalWidth = columnWidths.reduce((sum, width) => sum + width + 1, 1); |
| 39 | const scaleFactor = |
| 40 | totalWidth > terminalWidth ? terminalWidth / totalWidth : 1; |
| 41 | const adjustedWidths = columnWidths.map((width) => |
| 42 | Math.floor(width * scaleFactor), |
| 43 | ); |
| 44 | |
| 45 | // Helper function to render a cell with proper width |
| 46 | const renderCell = ( |
| 47 | content: string, |
| 48 | width: number, |
| 49 | isHeader = false, |
| 50 | ): React.ReactNode => { |
| 51 | const contentWidth = Math.max(0, width - 2); |
| 52 | const displayWidth = getPlainTextLength(content); |
| 53 | |
| 54 | let cellContent = content; |
| 55 | if (displayWidth > contentWidth) { |
| 56 | if (contentWidth <= 3) { |
| 57 | // Just truncate by character count |
| 58 | cellContent = content.substring( |
| 59 | 0, |
| 60 | Math.min(content.length, contentWidth), |
| 61 | ); |
| 62 | } else { |
| 63 | // Truncate preserving markdown formatting using binary search |
| 64 | let left = 0; |
| 65 | let right = content.length; |
| 66 | let bestTruncated = content; |
| 67 | |
| 68 | // Binary search to find the optimal truncation point |
| 69 | while (left <= right) { |
| 70 | const mid = Math.floor((left + right) / 2); |
| 71 | const candidate = content.substring(0, mid); |
| 72 | const candidateWidth = getPlainTextLength(candidate); |
| 73 | |
| 74 | if (candidateWidth <= contentWidth - 3) { |
| 75 | bestTruncated = candidate; |
| 76 | left = mid + 1; |
| 77 | } else { |
| 78 | right = mid - 1; |
| 79 | } |
| 80 | } |
nothing calls this directly
no test coverage detected