Skip to content

Commit d24db0a

Browse files
Copilotjosecelano
andcommitted
refactor: Improve encapsulation with writeln methods
Co-authored-by: josecelano <58816+josecelano@users.noreply.github.com>
1 parent 470d643 commit d24db0a

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

src/presentation/user_output.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,19 @@ impl StdoutWriter {
704704
/// Write a line to stdout
705705
///
706706
/// Writes the given message to the stdout channel.
707+
/// The message should include any necessary newline characters.
707708
/// Errors are silently ignored as output operations are best-effort.
708709
fn write_line(&mut self, message: &str) {
709710
write!(self.0, "{message}").ok();
710711
}
712+
713+
/// Write with a newline to stdout
714+
///
715+
/// Writes the given message followed by a newline to the stdout channel.
716+
/// Errors are silently ignored as output operations are best-effort.
717+
fn writeln(&mut self, message: &str) {
718+
writeln!(self.0, "{message}").ok();
719+
}
711720
}
712721

713722
/// Stderr writer wrapper for type safety
@@ -730,10 +739,19 @@ impl StderrWriter {
730739
/// Write a line to stderr
731740
///
732741
/// Writes the given message to the stderr channel.
742+
/// The message should include any necessary newline characters.
733743
/// Errors are silently ignored as output operations are best-effort.
734744
fn write_line(&mut self, message: &str) {
735745
write!(self.0, "{message}").ok();
736746
}
747+
748+
/// Write with a newline to stderr
749+
///
750+
/// Writes the given message followed by a newline to the stderr channel.
751+
/// Errors are silently ignored as output operations are best-effort.
752+
fn writeln(&mut self, message: &str) {
753+
writeln!(self.0, "{message}").ok();
754+
}
737755
}
738756

739757
// ============================================================================
@@ -1174,7 +1192,7 @@ impl UserOutput {
11741192
/// // Output to stdout: {"status": "destroyed", "environment": "test"}
11751193
/// ```
11761194
pub fn data(&mut self, data: &str) {
1177-
writeln!(self.stdout.0, "{data}").ok();
1195+
self.stdout.writeln(data);
11781196
}
11791197

11801198
/// Display a blank line to stderr (Normal level and above)
@@ -1193,7 +1211,7 @@ impl UserOutput {
11931211
/// ```
11941212
pub fn blank_line(&mut self) {
11951213
if self.verbosity_filter.should_show_blank_lines() {
1196-
writeln!(self.stderr.0).ok();
1214+
self.stderr.writeln("");
11971215
}
11981216
}
11991217

@@ -1248,9 +1266,9 @@ impl UserOutput {
12481266
/// ```
12491267
pub fn info_block(&mut self, title: &str, lines: &[&str]) {
12501268
if self.verbosity_filter.should_show_info_blocks() {
1251-
writeln!(self.stderr.0, "{title}").ok();
1269+
self.stderr.writeln(title);
12521270
for line in lines {
1253-
writeln!(self.stderr.0, "{line}").ok();
1271+
self.stderr.writeln(line);
12541272
}
12551273
}
12561274
}
@@ -1585,6 +1603,30 @@ mod tests {
15851603
assert!(test_output.stderr().contains("Progress message"));
15861604
assert!(test_output.stdout().contains("Result data"));
15871605
}
1606+
1607+
#[test]
1608+
fn stdout_writer_writeln_adds_newline() {
1609+
let buffer = Arc::new(Mutex::new(Vec::new()));
1610+
let writer = Box::new(test_support::TestWriter::new(Arc::clone(&buffer)));
1611+
1612+
let mut stdout = StdoutWriter::new(writer);
1613+
stdout.writeln("Test");
1614+
1615+
let output = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
1616+
assert_eq!(output, "Test\n");
1617+
}
1618+
1619+
#[test]
1620+
fn stderr_writer_writeln_adds_newline() {
1621+
let buffer = Arc::new(Mutex::new(Vec::new()));
1622+
let writer = Box::new(test_support::TestWriter::new(Arc::clone(&buffer)));
1623+
1624+
let mut stderr = StderrWriter::new(writer);
1625+
stderr.writeln("Error");
1626+
1627+
let output = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
1628+
assert_eq!(output, "Error\n");
1629+
}
15881630
}
15891631

15901632
// ============================================================================

0 commit comments

Comments
 (0)