Skip to content

Commit 663c071

Browse files
committed
fix: make table respect tty pipe and NO_COLOR (#2763)
Synced from monorepo@eec85cd75de8cef02f0107c664294b9e471e4384
1 parent 5003df7 commit 663c071

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

.sync-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
61e71be7ecbb8a8968cae4f7cd02f61b1410864c
1+
eec85cd75de8cef02f0107c664294b9e471e4384

src/commands/ai/terminal-ui.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ export class TerminalUI {
154154
const headers = Object.keys(firstResult);
155155

156156
const table = createTable({
157-
head: headers.map((h) => chalk.cyan(h)),
158-
style: {
159-
head: ['cyan'],
160-
border: ['grey']
161-
}
157+
head: headers.map((h) => chalk.cyan.bold(h))
162158
});
163159

164160
const displayRows = results.slice(0, 20);

src/lib/table.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import chalk from 'chalk';
12
import Table from 'cli-table3';
23

34
export type TableOptions = NonNullable<ConstructorParameters<typeof Table>[0]>;
@@ -22,7 +23,14 @@ const defaultTableOptions = {
2223
},
2324
style: {
2425
'padding-left': 0,
25-
'padding-right': 0
26+
'padding-right': 0,
27+
// Disable cli-table3's default ANSI colors (red head, grey border): they
28+
// ignore NO_COLOR and wrap padding and the column separator in escape
29+
// codes, which breaks piping output to tools like awk. Headers and cell
30+
// content are colored with chalk instead, which only emits colors on an
31+
// interactive TTY (and respects NO_COLOR / FORCE_COLOR).
32+
head: [],
33+
border: []
2634
}
2735
} satisfies TableOptions;
2836

@@ -44,7 +52,7 @@ export function createTable(options: TableOptions = {}) {
4452
export function renderTable(headers: string[], rows: string[][], options: TableOptions = {}) {
4553
const table = createTable({
4654
...options,
47-
head: headers
55+
head: headers.map((header) => chalk.red.bold(header))
4856
});
4957

5058
rows.forEach((row) => {

0 commit comments

Comments
 (0)