| |
| |
| |
| |
| |
|
|
| import React from 'react'; |
| import { Text, Box } from 'ink'; |
| import { Colors } from '../colors.js'; |
| import { RenderInline, getPlainTextLength } from './InlineMarkdownRenderer.js'; |
|
|
| interface TableRendererProps { |
| headers: string[]; |
| rows: string[][]; |
| terminalWidth: number; |
| } |
|
|
| |
| |
| |
| |
| export const TableRenderer: React.FC<TableRendererProps> = ({ |
| headers, |
| rows, |
| terminalWidth, |
| }) => { |
| |
| const columnWidths = headers.map((header, index) => { |
| const headerWidth = getPlainTextLength(header); |
| const maxRowWidth = Math.max( |
| ...rows.map((row) => getPlainTextLength(row[index] || '')), |
| ); |
| return Math.max(headerWidth, maxRowWidth) + 2; |
| }); |
|
|
| |
| const totalWidth = columnWidths.reduce((sum, width) => sum + width + 1, 1); |
| const scaleFactor = |
| totalWidth > terminalWidth ? terminalWidth / totalWidth : 1; |
| const adjustedWidths = columnWidths.map((width) => |
| Math.floor(width * scaleFactor), |
| ); |
|
|
| |
| const renderCell = ( |
| content: string, |
| width: number, |
| isHeader = false, |
| ): React.ReactNode => { |
| const contentWidth = Math.max(0, width - 2); |
| const displayWidth = getPlainTextLength(content); |
|
|
| let cellContent = content; |
| if (displayWidth > contentWidth) { |
| if (contentWidth <= 3) { |
| |
| cellContent = content.substring( |
| 0, |
| Math.min(content.length, contentWidth), |
| ); |
| } else { |
| |
| let left = 0; |
| let right = content.length; |
| let bestTruncated = content; |
|
|
| |
| while (left <= right) { |
| const mid = Math.floor((left + right) / 2); |
| const candidate = content.substring(0, mid); |
| const candidateWidth = getPlainTextLength(candidate); |
|
|
| if (candidateWidth <= contentWidth - 3) { |
| bestTruncated = candidate; |
| left = mid + 1; |
| } else { |
| right = mid - 1; |
| } |
| } |
|
|
| cellContent = bestTruncated + '...'; |
| } |
| } |
|
|
| |
| const actualDisplayWidth = getPlainTextLength(cellContent); |
| const paddingNeeded = Math.max(0, contentWidth - actualDisplayWidth); |
|
|
| return ( |
| <Text> |
| {isHeader ? ( |
| <Text bold color={Colors.AccentCyan}> |
| <RenderInline text={cellContent} /> |
| </Text> |
| ) : ( |
| <RenderInline text={cellContent} /> |
| )} |
| {' '.repeat(paddingNeeded)} |
| </Text> |
| ); |
| }; |
|
|
| |
| const renderBorder = (type: 'top' | 'middle' | 'bottom'): React.ReactNode => { |
| const chars = { |
| top: { left: 'β', middle: 'β¬', right: 'β', horizontal: 'β' }, |
| middle: { left: 'β', middle: 'βΌ', right: 'β€', horizontal: 'β' }, |
| bottom: { left: 'β', middle: 'β΄', right: 'β', horizontal: 'β' }, |
| }; |
|
|
| const char = chars[type]; |
| const borderParts = adjustedWidths.map((w) => char.horizontal.repeat(w)); |
| const border = char.left + borderParts.join(char.middle) + char.right; |
|
|
| return <Text>{border}</Text>; |
| }; |
|
|
| |
| const renderRow = (cells: string[], isHeader = false): React.ReactNode => { |
| const renderedCells = cells.map((cell, index) => { |
| const width = adjustedWidths[index] || 0; |
| return renderCell(cell || '', width, isHeader); |
| }); |
|
|
| return ( |
| <Text> |
| β{' '} |
| {renderedCells.map((cell, index) => ( |
| <React.Fragment key={index}> |
| {cell} |
| {index < renderedCells.length - 1 ? ' β ' : ''} |
| </React.Fragment> |
| ))}{' '} |
| β |
| </Text> |
| ); |
| }; |
|
|
| return ( |
| <Box flexDirection="column" marginY={1}> |
| {/* Top border */} |
| {renderBorder('top')} |
| |
| {/* Header row */} |
| {renderRow(headers, true)} |
| |
| {/* Middle border */} |
| {renderBorder('middle')} |
| |
| {/* Data rows */} |
| {rows.map((row, index) => ( |
| <React.Fragment key={index}>{renderRow(row)}</React.Fragment> |
| ))} |
| |
| {/* Bottom border */} |
| {renderBorder('bottom')} |
| </Box> |
| ); |
| }; |
|
|