Skip to content

feat: auto-downgrade output to YAML in non-TTY#737

Merged
jackwener merged 4 commits intomainfrom
feat/tty-output-detection
Apr 3, 2026
Merged

feat: auto-downgrade output to YAML in non-TTY#737
jackwener merged 4 commits intomainfrom
feat/tty-output-detection

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • Non-TTY stdout (pipes, AI agents, subprocesses) now auto-outputs YAML instead of table with ANSI colors + box-drawing
  • OUTPUT env var (yaml/json/table/etc) overrides auto-detection
  • Explicit -f flag always takes precedence
  • Mirrors twitter-cli's output.py TTY detection pattern

Closes the TTY detection gap identified in #98.

Test plan

  • npx vitest run src/output.test.ts — 4 tests covering TTY/non-TTY/env-var/explicit-flag
  • Manual: opencli zhihu question 123 | cat should output YAML
  • Manual: OUTPUT=json opencli zhihu question 123 | cat should output JSON

When stdout is not a TTY (pipes, AI agents, subprocesses), automatically
output YAML instead of table with ANSI colors and box-drawing characters.
This makes opencli output parseable by downstream tools and AI agents.

Behavior:
- TTY: table (default, unchanged)
- Non-TTY: yaml (auto-detected)
- OUTPUT env var: overrides auto-detection (yaml/json/table/etc)
- Explicit -f flag: always respected
- fmt='table' from commanderAdapter now correctly triggers non-TTY downgrade
- Priority: explicit -f (non-table) > OUTPUT env var > TTY auto-detect
- Added test for explicit -f precedence over OUTPUT env var
Use Commander's getOptionValueSource to distinguish explicit -f from
default. Explicit -f table in non-TTY keeps table output. Only auto-
downgrade when user didn't pass -f.

Priority: explicit -f > OUTPUT env var > TTY auto-detect > table default
When user passes -f explicitly, command-level defaultFormat (e.g.
gemini/ask defaultFormat:'plain') no longer overrides their choice.
@jackwener jackwener merged commit c161f0f into main Apr 3, 2026
11 checks passed
@jackwener jackwener deleted the feat/tty-output-detection branch April 3, 2026 14:26
@Astro-Han
Copy link
Copy Markdown
Contributor

isTTY 判断输出环境而非调用者身份,太巧妙了。比检测环境变量或 Agent 身份更通用、零配置,学到了。

just-buer pushed a commit to just-buer/opencli that referenced this pull request Apr 8, 2026
* feat: auto-downgrade table output to YAML in non-TTY environments

When stdout is not a TTY (pipes, AI agents, subprocesses), automatically
output YAML instead of table with ANSI colors and box-drawing characters.
This makes opencli output parseable by downstream tools and AI agents.

Behavior:
- TTY: table (default, unchanged)
- Non-TTY: yaml (auto-detected)
- OUTPUT env var: overrides auto-detection (yaml/json/table/etc)
- Explicit -f flag: always respected

* fix: TTY detection now works with commanderAdapter default fmt

- fmt='table' from commanderAdapter now correctly triggers non-TTY downgrade
- Priority: explicit -f (non-table) > OUTPUT env var > TTY auto-detect
- Added test for explicit -f precedence over OUTPUT env var

* fix: explicit -f flag now takes precedence over TTY auto-detection

Use Commander's getOptionValueSource to distinguish explicit -f from
default. Explicit -f table in non-TTY keeps table output. Only auto-
downgrade when user didn't pass -f.

Priority: explicit -f > OUTPUT env var > TTY auto-detect > table default

* fix: explicit -f also skips command defaultFormat override

When user passes -f explicitly, command-level defaultFormat (e.g.
gemini/ask defaultFormat:'plain') no longer overrides their choice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants