Skip to content

Commit

Permalink
Merge pull request #248 from epage/atty
Browse files Browse the repository at this point in the history
fix!: Replace atty with is_terminal
  • Loading branch information
epage authored Nov 24, 2022
2 parents e572d04 + d55d269 commit 4c37917
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 39 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.60.0" # MSRV
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ jobs:
- name: Run crate example
run: cargo run --example default
msrv:
name: "Check MSRV: 1.41.0"
name: "Check MSRV: 1.60.0"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.41.0 # MSRV
toolchain: 1.60.0 # MSRV
profile: minimal
override: true
- uses: Swatinem/rust-cache@v1
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.41.0 # MSRV
toolchain: 1.60.0 # MSRV
profile: minimal
override: true
components: clippy
Expand Down
145 changes: 134 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-cli/env_logger/"
categories = ["development-tools::debugging"]
keywords = ["logging", "log", "logger"]
edition = "2018"
rust-version = "1.41.0" # MSRV
edition = "2021"
rust-version = "1.60.0" # MSRV
include = [
"build.rs",
"src/**/*",
Expand All @@ -37,14 +37,18 @@ pre-release-replacements = [
]

[features]
default = ["termcolor", "atty", "humantime", "regex"]
default = ["auto-color", "humantime", "regex"]
color = ["dep:termcolor"]
auto-color = ["dep:is-terminal", "color"]
humantime = ["dep:humantime"]
regex = ["dep:regex"]

[dependencies]
log = { version = "0.4.8", features = ["std"] }
regex = { version = "1.0.3", optional = true, default-features=false, features=["std", "perf"] }
termcolor = { version = "1.1.1", optional = true }
humantime = { version = "2.0.0", optional = true }
atty = { version = "0.2.5", optional = true }
is-terminal = { version = "0.4.0", optional = true }

[[test]]
name = "regexp_filter"
Expand Down
2 changes: 1 addition & 1 deletion ci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod permute;
mod task;

fn main() {
let features = ["termcolor", "humantime", "atty", "regex"];
let features = ["color", "humantime", "auto-color", "regex"];

// Run a default build
if !task::test(Default::default()) {
Expand Down
2 changes: 1 addition & 1 deletion ci/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn test(args: TestArgs) -> bool {
}

if let Some(features) = &features {
command.args(&["--features", features]);
command.args(["--features", features]);
}

println!("running {:?}", command);
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $ export MY_LOG_STYLE=never
If you want to control the logging output completely, see the `custom_logger` example.
*/

#[cfg(all(feature = "termcolor", feature = "humantime"))]
#[cfg(all(feature = "color", feature = "humantime"))]
fn main() {
use env_logger::{fmt::Color, Builder, Env};

Expand Down Expand Up @@ -50,5 +50,5 @@ fn main() {
log::info!("a log from `MyLogger`");
}

#[cfg(not(all(feature = "termcolor", feature = "humantime")))]
#[cfg(not(all(feature = "color", feature = "humantime")))]
fn main() {}
12 changes: 6 additions & 6 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ impl Builder {
}
}

#[cfg(feature = "termcolor")]
#[cfg(feature = "color")]
type SubtleStyle = StyledValue<'static, &'static str>;
#[cfg(not(feature = "termcolor"))]
#[cfg(not(feature = "color"))]
type SubtleStyle = &'static str;

/// The default format.
Expand Down Expand Up @@ -233,7 +233,7 @@ impl<'a> DefaultFormat<'a> {
}

fn subtle_style(&self, text: &'static str) -> SubtleStyle {
#[cfg(feature = "termcolor")]
#[cfg(feature = "color")]
{
self.buf
.style()
Expand All @@ -242,7 +242,7 @@ impl<'a> DefaultFormat<'a> {
.clone()
.into_value(text)
}
#[cfg(not(feature = "termcolor"))]
#[cfg(not(feature = "color"))]
{
text
}
Expand All @@ -268,11 +268,11 @@ impl<'a> DefaultFormat<'a> {
}

let level = {
#[cfg(feature = "termcolor")]
#[cfg(feature = "color")]
{
self.buf.default_styled_level(record.level())
}
#[cfg(not(feature = "termcolor"))]
#[cfg(not(feature = "color"))]
{
record.level()
}
Expand Down
17 changes: 9 additions & 8 deletions src/fmt/writer/atty.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
/*
This internal module contains the terminal detection implementation.
If the `atty` crate is available then we use it to detect whether we're
attached to a particular TTY. If the `atty` crate is not available we
assume we're not attached to anything. This effectively prevents styles
from being printed.
If the `auto-color` feature is enabled then we detect whether we're attached to a particular TTY.
Otherwise, assume we're not attached to anything. This effectively prevents styles from being
printed.
*/

#[cfg(feature = "atty")]
#[cfg(feature = "auto-color")]
mod imp {
use is_terminal::IsTerminal;

pub(in crate::fmt) fn is_stdout() -> bool {
atty::is(atty::Stream::Stdout)
std::io::stdout().is_terminal()
}

pub(in crate::fmt) fn is_stderr() -> bool {
atty::is(atty::Stream::Stderr)
std::io::stderr().is_terminal()
}
}

#[cfg(not(feature = "atty"))]
#[cfg(not(feature = "auto-color"))]
mod imp {
pub(in crate::fmt) fn is_stdout() -> bool {
false
Expand Down
1 change: 1 addition & 0 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl Builder {
}

/// Whether or not to capture logs for `cargo test`.
#[allow(clippy::wrong_self_convention)]
pub(crate) fn is_test(&mut self, is_test: bool) -> &mut Self {
self.is_test = is_test;
self
Expand Down
4 changes: 2 additions & 2 deletions src/fmt/writer/termcolor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Its public API is available when the `termcolor` crate is available.
The terminal printing is shimmed when the `termcolor` crate is not available.
*/

#[cfg_attr(feature = "termcolor", path = "extern_impl.rs")]
#[cfg_attr(not(feature = "termcolor"), path = "shim_impl.rs")]
#[cfg_attr(feature = "color", path = "extern_impl.rs")]
#[cfg_attr(not(feature = "color"), path = "shim_impl.rs")]
mod imp;

pub(in crate::fmt) use self::imp::*;
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ pub fn init() {
/// ```
/// use env_logger::{Builder, Env};
///
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// # fn run() -> Result<(), Box<dyn ::std::error::Error>> {
/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");
///
/// env_logger::try_init_from_env(env)?;
Expand Down

0 comments on commit 4c37917

Please sign in to comment.