diff --git a/termwiz/src/render/terminfo.rs b/termwiz/src/render/terminfo.rs index ac2ec9d08..d80b4892f 100644 --- a/termwiz/src/render/terminfo.rs +++ b/termwiz/src/render/terminfo.rs @@ -125,6 +125,10 @@ impl TerminfoRenderer { } let has_true_color = self.caps.color_level() == ColorLevel::TrueColor; + let terminfo_color: i32 = match self.get_capability::() { + Some(cap::MaxColors(n)) => n, + None => 0, + }; if attr.foreground != current_foreground { match (has_true_color, attr.foreground) { @@ -144,14 +148,17 @@ impl TerminfoRenderer { } (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) | (_, ColorAttribute::PaletteIndex(idx)) => { - if let Some(set) = self.get_capability::() { - set.expand().color(idx).to(out.by_ref())?; - } else { - write!( - out, - "{}", - CSI::Sgr(Sgr::Foreground(ColorSpec::PaletteIndex(idx))) - )?; + match self.get_capability::() { + Some(set) if (idx as i32) < terminfo_color => { + set.expand().color(idx).to(out.by_ref())?; + } + _ => { + write!( + out, + "{}", + CSI::Sgr(Sgr::Foreground(ColorSpec::PaletteIndex(idx))) + )?; + } } } } @@ -175,14 +182,17 @@ impl TerminfoRenderer { } (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) | (_, ColorAttribute::PaletteIndex(idx)) => { - if let Some(set) = self.get_capability::() { - set.expand().color(idx).to(out.by_ref())?; - } else { - write!( - out, - "{}", - CSI::Sgr(Sgr::Background(ColorSpec::PaletteIndex(idx))) - )?; + match self.get_capability::() { + Some(set) if (idx as i32) < terminfo_color => { + set.expand().color(idx).to(out.by_ref())?; + } + _ => { + write!( + out, + "{}", + CSI::Sgr(Sgr::Background(ColorSpec::PaletteIndex(idx))) + )?; + } } } }