Skip to content

Commit 02406f1

Browse files
committed
refactor: table library, unify calls (#2525)
Synced from monorepo@15018a01e31f1ccc2e396783821f1dc09e6994ef
1 parent db732f6 commit 02406f1

8 files changed

Lines changed: 81 additions & 29 deletions

File tree

.sync-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
785e18f56f0be8a283e43249f728cdaec27f3402
1+
15018a01e31f1ccc2e396783821f1dc09e6994ef

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
"zod-config": "1.4.0",
3939
"@xata.io/ai": "0.1.0",
4040
"@xata.io/api": "0.1.0",
41+
"@xata.io/lang": "0.0.1",
4142
"@xata.io/config": "0.0.0",
4243
"@xata.io/pgroll": "0.9.0",
4344
"@xata.io/pgstream": "0.2.0",
4445
"@xata.io/sql": "0.1.3",
4546
"@xata.io/test-utils": "0.0.0",
4647
"@xata.io/tsconfig": "0.0.1",
47-
"@xata.io/utils": "0.1.0",
48-
"@xata.io/lang": "0.0.1"
48+
"@xata.io/utils": "0.1.0"
4949
},
5050
"scripts": {
5151
"dev": "bun src/bin/cli.ts",

src/commands/ai/terminal-ui.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import chalk from 'chalk';
22
import { stdin, stdout } from 'node:process';
33
import stripAnsi from 'strip-ansi';
4-
import Table from 'cli-table3';
5-
import { borderlessTableOptions } from '~/lib/table';
4+
import { createTable } from '~/lib/table';
65

76
interface TerminalUIProps {
87
schema: any[];
@@ -154,11 +153,9 @@ export class TerminalUI {
154153
const firstResult = results[0];
155154
const headers = Object.keys(firstResult);
156155

157-
const table = new Table({
158-
...borderlessTableOptions,
156+
const table = createTable({
159157
head: headers.map((h) => chalk.cyan(h)),
160158
style: {
161-
...borderlessTableOptions.style,
162159
head: ['cyan'],
163160
border: ['grey']
164161
}

src/commands/branch/metrics.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
type BranchMetricsReport
1818
} from '@xata.io/utils';
1919
import chalk from 'chalk';
20-
import Table from 'cli-table3';
2120
import type { LocalContext } from '~/context';
2221
import { getErrorMessage } from '~/lib/cli-utils';
22+
import { renderTable } from '~/lib/table';
2323

2424
type OutputFormat = 'table' | 'json' | 'ndjson' | 'tui';
2525

@@ -229,14 +229,12 @@ async function watchMetrics(
229229
}
230230

231231
function renderSummaryTable(report: BranchMetricsReport, aggregation: BranchMetricAggregation): string {
232-
const table = new Table({
233-
head: ['Metric', 'Instance', 'Agg', 'Latest', 'Min', 'Max', 'Avg', 'Unit', 'Status']
234-
});
232+
const rows: string[][] = [];
235233

236234
for (const metric of report.metrics) {
237235
const series = metric.series.filter((s) => s.aggregation === aggregation);
238236
if (series.length === 0) {
239-
table.push([
237+
rows.push([
240238
metric.key,
241239
'',
242240
aggregation,
@@ -251,7 +249,7 @@ function renderSummaryTable(report: BranchMetricsReport, aggregation: BranchMetr
251249
}
252250

253251
for (const serie of series) {
254-
table.push([
252+
rows.push([
255253
metric.key,
256254
serie.instanceName,
257255
serie.aggregation,
@@ -265,9 +263,11 @@ function renderSummaryTable(report: BranchMetricsReport, aggregation: BranchMetr
265263
}
266264
}
267265

266+
const table = renderTable(['Metric', 'Instance', 'Agg', 'Latest', 'Min', 'Max', 'Avg', 'Unit', 'Status'], rows);
267+
268268
return `${chalk.bold(`Metrics for ${report.target.branchName} (${report.target.branchId})`)}\n${chalk.dim(
269269
`${report.timeRange.start}${report.timeRange.end}`
270-
)}\n${table.toString()}\n`;
270+
)}\n${table}\n`;
271271
}
272272

273273
function renderDashboard(report: BranchMetricsReport, aggregation: BranchMetricAggregation): string {

src/commands/branch/metrics.unit.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ describe('branch metrics command', () => {
105105
});
106106
});
107107

108+
test('prints branch metrics as a compact borderless table', async () => {
109+
const { context, stdout, branchMetrics } = buildContext();
110+
111+
await implementation.call(context, { ...baseFlags, output: 'table' });
112+
113+
expect(branchMetrics).toHaveBeenCalledTimes(1);
114+
const output = stdout.join('');
115+
expect(output).toContain('Metrics for main (branch)');
116+
expect(output).toContain('Metric');
117+
expect(output).toContain('Primary');
118+
});
119+
108120
test('shows the expected timestamp format for invalid start and end times', async () => {
109121
const { context: startContext } = buildContext();
110122
await expect(implementation.call(startContext, { ...baseFlags, start: '10' })).rejects.toThrow(

src/lib/cli-utils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from 'chalk';
2-
import Table from 'cli-table3';
32
import invariant from 'tiny-invariant';
43
import { decodeJwt } from 'jose';
54
import type { LocalContext } from '~/context';
@@ -9,7 +8,7 @@ import { config } from './config';
98
import { DEFAULT_DATABASE_NAME } from './constants';
109
import { getProfile } from './profile';
1110
import { projectConfig } from './project-config';
12-
import { borderlessTableOptions } from './table';
11+
import { renderTable } from './table';
1312

1413
export const print = (
1514
context: LocalContext,
@@ -23,15 +22,9 @@ export const print = (
2322
return JSON.stringify(data, null, 2);
2423
}
2524

26-
const table = new Table({
27-
...borderlessTableOptions,
28-
head: headers
29-
});
30-
rows.forEach((row) => {
31-
table.push(row);
32-
});
33-
context.process.stdout.write(`${table.toString()}\n`);
34-
return table.toString();
25+
const table = renderTable(headers, rows);
26+
context.process.stdout.write(`${table}\n`);
27+
return table;
3528
};
3629

3730
type BaseOptions = {

src/lib/cli-utils.unit.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'bun:test';
22
import stripAnsi from 'strip-ansi';
33
import { getErrorMessage, groupAndSortRegions, print } from './cli-utils';
4+
import { renderTable } from './table';
45

56
type Region = {
67
id: string;
@@ -209,7 +210,24 @@ describe('print', () => {
209210
);
210211

211212
expect(writes).toEqual([`${result}\n`]);
212-
expect(stripAnsi(result)).not.toMatch(/[]/);
213+
expect(normalizeTableOutput(result)).toEqual([
214+
['name', 'status'],
215+
['alpha', 'ready'],
216+
['beta', 'paused']
217+
]);
218+
});
219+
});
220+
221+
describe('renderTable', () => {
222+
it('should render compact borderless tables', () => {
223+
const result = renderTable(
224+
['name', 'status'],
225+
[
226+
['alpha', 'ready'],
227+
['beta', 'paused']
228+
]
229+
);
230+
213231
expect(normalizeTableOutput(result)).toEqual([
214232
['name', 'status'],
215233
['alpha', 'ready'],

src/lib/table.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const borderlessTableOptions = {
1+
import Table from 'cli-table3';
2+
3+
export type TableOptions = NonNullable<ConstructorParameters<typeof Table>[0]>;
4+
5+
const defaultTableOptions = {
26
chars: {
37
top: '',
48
'top-mid': '',
@@ -20,4 +24,32 @@ export const borderlessTableOptions = {
2024
'padding-left': 0,
2125
'padding-right': 0
2226
}
23-
};
27+
} satisfies TableOptions;
28+
29+
export function createTable(options: TableOptions = {}) {
30+
return new Table({
31+
...defaultTableOptions,
32+
...options,
33+
chars: {
34+
...defaultTableOptions.chars,
35+
...options.chars
36+
},
37+
style: {
38+
...defaultTableOptions.style,
39+
...options.style
40+
}
41+
});
42+
}
43+
44+
export function renderTable(headers: string[], rows: string[][], options: TableOptions = {}) {
45+
const table = createTable({
46+
...options,
47+
head: headers
48+
});
49+
50+
rows.forEach((row) => {
51+
table.push(row);
52+
});
53+
54+
return table.toString();
55+
}

0 commit comments

Comments
 (0)