Skip to content

Commit 87008fd

Browse files
committed
fix(fmt): Don't panic on broken pipes without termcolor
Fixes #221
1 parent 6d55509 commit 87008fd

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/fmt/writer/buffer/plain.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ use crate::fmt::{WritableTarget, WriteStyle};
44

55
pub(in crate::fmt::writer) struct BufferWriter {
66
target: WritableTarget,
7+
is_test: bool,
78
}
89

910
impl BufferWriter {
10-
pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
11+
pub(in crate::fmt::writer) fn stderr(is_test: bool, _write_style: WriteStyle) -> Self {
1112
BufferWriter {
1213
target: WritableTarget::Stderr,
14+
is_test,
1315
}
1416
}
1517

16-
pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
18+
pub(in crate::fmt::writer) fn stdout(is_test: bool, _write_style: WriteStyle) -> Self {
1719
BufferWriter {
1820
target: WritableTarget::Stdout,
21+
is_test,
1922
}
2023
}
2124

2225
pub(in crate::fmt::writer) fn pipe(pipe: Box<Mutex<dyn io::Write + Send + 'static>>) -> Self {
2326
BufferWriter {
2427
target: WritableTarget::Pipe(pipe),
28+
is_test: false,
2529
}
2630
}
2731

@@ -34,14 +38,22 @@ impl BufferWriter {
3438
}
3539

3640
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
41+
use std::io::Write as _;
42+
3743
// This impl uses the `eprint` and `print` macros
3844
// instead of using the streams directly.
3945
// This is so their output can be captured by `cargo test`.
40-
match &self.target {
46+
match (&self.target, self.is_test) {
4147
// Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty.
42-
WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?,
43-
WritableTarget::Stdout => print!("{}", String::from_utf8_lossy(&buf.0)),
44-
WritableTarget::Stderr => eprint!("{}", String::from_utf8_lossy(&buf.0)),
48+
(WritableTarget::Pipe(pipe), _) => pipe.lock().unwrap().write_all(&buf.0)?,
49+
(WritableTarget::Stdout, true) => print!("{}", String::from_utf8_lossy(&buf.0)),
50+
(WritableTarget::Stdout, false) => {
51+
write!(std::io::stdout(), "{}", String::from_utf8_lossy(&buf.0))?
52+
}
53+
(WritableTarget::Stderr, true) => eprint!("{}", String::from_utf8_lossy(&buf.0)),
54+
(WritableTarget::Stderr, false) => {
55+
write!(std::io::stderr(), "{}", String::from_utf8_lossy(&buf.0))?
56+
}
4557
}
4658

4759
Ok(())

0 commit comments

Comments
 (0)