(data: JSONValue, wide: boolean)
| 35 | } |
| 36 | |
| 37 | function formatTable(data: JSONValue, wide: boolean): string { |
| 38 | if (!data) { |
| 39 | return "No data returned."; |
| 40 | } |
| 41 | |
| 42 | // Handle single object |
| 43 | if (!Array.isArray(data)) { |
| 44 | return formatSingleObject(data as JSONObject); |
| 45 | } |
| 46 | |
| 47 | const items: JSONArray = data as JSONArray; |
| 48 | if (items.length === 0) { |
| 49 | return "No results found."; |
| 50 | } |
| 51 | |
| 52 | // Get all keys from the first item |
| 53 | const firstItem: JSONObject = items[0] as JSONObject; |
| 54 | if (!firstItem || typeof firstItem !== "object") { |
| 55 | return formatJson(data); |
| 56 | } |
| 57 | |
| 58 | let columns: string[] = Object.keys(firstItem); |
| 59 | |
| 60 | // In non-wide mode, limit columns to keep the table readable |
| 61 | if (!wide && columns.length > 6) { |
| 62 | // Prioritize common fields |
| 63 | const priority: string[] = [ |
| 64 | "_id", |
| 65 | "name", |
| 66 | "title", |
| 67 | "status", |
| 68 | "createdAt", |
| 69 | "updatedAt", |
| 70 | ]; |
| 71 | const prioritized: string[] = priority.filter((col: string) => { |
| 72 | return columns.includes(col); |
| 73 | }); |
| 74 | const remaining: string[] = columns.filter((col: string) => { |
| 75 | return !priority.includes(col); |
| 76 | }); |
| 77 | columns = [...prioritized, ...remaining].slice(0, 6); |
| 78 | } |
| 79 | |
| 80 | const useColor: boolean = !isColorDisabled(); |
| 81 | |
| 82 | const table: Table.Table = new Table({ |
| 83 | head: columns.map((col: string) => { |
| 84 | return useColor ? chalk.cyan(col) : col; |
| 85 | }), |
| 86 | style: { |
| 87 | head: [], |
| 88 | border: [], |
| 89 | }, |
| 90 | wordWrap: true, |
| 91 | }); |
| 92 | |
| 93 | for (const item of items) { |
| 94 | const row: string[] = columns.map((col: string) => { |
no test coverage detected