Skip to content

Commit

Permalink
Merge pull request #17 from bpowers/is_terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO authored Nov 30, 2022
2 parents 904ac05 + 003d2cf commit 39ca02b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion spdlog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ multi-thread = ["crossbeam"]
[dependencies]
arc-swap = "1.5.1"
atomic = "0.5.1"
atty = "0.2.14"
cfg-if = "1.0.0"
chrono = "0.4.22"
crossbeam = { version = "0.8.2", optional = true }
flexible-string = { version = "0.1.0", optional = true }
if_chain = "1.0.2"
is-terminal = "0.4"
log = { version = "0.4", optional = true }
once_cell = "1.16.0"
spdlog-macros = { version = "0.1.0", path = "../spdlog-macros" }
Expand Down
31 changes: 20 additions & 11 deletions spdlog/src/sink/std_stream_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl StdStreamDest<io::Stdout, io::Stderr> {
StdStreamDest::Stderr(stream) => StdStreamDest::Stderr(stream.lock()),
}
}

fn stream_type(&self) -> StdStream {
match self {
StdStreamDest::Stdout(_) => StdStream::Stdout,
StdStreamDest::Stderr(_) => StdStream::Stderr,
}
}
}

macro_rules! impl_write_for_dest {
Expand Down Expand Up @@ -79,7 +86,6 @@ impl_write_for_dest!(StdStreamDest<io::StdoutLock<'_>, io::StderrLock<'_>>);
pub struct StdStreamSink {
common_impl: helper::CommonImpl,
dest: StdStreamDest<io::Stdout, io::Stderr>,
atty_stream: atty::Stream,
should_render_style: bool,
level_style_codes: LevelStyleCodes,
}
Expand Down Expand Up @@ -116,14 +122,20 @@ impl StdStreamSink {

/// Sets the style mode.
pub fn set_style_mode(&mut self, style_mode: StyleMode) {
self.should_render_style = Self::should_render_style(style_mode, self.atty_stream);
self.should_render_style = Self::should_render_style(style_mode, self.dest.stream_type());
}

#[must_use]
fn should_render_style(style_mode: StyleMode, atty_stream: atty::Stream) -> bool {
fn should_render_style(style_mode: StyleMode, stream: StdStream) -> bool {
use is_terminal::IsTerminal;
let is_terminal = match stream {
StdStream::Stdout => io::stdout().is_terminal(),
StdStream::Stderr => io::stderr().is_terminal(),
};

match style_mode {
StyleMode::Always => true,
StyleMode::Auto => atty::is(atty_stream) && enable_ansi_escape_sequences(),
StyleMode::Auto => is_terminal && enable_ansi_escape_sequences(),
StyleMode::Never => false,
}
}
Expand Down Expand Up @@ -263,16 +275,13 @@ impl StdStreamSinkBuilder<()> {
impl StdStreamSinkBuilder<StdStream> {
/// Builds a [`StdStreamSink`].
pub fn build(self) -> Result<StdStreamSink> {
let atty_stream = match self.std_stream {
StdStream::Stdout => atty::Stream::Stdout,
StdStream::Stderr => atty::Stream::Stderr,
};

Ok(StdStreamSink {
common_impl: helper::CommonImpl::from_builder(self.common_builder_impl),
dest: StdStreamDest::new(self.std_stream),
atty_stream,
should_render_style: StdStreamSink::should_render_style(self.style_mode, atty_stream),
should_render_style: StdStreamSink::should_render_style(
self.style_mode,
self.std_stream,
),
level_style_codes: LevelStyleCodes::default(),
})
}
Expand Down

0 comments on commit 39ca02b

Please sign in to comment.