Skip to content

Commit 41e7fe3

Browse files
committed
Decouple the logic for terminal coloring from PrettyFormatter.
PrettyFormatter doesn't need to check the OutputLocation enum value anymore. This improves the separation of concerns.
1 parent 2c2d594 commit 41e7fe3

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

library/test/src/console.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ impl<T: Write> Write for OutputLocation<T> {
4141
}
4242
}
4343

44+
impl<T: Write> OutputLocation<T> {
45+
pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
46+
match self {
47+
OutputLocation::Pretty(ref mut term) => {
48+
term.fg(color)?;
49+
term.write_all(word.as_bytes())?;
50+
term.reset()?;
51+
}
52+
OutputLocation::Raw(ref mut stdout) => {
53+
stdout.write_all(word.as_bytes())?;
54+
}
55+
}
56+
57+
self.flush()
58+
}
59+
60+
pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
61+
let s = s.as_ref();
62+
self.write_all(s.as_bytes())?;
63+
self.flush()
64+
}
65+
}
66+
4467
pub struct ConsoleTestDiscoveryState {
4568
pub log_out: Option<File>,
4669
pub tests: usize,

library/test/src/formatters/pretty.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,12 @@ impl<T: Write> PrettyFormatter<T> {
6969
self.write_pretty(result, color)
7070
}
7171

72-
pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
73-
match self.out {
74-
OutputLocation::Pretty(ref mut term) => {
75-
if self.use_color {
76-
term.fg(color)?;
77-
}
78-
term.write_all(word.as_bytes())?;
79-
if self.use_color {
80-
term.reset()?;
81-
}
82-
term.flush()
83-
}
84-
OutputLocation::Raw(ref mut stdout) => {
85-
stdout.write_all(word.as_bytes())?;
86-
stdout.flush()
87-
}
88-
}
72+
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
73+
if self.use_color { self.out.write_pretty(word, color) } else { self.out.write_plain(word) }
8974
}
9075

91-
pub fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
92-
let s = s.as_ref();
93-
self.out.write_all(s.as_bytes())?;
94-
self.out.flush()
76+
fn write_plain<S: AsRef<str>>(&mut self, s: S) -> io::Result<()> {
77+
self.out.write_plain(s)
9578
}
9679

9780
fn write_time(

0 commit comments

Comments
 (0)