Skip to content

Add flush() method to UserOutput for explicit buffering control - #141

Merged
josecelano merged 2 commits into
mainfrom
copilot/add-buffering-control-to-user-output
Nov 5, 2025
Merged

josecelano merged 2 commits into
mainfrom
copilot/add-buffering-control-to-user-output

Conversation

Copilot AI commented Nov 4, 2025 •

Copy link
Copy Markdown
Contributor

Adds explicit buffering control to UserOutput following Rust's Write trait pattern. While output is line-buffered by default, explicit flush ensures immediate visibility before long-running operations and improves test reliability.

Changes

Core API

  • Added flush() method that flushes both stdout and stderr writers
  • Returns std::io::Result<()> for error propagation
  • Safe to call multiple times (idempotent)

Documentation

  • Module-level "Buffering Behavior" section explaining when to use flush()
  • Rustdoc examples demonstrating usage patterns

Tests

  • 5 tests covering success scenarios, idempotency, empty buffers, dual-channel flushing, and sequential patterns
  • All existing tests pass (101 total in module)

Usage

let mut output = UserOutput::new(VerbosityLevel::Normal);
output.progress("Starting long operation...");
output.flush()?; // Ensure message appears immediately
perform_long_operation();

Implementation

Directly flushes the typed writer wrappers (StdoutWriter and StderrWriter) maintaining type safety through the newtype pattern:

pub fn flush(&mut self) -> std::io::Result<()> {
    self.stdout.0.flush()?;
    self.stderr.0.flush()?;
    Ok(())
}

Backward compatible - purely additive change.

Original prompt

This section details on the original issue you should resolve

<issue_title>Proposal 7: Add Buffering Control</issue_title>
<issue_description>## Overview

Add explicit buffering control to the UserOutput module by providing a flush() method. While writes are typically line-buffered by default, explicit flush control ensures output appears immediately when needed and provides better testing capabilities.

Specification

See detailed specification: docs/issues/138-add-buffering-control.md

Goals

  • Add flush() method to UserOutput for explicit buffer control
  • Document buffering behavior in module documentation
  • Add tests demonstrating flush behavior
  • Maintain backward compatibility with existing API
  • Follow Rust standard patterns from Write trait

Implementation Plan

Phase 1: Core Implementation (30 minutes)

  • Add flush() method to UserOutput
  • Implement flush for both stdout and stderr writers
  • Add rustdoc documentation for the method

Phase 2: Documentation (20 minutes)

  • Update module-level documentation with buffering behavior section
  • Add usage examples showing when to use flush()

Phase 3: Testing (30 minutes)

  • Add unit tests for successful flush
  • Add tests for multiple flush calls
  • Add tests for empty buffer flushing

Phase 4: Quality Assurance (20 minutes)

  • Run ./scripts/pre-commit.sh and fix any issues
  • Verify all linters pass

Total Estimated Time: 2 hours

Acceptance Criteria

Functional Requirements

  • UserOutput::flush() method flushes both stdout and stderr
  • Flush can be called multiple times safely
  • Flush returns std::io::Result<()> for error handling
  • Existing functionality is not affected

Testing Requirements

  • Unit tests cover successful flush scenarios
  • Tests verify idempotency (multiple flushes are safe)
  • All existing tests continue to pass

Quality Requirements

  • Pre-commit checks pass: ./scripts/pre-commit.sh
  • Code follows project conventions

Related

Labels

enhancement, phase-2, user-output, P2

Priority

P2 (Phase 2 - Polish & Extensions)

Estimated Effort

2 hours
</issue_description>

Comments on the Issue (you are @copilot in this section)

@josecelano **Parent Epic**: #102 - User Output Architecture Improvements

This issue is part of Phase 2: Polish & Extensions of the User Output Architecture refactoring.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Add flush() method to UserOutput that flushes both stdout and stderr
- Update module-level documentation with buffering behavior section
- Add comprehensive unit tests for flush functionality:
  - Test successful flush
  - Test multiple flush calls (idempotency)
  - Test empty buffer flushing
  - Test flushing both channels
  - Test sequential flush patterns
- Follow Rust's Write trait pattern for error handling
- Maintain backward compatibility with existing API
- All 101 tests pass (96 original + 5 new buffering tests)

Co-authored-by: josecelano <[email protected]>
Copilot AI changed the title [WIP] Add flush method for explicit buffering control in UserOutput Add flush() method to UserOutput for explicit buffering control Nov 4, 2025
Copilot AI requested a review from josecelano November 4, 2025 22:23
@josecelano
josecelano marked this pull request as ready for review November 5, 2025 07:55

@josecelano josecelano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK b3f98c7

@josecelano
josecelano merged commit 7486b25 into main Nov 5, 2025
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.

Proposal 7: Add Buffering Control

2 participants