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 7bbdbd9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,23 @@ pub fn style<D: Display>(val: D) -> StyledContent<D> {
///
/// This does not always provide a good result.
pub fn available_color_count() -> u16 {
if 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("TERM")
.map(|x| if x.contains("256color") { 256 } else { 8 })
.unwrap_or(8)
.or_else(|_| env::var("COLORTERM"))
.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 7bbdbd9

Please sign in to comment.