From f29569dcffdf8240a0c294c18652562622f9247a Mon Sep 17 00:00:00 2001 From: Satya Patel <14907857+saddlepaddle@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:26:43 +0000 Subject: [PATCH] fix(cli-framework): take the terminal as an input to table() table() decided on OSC 8 links by reading process.stdout.isTTY and TERM itself, so its tests depended on the runner. A runner with TERM=dumb printed plain URLs and failed the link tests. table() now takes the terminal as an optional last parameter. It defaults to the real stdout and TERM, so callers do not change. The tests state the terminal they test, and keep one dumb-terminal case. The cause was found in #9664. --- packages/cli-framework/src/output.test.ts | 47 +++++++++++------------ packages/cli-framework/src/output.ts | 15 +++++--- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/cli-framework/src/output.test.ts b/packages/cli-framework/src/output.test.ts index 63e18ab1565..9e3ff7b4175 100644 --- a/packages/cli-framework/src/output.test.ts +++ b/packages/cli-framework/src/output.test.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, test } from "bun:test"; +import { describe, expect, test } from "bun:test"; import { formatOutput, table } from "./output"; @@ -12,16 +12,8 @@ const BEL = "\x07"; // Built from constants: a regex literal cannot hold control characters. const OSC8 = new RegExp(`${ESC}\\]8;;[^${BEL}]*${BEL}`, "g"); -const originalIsTTY = process.stdout.isTTY; - -function setTTY(value: boolean | undefined): void { - Object.defineProperty(process.stdout, "isTTY", { - value, - configurable: true, - }); -} - -afterEach(() => setTTY(originalIsTTY)); +const LINKING = { isTTY: true, term: "xterm-256color" }; +const PIPE = { isTTY: false, term: "xterm-256color" }; describe("formatOutput", () => { const raw = { raw: "#!/bin/sh\necho hi\n" }; @@ -57,53 +49,58 @@ describe("formatOutput", () => { describe("table", () => { test("truncates a cell wider than its cap", () => { - setTTY(false); const out = table( [{ title: "Schema history for every published page and version" }], ["title"], undefined, [30], + PIPE, ); expect(out).toContain("Schema history for every publ…"); }); test("prints a URL in full however narrow its column", () => { - setTTY(false); - const out = table(ROWS, COLUMNS, undefined, CAPS); + const out = table(ROWS, COLUMNS, undefined, CAPS, PIPE); expect(out).toContain(URL); expect(out).not.toContain("…"); expect(out).not.toContain("\x1b]8;;"); }); test("links the URL it prints", () => { - setTTY(true); - const out = table(ROWS, COLUMNS, undefined, CAPS); + const out = table(ROWS, COLUMNS, undefined, CAPS, LINKING); expect(out).toContain(`\x1b]8;;${URL}\x07${URL}\x1b]8;;\x07`); }); + test("prints a plain URL on a dumb terminal", () => { + const out = table(ROWS, COLUMNS, undefined, CAPS, { + isTTY: true, + term: "dumb", + }); + expect(out).toContain(URL); + expect(out).not.toContain("\x1b]8;;"); + }); + test("keeps padding outside the link so it stops at the URL", () => { - setTTY(true); const out = table( [{ url: "http://a.dev" }, { url: URL }], ["url"], ["URL"], [50], + LINKING, ); expect(out).toContain("\x07http://a.dev\x1b]8;;\x07"); }); test("columns line up whether or not links are emitted", () => { - const widths = [true, false].map((tty) => { - setTTY(tty); - return table(ROWS, COLUMNS, undefined, CAPS) + const widths = [LINKING, PIPE].map((terminal) => + table(ROWS, COLUMNS, undefined, CAPS, terminal) .split("\n") - .map((line) => line.replace(OSC8, "").length); - }); + .map((line) => line.replace(OSC8, "").length), + ); expect(widths[0]).toEqual(widths[1]!); }); test("lines the next column up behind a URL wider than its cap", () => { - setTTY(false); const lines = table( [ { url: URL, id: "4a436fc7" }, @@ -112,6 +109,7 @@ describe("table", () => { ["url", "id"], ["URL", "ID"], [20, 10], + PIPE, ).split("\n"); const starts = ["ID", "4a436fc7", "860b82f8"].map((cell, i) => lines[i]?.indexOf(cell), @@ -120,8 +118,7 @@ describe("table", () => { }); test("leaves non-URL cells alone", () => { - setTTY(true); - const out = table([{ id: "not a url" }], ["id"], ["ID"], [30]); + const out = table([{ id: "not a url" }], ["id"], ["ID"], [30], LINKING); expect(out).not.toContain("\x1b]8;;"); }); }); diff --git a/packages/cli-framework/src/output.ts b/packages/cli-framework/src/output.ts index e893da815c7..3320a8213ec 100644 --- a/packages/cli-framework/src/output.ts +++ b/packages/cli-framework/src/output.ts @@ -94,16 +94,18 @@ function isPaginatedEnvelope(data: unknown): data is { items: unknown[] } { const URL_CELL = /^https?:\/\/\S+$/; -const supportsHyperlinks = () => - process.stdout.isTTY === true && process.env.TERM !== "dumb"; +interface Terminal { + isTTY: boolean | undefined; + term: string | undefined; +} /** * OSC 8 hyperlink, for terminals that honour it. A terminal that does not * falls back to detecting links in the text it can see, which is why a URL * cell is never truncated: the visible text has to stand on its own. */ -function hyperlink(url: string, label: string): string { - if (!supportsHyperlinks()) return label; +function hyperlink(url: string, label: string, terminal: Terminal): string { + if (terminal.isTTY !== true || terminal.term === "dumb") return label; return `\x1b]8;;${url}\x07${label}\x1b]8;;\x07`; } @@ -120,6 +122,7 @@ export function table( columns: string[], headers?: string[], maxColWidth: number | (number | undefined)[] = 60, + terminal: Terminal = { isTTY: process.stdout.isTTY, term: process.env.TERM }, ): string { if (data.length === 0) return "No results."; @@ -149,7 +152,9 @@ export function table( r .map((cell, i) => { const padding = " ".repeat(Math.max(0, widths[i]! - cell.text.length)); - const body = cell.link ? hyperlink(cell.link, cell.text) : cell.text; + const body = cell.link + ? hyperlink(cell.link, cell.text, terminal) + : cell.text; return body + padding; }) .join(" "),