Lines
100 %
Functions
Branches
/// How many colours the terminal can show.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorDepth {
Rgb,
Indexed256,
Ansi16,
}
const TRUECOLOR_TERMS: [&str; 5] = ["kitty", "ghostty", "wezterm", "foot", "alacritty"];
impl ColorDepth {
/// The word the config and the command line name this depth with.
#[must_use]
pub fn name(self) -> &'static str {
match self {
ColorDepth::Rgb => "rgb",
ColorDepth::Indexed256 => "256",
ColorDepth::Ansi16 => "16",
/// The depth `TERM` and `COLORTERM` advertise.
pub fn detect(term: Option<&str>, colorterm: Option<&str>) -> ColorDepth {
let colorterm = colorterm.map(str::to_ascii_lowercase);
let term = term.map(str::to_ascii_lowercase).unwrap_or_default();
let truecolor_terminal = TRUECOLOR_TERMS.iter().any(|name| term.contains(name))
|| term.ends_with("-direct")
|| term.ends_with("-truecolor");
match colorterm.as_deref() {
Some("truecolor" | "24bit") => ColorDepth::Rgb,
_ if truecolor_terminal => ColorDepth::Rgb,
_ if term.ends_with("-256color") => ColorDepth::Indexed256,
_ => ColorDepth::Ansi16,
impl std::str::FromStr for ColorDepth {
type Err = String;
fn from_str(s: &str) -> Result<ColorDepth, String> {
match s.to_ascii_lowercase().as_str() {
"rgb" => Ok(ColorDepth::Rgb),
"256" => Ok(ColorDepth::Indexed256),
"16" => Ok(ColorDepth::Ansi16),
_ => Err(format!("unknown colour depth `{s}`; known: rgb, 256, 16")),