Skip to content

Commit

Permalink
refactor(fmt): Track WriteStyle with the BufferWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 17, 2024
1 parent f4a0c2c commit 6d55509
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/fmt/writer/buffer/plain.rs
Expand Up @@ -19,15 +19,16 @@ impl BufferWriter {
}
}

pub(in crate::fmt::writer) fn pipe(
_write_style: WriteStyle,
pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
) -> Self {
pub(in crate::fmt::writer) fn pipe(pipe: Box<Mutex<dyn io::Write + Send + 'static>>) -> Self {
BufferWriter {
target: WritableTarget::Pipe(pipe),
}
}

pub(in crate::fmt::writer) fn write_style(&self) -> WriteStyle {
WriteStyle::Never
}

pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer(Vec::new())
}
Expand Down
14 changes: 10 additions & 4 deletions src/fmt/writer/buffer/termcolor.rs
Expand Up @@ -8,6 +8,7 @@ use crate::fmt::{WritableTarget, WriteStyle};
pub(in crate::fmt::writer) struct BufferWriter {
inner: termcolor::BufferWriter,
uncolored_target: Option<WritableTarget>,
write_style: WriteStyle,
}

impl BufferWriter {
Expand All @@ -19,6 +20,7 @@ impl BufferWriter {
} else {
None
},
write_style,
}
}

Expand All @@ -30,20 +32,24 @@ impl BufferWriter {
} else {
None
},
write_style,
}
}

pub(in crate::fmt::writer) fn pipe(
write_style: WriteStyle,
pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
) -> Self {
pub(in crate::fmt::writer) fn pipe(pipe: Box<Mutex<dyn io::Write + Send + 'static>>) -> Self {
let write_style = WriteStyle::Never;
BufferWriter {
// The inner Buffer is never printed from, but it is still needed to handle coloring and other formatting
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
uncolored_target: Some(WritableTarget::Pipe(pipe)),
write_style,
}
}

pub(in crate::fmt::writer) fn write_style(&self) -> WriteStyle {
self.write_style
}

pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer {
inner: self.inner.buffer(),
Expand Down
15 changes: 8 additions & 7 deletions src/fmt/writer/mod.rs
Expand Up @@ -98,12 +98,11 @@ impl WriteStyle {
/// A terminal target with color awareness.
pub(crate) struct Writer {
inner: BufferWriter,
write_style: WriteStyle,
}

impl Writer {
pub fn write_style(&self) -> WriteStyle {
self.write_style
self.inner.write_style()
}

pub(super) fn buffer(&self) -> Buffer {
Expand Down Expand Up @@ -190,17 +189,19 @@ impl Builder {
}
color_choice => color_choice,
};
let color_choice = if self.is_test {
WriteStyle::Never
} else {
color_choice
};

let writer = match mem::take(&mut self.target) {
Target::Stderr => BufferWriter::stderr(self.is_test, color_choice),
Target::Stdout => BufferWriter::stdout(self.is_test, color_choice),
Target::Pipe(pipe) => BufferWriter::pipe(color_choice, Box::new(Mutex::new(pipe))),
Target::Pipe(pipe) => BufferWriter::pipe(Box::new(Mutex::new(pipe))),
};

Writer {
inner: writer,
write_style: self.write_style,
}
Writer { inner: writer }
}
}

Expand Down

0 comments on commit 6d55509

Please sign in to comment.