From a72b3794c1d35a08ab3bde05b6d7416cad52fc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4ggstr=C3=B6m?= Date: Wed, 29 Mar 2023 11:54:35 +0200 Subject: [PATCH] Query terminfo for color capabilities --- Cargo.toml | 1 + src/control.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 170c690..1a40b83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ no-color = [] [dependencies] atty = "0.2" lazy_static = "1" +terminfo = "0.7" [target.'cfg(windows)'.dependencies.winapi] version = "0.3" diff --git a/src/control.rs b/src/control.rs index 7ad6e62..0aae563 100644 --- a/src/control.rs +++ b/src/control.rs @@ -104,8 +104,15 @@ impl ShouldColorize { /// followed by `CLICOLOR` combined with tty check. pub fn from_env() -> Self { ShouldColorize { - clicolor: ShouldColorize::normalize_env(env::var("CLICOLOR")).unwrap_or_else(|| true) - && atty::is(atty::Stream::Stdout), + clicolor: (ShouldColorize::normalize_env(env::var("CLICOLOR")).unwrap_or(true)) + && atty::is(atty::Stream::Stdout) + && (if let Ok(db) = terminfo::Database::from_env() { + db.get::() + .map(|mc| 2 < mc.into()) + .unwrap_or(false) + } else { + true + }), clicolor_force: ShouldColorize::resolve_clicolor_force( env::var("NO_COLOR"), env::var("CLICOLOR_FORCE"),