Skip to content

Conversation

@orien
Copy link

@orien orien commented Jan 16, 2026

Context

The diff formatting library expects streams to provide an optional columns property (via the FormatStream interface) to enable terminal width-aware table formatting. StringWriteStream currently lacks this property, causing the diff formatter to format tables without width constraints, leading to overflow and poor rendering in different terminal environments.

Changes

  • Added columns getter to StringWriteStream that dynamically reads terminal width from process.stdout.columns
  • Getter returns current terminal width on every access, allowing it to reflect runtime changes such as terminal resizing
  • Returns undefined in non-TTY environments (CI/CD pipelines, redirected output)
  • The formatter will automatically use this property when calling formatTable() for IAM changes, security group changes, and other table-formatted output
  • Added comprehensive unit tests

Consequences

The Formatter class will automatically use this property when calling formatTable() for IAM changes, security group changes, and other table-formatted output

Considerations

The columns getter reads from process.stdout.columns on every access, ensuring it always reflects the current terminal width. This design allows the stream to respond to terminal resize events without requiring additional event listeners or manual updates. The diff formatter will automatically pick up this property through the existing FormatStream interface.

Now, there's no guarantee that people will output the text collected in the StringWriteStream to stdout, but that is my use case. 🤷


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added the p2 label Jan 16, 2026
@aws-cdk-automation aws-cdk-automation requested a review from a team January 16, 2026 14:50
auto-merge was automatically disabled January 16, 2026 14:55

Head branch was pushed to by a user without write access

@orien orien force-pushed the toolkit-lib-terminal-width branch from d507780 to 69ac99b Compare January 16, 2026 14:55
@orien orien changed the title Add terminal width support to StringWriteStream feat: add terminal width support to StringWriteStream Jan 16, 2026
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Does this mean we need to implement a specific interface that advertises columns? Who gets to take advantage of this?

constructor() {
super();
// Read terminal width from stdout to enable proper table formatting
this.columns = process.stdout.columns;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be a getter, because the column count can change over time.

@orien orien force-pushed the toolkit-lib-terminal-width branch from 69ac99b to f59f646 Compare January 16, 2026 15:02
@orien
Copy link
Author

orien commented Jan 16, 2026

Thanks. Does this mean we need to implement a specific interface that advertises columns? Who gets to take advantage of this?

@rix0rrr Thanks for the quick review!

I've added some more information to the Context and Consequences sections in the PR description.

@orien orien force-pushed the toolkit-lib-terminal-width branch from f59f646 to b49b40c Compare January 16, 2026 15:16
Adds terminal width detection to StringWriteStream to enable proper table
formatting that respects terminal width constraints and prevents overflow.

The columns getter dynamically reads from process.stdout.columns, allowing
it to reflect runtime changes such as terminal resizing. Returns undefined
in non-TTY environments (e.g., CI/CD pipelines).

Includes comprehensive unit tests with 100% coverage covering:
- Core stream functionality (writing, buffering, conversion)
- Terminal width initialization and handling
- TTY and non-TTY environment support
- Multiple terminal width scenarios
- Dynamic terminal resize handling
@orien orien force-pushed the toolkit-lib-terminal-width branch from b49b40c to b343464 Compare January 16, 2026 15:18
@mrgrain mrgrain deployed to integ-approval January 20, 2026 10:31 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants