Skip to content

Commit

Permalink
Improve color detection across platforms
Browse files Browse the repository at this point in the history
Fixes #882
  • Loading branch information
heaths committed May 4, 2024
1 parent 714b9ac commit b8ae856
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,24 @@ pub fn style<D: Display>(val: D) -> StyledContent<D> {
///
/// This does not always provide a good result.
pub fn available_color_count() -> u16 {
env::var("TERM")
.map(|x| if x.contains("256color") { 256 } else { 8 })
.unwrap_or(8)
#[cfg(windows)]
{
// Check if we're running in a pseudo TTY, which supports true color.
// Fall back to env vars otherwise for other terminals on Windows.
if crate::ansi_support::supports_ansi() {
return u16::MAX;
}
}

const DEFAULT: u16 = 8;
env::var("COLORTERM")
.or_else(|_| env::var("TERM"))
.map(|x| match x {
_ if x.contains("24bit") || x.contains("truecolor") => u16::MAX,
_ if x.contains("256") => 256,
_ => DEFAULT,
})
.unwrap_or(DEFAULT)
}

/// Forces colored output on or off globally, overriding NO_COLOR.
Expand Down

0 comments on commit b8ae856

Please sign in to comment.