Skip to content

Use terminal-size as fallback for piped processes#854

Merged
sindresorhus merged 1 commit into
vadimdemedes:masterfrom
costajohnt:fix/terminal-size-piped
Jan 21, 2026
Merged

Use terminal-size as fallback for piped processes#854
sindresorhus merged 1 commit into
vadimdemedes:masterfrom
costajohnt:fix/terminal-size-piped

Conversation

@costajohnt

@costajohnt costajohnt commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Uses terminal-size package to detect actual terminal width when stdout.columns is unavailable (piped processes)
  • Falls back to 80 columns only when both methods fail
  • Ensures consistent terminal width detection across all code paths

Problem

When Ink's output is piped (e.g., node app.js | cat), stdout.columns returns 0 or undefined, causing layout to default to 80 columns even when the terminal is wider.

Solution

The terminal-size package can detect terminal dimensions even when stdout is piped, by checking stdin/stderr or using platform-specific methods.

Changes made:

  1. Added terminal-size dependency - Zero-dependency package by @sindresorhus
  2. Modified getTerminalWidth() method - Uses terminal-size as fallback:
    getTerminalWidth = () => {
        if (this.options.stdout.columns) {
            return this.options.stdout.columns;
        }
        const size = terminalSize();
        return size?.columns ?? 80;
    };
  3. Updated screen reader code path - Changed direct stdout.columns access to use getTerminalWidth() for consistency

Test plan

  • TypeScript compilation passes
  • Linting passes
  • All stdout.columns usages now go through getTerminalWidth()
  • Manual test: Verified piped output (npm run example test.tsx | cat) detects actual terminal width instead of defaulting to 80

Fixes #670

When stdout is piped (e.g., `node app.js | cat`), `stdout.columns` is
0 or undefined. This change uses the terminal-size package to detect
the actual terminal width as a fallback before defaulting to 80 columns.

Changes:
- Add terminal-size package as dependency
- Modify getTerminalWidth() to use terminal-size when stdout.columns
  is unavailable
- Update screen reader code path to use getTerminalWidth() for
  consistency

Fixes #670
@sindresorhus sindresorhus merged commit 6dc3b03 into vadimdemedes:master Jan 21, 2026
1 check passed
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.

Terminal size when piped

2 participants