Skip to content

Commit 39ca02b

Browse files
authored
Merge pull request #17 from bpowers/is_terminal
2 parents 904ac05 + 003d2cf commit 39ca02b

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

spdlog/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ multi-thread = ["crossbeam"]
4040
[dependencies]
4141
arc-swap = "1.5.1"
4242
atomic = "0.5.1"
43-
atty = "0.2.14"
4443
cfg-if = "1.0.0"
4544
chrono = "0.4.22"
4645
crossbeam = { version = "0.8.2", optional = true }
4746
flexible-string = { version = "0.1.0", optional = true }
4847
if_chain = "1.0.2"
48+
is-terminal = "0.4"
4949
log = { version = "0.4", optional = true }
5050
once_cell = "1.16.0"
5151
spdlog-macros = { version = "0.1.0", path = "../spdlog-macros" }

spdlog/src/sink/std_stream_sink.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ impl StdStreamDest<io::Stdout, io::Stderr> {
4747
StdStreamDest::Stderr(stream) => StdStreamDest::Stderr(stream.lock()),
4848
}
4949
}
50+
51+
fn stream_type(&self) -> StdStream {
52+
match self {
53+
StdStreamDest::Stdout(_) => StdStream::Stdout,
54+
StdStreamDest::Stderr(_) => StdStream::Stderr,
55+
}
56+
}
5057
}
5158

5259
macro_rules! impl_write_for_dest {
@@ -79,7 +86,6 @@ impl_write_for_dest!(StdStreamDest<io::StdoutLock<'_>, io::StderrLock<'_>>);
7986
pub struct StdStreamSink {
8087
common_impl: helper::CommonImpl,
8188
dest: StdStreamDest<io::Stdout, io::Stderr>,
82-
atty_stream: atty::Stream,
8389
should_render_style: bool,
8490
level_style_codes: LevelStyleCodes,
8591
}
@@ -116,14 +122,20 @@ impl StdStreamSink {
116122

117123
/// Sets the style mode.
118124
pub fn set_style_mode(&mut self, style_mode: StyleMode) {
119-
self.should_render_style = Self::should_render_style(style_mode, self.atty_stream);
125+
self.should_render_style = Self::should_render_style(style_mode, self.dest.stream_type());
120126
}
121127

122128
#[must_use]
123-
fn should_render_style(style_mode: StyleMode, atty_stream: atty::Stream) -> bool {
129+
fn should_render_style(style_mode: StyleMode, stream: StdStream) -> bool {
130+
use is_terminal::IsTerminal;
131+
let is_terminal = match stream {
132+
StdStream::Stdout => io::stdout().is_terminal(),
133+
StdStream::Stderr => io::stderr().is_terminal(),
134+
};
135+
124136
match style_mode {
125137
StyleMode::Always => true,
126-
StyleMode::Auto => atty::is(atty_stream) && enable_ansi_escape_sequences(),
138+
StyleMode::Auto => is_terminal && enable_ansi_escape_sequences(),
127139
StyleMode::Never => false,
128140
}
129141
}
@@ -263,16 +275,13 @@ impl StdStreamSinkBuilder<()> {
263275
impl StdStreamSinkBuilder<StdStream> {
264276
/// Builds a [`StdStreamSink`].
265277
pub fn build(self) -> Result<StdStreamSink> {
266-
let atty_stream = match self.std_stream {
267-
StdStream::Stdout => atty::Stream::Stdout,
268-
StdStream::Stderr => atty::Stream::Stderr,
269-
};
270-
271278
Ok(StdStreamSink {
272279
common_impl: helper::CommonImpl::from_builder(self.common_builder_impl),
273280
dest: StdStreamDest::new(self.std_stream),
274-
atty_stream,
275-
should_render_style: StdStreamSink::should_render_style(self.style_mode, atty_stream),
281+
should_render_style: StdStreamSink::should_render_style(
282+
self.style_mode,
283+
self.std_stream,
284+
),
276285
level_style_codes: LevelStyleCodes::default(),
277286
})
278287
}

0 commit comments

Comments
 (0)