Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type GlobalFlags = AuthOverrides & {
dryRun: boolean;
verbose: boolean;
pretty: boolean;
prettyExplicit?: boolean;
agentMode?: boolean;
};

Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion src/output.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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,
);
});
});
16 changes: 15 additions & 1 deletion src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +65,7 @@ export function printResponse(

const normalized = unwrapResultsData(value);

if (agentModeEnabled(options.agentMode)) {
if (shouldUseAgentOutput(options)) {
write(`${encode(normalized)}\n`);
return;
}
Expand Down