use super::Rgb;
use super::convert::{palette, to_16, to_256};
use crate::environ::Environment;
use crate::renderer::cell::{Colour, Link, Pen};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ColorProfile {
NoTty,
Ascii,
Ansi,
Ansi256,
TrueColor,
}
impl fmt::Display for ColorProfile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::NoTty => "NoTTY",
Self::Ascii => "Ascii",
Self::Ansi => "ANSI",
Self::Ansi256 => "ANSI256",
Self::TrueColor => "TrueColor",
})
}
}
impl ColorProfile {
#[must_use]
pub fn detect(is_terminal: bool, env: &Environment) -> Self {
Self::detect_with(is_terminal, env, tmux_info)
}
pub(crate) fn detect_with(
is_terminal: bool,
env: &Environment,
tmux: impl FnOnce() -> Option<String>,
) -> Self {
let tty = env.flag("TTY_FORCE") || is_terminal;
let term = env.get("TERM");
let dumb = term.is_none_or(|t| t == "dumb");
let profile = from_env(tty, env);
if profile == Self::TrueColor || env.flag("NO_COLOR") {
return profile;
}
if tty && !dumb {
let terminfo = if term.is_some_and(|t| !t.is_empty()) {
Self::Ansi
} else {
Self::NoTty
};
return profile.max(terminfo).max(in_tmux(env, tmux));
}
profile
}
pub(crate) fn colour(self, c: Colour) -> Colour {
match (self, c) {
(Self::NoTty | Self::Ascii, _) | (_, Colour::Default) => Colour::Default,
(Self::TrueColor, c)
| (_, c @ Colour::Basic(_))
| (Self::Ansi256, c @ Colour::Indexed(_)) => c,
(_, Colour::Indexed(n)) => Colour::Basic(to_16(n)),
(Self::Ansi256, Colour::Rgb(r, g, b)) => Colour::Indexed(to_256(Rgb::new(r, g, b))),
(_, Colour::Rgb(r, g, b)) => Colour::Basic(to_16(to_256(Rgb::new(r, g, b)))),
}
}
pub(crate) fn pen(self, pen: Pen) -> Pen {
if self == Self::NoTty {
return Pen::DEFAULT;
}
Pen {
fg: self.colour(pen.fg),
bg: self.colour(pen.bg),
underline_colour: self.colour(pen.underline_colour),
..pen
}
}
pub(crate) fn link(self, link: &Link) -> Link {
if self == Self::NoTty {
None
} else {
link.clone()
}
}
pub(crate) fn rgb(self, c: Rgb) -> Option<Rgb> {
match self {
Self::NoTty | Self::Ascii => None,
Self::Ansi => Some(palette(to_16(to_256(c)))),
Self::Ansi256 => Some(palette(to_256(c))),
Self::TrueColor => Some(c),
}
}
}
fn from_env(tty: bool, env: &Environment) -> ColorProfile {
let term = env.get("TERM");
let dumb = term.is_none_or(|t| t == "dumb");
let env_profile = term_profile(env);
let mut p = if !tty || dumb {
ColorProfile::NoTty
} else {
env_profile
};
if env.flag("NO_COLOR") && tty {
return p.min(ColorProfile::Ascii);
}
if env.flag("CLICOLOR_FORCE") {
return p.max(ColorProfile::Ansi).max(env_profile);
}
if env.flag("CLICOLOR") && tty && !dumb {
p = p.max(ColorProfile::Ansi);
}
p
}
const TRUE_COLOUR_TERMS: [&str; 8] = [
"alacritty",
"contour",
"foot",
"ghostty",
"kitty",
"rio",
"st",
"wezterm",
];
fn term_profile(env: &Environment) -> ColorProfile {
let term = env.var("TERM");
if TRUE_COLOUR_TERMS.iter().any(|t| term.contains(t)) || says_true_colour(env, term) {
return ColorProfile::TrueColor;
}
let mut p = if term.is_empty() || term == "dumb" {
ColorProfile::NoTty
} else {
ColorProfile::Ansi
};
if is_multiplexer(term) || term.ends_with("256color") {
p = p.max(ColorProfile::Ansi256);
}
p
}
fn is_multiplexer(term: &str) -> bool {
term.starts_with("tmux") || term.starts_with("screen")
}
fn says_true_colour(env: &Environment, term: &str) -> bool {
let colorterm = env.var("COLORTERM").to_lowercase();
let says = ["truecolor", "24bit", "yes", "true"].contains(&colorterm.as_str());
!env.var("WT_SESSION").is_empty()
|| env.flag("GOOGLE_CLOUD_SHELL")
|| (says && !is_multiplexer(term))
|| term.ends_with("direct")
}
fn in_tmux(env: &Environment, info: impl FnOnce() -> Option<String>) -> ColorProfile {
if env.var("TMUX").is_empty() {
return ColorProfile::NoTty;
}
let says_rgb =
|line: &str| (line.contains("Tc") || line.contains("RGB")) && line.contains("true");
match info() {
Some(out) if out.lines().any(says_rgb) => ColorProfile::TrueColor,
_ => ColorProfile::Ansi256,
}
}
fn tmux_info() -> Option<String> {
let out = std::process::Command::new("tmux")
.arg("info")
.stdin(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.output()
.ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}