diff --git a/src/api-client.ts b/src/api-client.ts index 1b98b67..833590e 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -17,6 +17,7 @@ export type GlobalFlags = AuthOverrides & { dryRun: boolean; verbose: boolean; pretty: boolean; + prettyExplicit?: boolean; agentMode?: boolean; }; diff --git a/src/cli.ts b/src/cli.ts index 4eb3f0f..678524b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -73,6 +73,7 @@ function buildProgram(): Command { apiBase: opts.apiBase, dryRun: Boolean(opts.dryRun), pretty: opts.pretty !== false, + prettyExplicit: program.getOptionValueSource("pretty") === "cli", verbose: Boolean(opts.verbose), agentMode: opts.agentMode, clientId: opts.clientId, diff --git a/src/output.test.ts b/src/output.test.ts index 64b1f95..554cccf 100644 --- a/src/output.test.ts +++ b/src/output.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; -import { printResponse } from "./output.js"; +import { printResponse, shouldUseAgentOutput } from "./output.js"; describe("printResponse", () => { it("unwraps results.data into data/nextCursor", () => { @@ -26,3 +26,35 @@ describe("printResponse", () => { }); }); }); + +describe("shouldUseAgentOutput", () => { + it("honors explicit --pretty in an auto-detected agent environment", () => { + assert.equal( + shouldUseAgentOutput( + { pretty: true, prettyExplicit: true }, + true, + ), + false, + ); + }); + + it("honors explicit --no-pretty in an auto-detected agent environment", () => { + assert.equal( + shouldUseAgentOutput( + { pretty: false, prettyExplicit: true }, + true, + ), + false, + ); + }); + + it("keeps explicit agent mode precedence over explicit pretty output", () => { + assert.equal( + shouldUseAgentOutput( + { pretty: true, prettyExplicit: true, agentMode: true }, + false, + ), + true, + ); + }); +}); diff --git a/src/output.ts b/src/output.ts index 9f77f2b..8779fdd 100644 --- a/src/output.ts +++ b/src/output.ts @@ -3,9 +3,23 @@ import { agentModeEnabled } from "./agent-mode.js"; export type OutputOptions = { pretty: boolean; + prettyExplicit?: boolean; agentMode?: boolean; }; +export function shouldUseAgentOutput( + options: OutputOptions, + agentEnvironmentDetected?: boolean, +): boolean { + if (options.agentMode !== undefined) { + return options.agentMode; + } + return ( + !options.prettyExplicit && + (agentEnvironmentDetected ?? agentModeEnabled()) + ); +} + function unwrapResultsData(value: unknown): unknown { if (!value || typeof value !== "object" || Array.isArray(value)) { return value; @@ -51,7 +65,7 @@ export function printResponse( const normalized = unwrapResultsData(value); - if (agentModeEnabled(options.agentMode)) { + if (shouldUseAgentOutput(options)) { write(`${encode(normalized)}\n`); return; }