From 786b5c533e32cfe88dbf8742fdbc18829fb46334 Mon Sep 17 00:00:00 2001 From: Kirawi <67773714+kirawi@users.noreply.github.com> Date: Sun, 11 Feb 2024 12:38:09 -0500 Subject: [PATCH] follow neovim's truecolor detection (#9577) --- Cargo.lock | 1 + helix-term/Cargo.toml | 1 + helix-term/src/lib.rs | 25 +++++++++++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7ef8eb05..a0f5645dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1387,6 +1387,7 @@ dependencies = [ "signal-hook-tokio", "smallvec", "tempfile", + "termini", "tokio", "tokio-stream", "toml", diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index a0d6754d0..f3343b7a4 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -42,6 +42,7 @@ signal-hook = "0.3" tokio-stream = "0.1" futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } arc-swap = { version = "1.6.0" } +termini = "1" # Logging fern = "0.6" diff --git a/helix-term/src/lib.rs b/helix-term/src/lib.rs index b1413ed0d..cdde86ec5 100644 --- a/helix-term/src/lib.rs +++ b/helix-term/src/lib.rs @@ -22,17 +22,30 @@ use url::Url; pub use keymap::macros::*; -#[cfg(not(windows))] -fn true_color() -> bool { - std::env::var("COLORTERM") - .map(|v| matches!(v.as_str(), "truecolor" | "24bit")) - .unwrap_or(false) -} #[cfg(windows)] fn true_color() -> bool { true } +#[cfg(not(windows))] +fn true_color() -> bool { + if matches!( + std::env::var("COLORTERM").map(|v| matches!(v.as_str(), "truecolor" | "24bit")), + Ok(true) + ) { + return true; + } + + match termini::TermInfo::from_env() { + Ok(t) => { + t.extended_cap("RGB").is_some() + || t.extended_cap("Tc").is_some() + || (t.extended_cap("setrgbf").is_some() && t.extended_cap("setrgbb").is_some()) + } + Err(_) => false, + } +} + /// Function used for filtering dir entries in the various file pickers. fn filter_picker_entry(entry: &DirEntry, root: &Path, dedup_symlinks: bool) -> bool { // We always want to ignore the .git directory, otherwise if