You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apparently this repo is no longer compatible with the new version of Ink since it is a class component.
I wrote the following (almost) equivalent react function version:
// Table.tsximportReactfrom'react';import{Box,Text}from'ink';typeScalar=string|number|boolean|null|undefined;typeScalarDict={[key: string]: Scalar;};typeColumn={key: string;width: number;};typeTableProps={data: ScalarDict[];showHeaders?: boolean;headerStyles?: {color?: string;backgroundColor?: string;bold?: boolean;italic?: boolean;underline?: boolean;inverse?: boolean;strikethrough?: boolean;dimColor?: boolean;};};// Helper function to generate headers from datafunctiongenerateHeaders(data: ScalarDict[]): ScalarDict{letheaders: ScalarDict={};data.forEach(row=>{Object.keys(row).forEach(key=>{headers[key]=key;});});returnheaders;}constTable=({data, showHeaders =true, headerStyles}: TableProps)=>{// Determine columns and their widthsconstcolumns: Column[]=getColumns(data);return(<BoxflexDirection="column">{renderHeaderSeparators(columns)}{showHeaders&&(<>{renderRow(generateHeaders(data),columns,{color: 'blue',bold: true,
...headerStyles,})}{renderRowSeparators(columns)}</>)}{data.map((row,index)=>(<React.Fragmentkey={`row-${index}`}>{index !==0&&renderRowSeparators(columns)}{renderRow(row,columns)}</React.Fragment>
))}{renderFooterSeparators(columns)}</Box>);};// Helper function to determine columns and their widthsfunctiongetColumns(data: ScalarDict[]): Column[]{letcolumnWidths: {[key: string]: number}={};data.forEach(row=>{Object.keys(row).forEach(key=>{constvalueLength=row[key]?.toString().length||0;columnWidths[key]=Math.max(columnWidths[key]||key.length,valueLength,);});});returnObject.keys(columnWidths).map(key=>({key: key,width: (columnWidths[key]??0)+2,// adding padding}));}// Helper function to render a row with separatorsfunctionrenderRow(row: ScalarDict,columns: Column[],textStyles?: any){return(<BoxflexDirection="row"><Text>│</Text>
{columns.map((column,index)=>(<React.Fragmentkey={column.key}>{index!==0&&<Text>│</Text>}
{/* Add separator before each cell except the first one */}<Boxwidth={column.width}justifyContent="center"><Text{...textStyles}>{row[column.key]?.toString()||''}</Text></Box></React.Fragment>))}<Text>│</Text></Box>);}functionrenderHeaderSeparators(columns: Column[]){returnrenderRowSeparators(columns,'┌','┬','┐');}functionrenderFooterSeparators(columns: Column[]){returnrenderRowSeparators(columns,'└','┴','┘');}functionrenderRowSeparators(columns: Column[],leftChar='├',midChar='┼',rightChar='┤',){return(<BoxflexDirection="row"><Text>{leftChar}</Text>
{columns.map((column,index)=>(<React.Fragmentkey={column.key}><Text>{'─'.repeat(column.width)}</Text>
{index<columns.length-1 ? (<Text>{midChar}</Text>) : (<Text>{rightChar}</Text>)}</React.Fragment>))}</Box>
);}exportdefaultTable;
Apparently this repo is no longer compatible with the new version of Ink since it is a class component.
I wrote the following (almost) equivalent react function version:
Usage:
Feel free to use it in your codes. 💫
The text was updated successfully, but these errors were encountered: