From 4090417b2973203fb558f87c2a84b9f68616974f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 20 Jul 2018 06:54:51 -0700 Subject: [PATCH] tidy up true color fallback handling --- src/color.rs | 40 +++++++++++++++++++++++++--------------- src/render/terminfo.rs | 37 +++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/color.rs b/src/color.rs index 9cafb1c6f..d14ff27b7 100644 --- a/src/color.rs +++ b/src/color.rs @@ -110,6 +110,9 @@ impl<'de> Deserialize<'de> for RgbColor { } } +/// An index into the fixed color palette. +pub type PaletteIndex = u8; + /// Specifies the color to be used when rendering a cell. /// This differs from `ColorAttribute` in that this type can only /// specify one of the possible color types at once, whereas the @@ -118,7 +121,7 @@ impl<'de> Deserialize<'de> for RgbColor { pub enum ColorSpec { Default, /// Use either a raw number, or use values from the `AnsiColor` enum - PaletteIndex(u8), + PaletteIndex(PaletteIndex), TrueColor(RgbColor), } @@ -144,29 +147,36 @@ impl From for ColorSpec { /// type used in the `CellAttributes` struct and can specify an optional /// TrueColor value, allowing a fallback to a more traditional palette /// index if TrueColor is not available. -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)] -pub struct ColorAttribute { - /// Used if the terminal supports full color - pub full: Option, - /// If the terminal doesn't support full color, or the full color - /// spec is_none, use old school ansi color number. - pub ansi: ColorSpec, +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum ColorAttribute { + /// Use RgbColor when supported, falling back to the specified PaletteIndex. + TrueColorWithPaletteFallback(RgbColor, PaletteIndex), + /// Use RgbColor when supported, falling back to the default color + TrueColorWithDefaultFallback(RgbColor), + /// Use the specified PaletteIndex + PaletteIndex(PaletteIndex), + /// Use the default color + Default, +} + +impl Default for ColorAttribute { + fn default() -> Self { + ColorAttribute::Default + } } impl From for ColorAttribute { fn from(col: AnsiColor) -> Self { - Self { - full: None, - ansi: ColorSpec::PaletteIndex(col as u8), - } + ColorAttribute::PaletteIndex(col as u8) } } impl From for ColorAttribute { fn from(spec: ColorSpec) -> Self { - Self { - full: None, - ansi: spec, + match spec { + ColorSpec::Default => ColorAttribute::Default, + ColorSpec::PaletteIndex(idx) => ColorAttribute::PaletteIndex(idx), + ColorSpec::TrueColor(color) => ColorAttribute::TrueColorWithDefaultFallback(color), } } } diff --git a/src/render/terminfo.rs b/src/render/terminfo.rs index f245e93db..e887fc50b 100644 --- a/src/render/terminfo.rs +++ b/src/render/terminfo.rs @@ -1,7 +1,7 @@ //! Rendering of Changes using terminfo use caps::{Capabilities, ColorLevel}; use cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline}; -use color::ColorSpec; +use color::{ColorAttribute, ColorSpec}; use escape::csi::{Cursor, Edit, EraseInDisplay, Sgr, CSI}; use escape::osc::OperatingSystemCommand; use escape::EncodeEscape; @@ -204,21 +204,21 @@ impl Renderer for TerminfoRenderer { let has_true_color = self.caps.color_level() == ColorLevel::TrueColor; if attr.foreground != current_attr.foreground { - match (has_true_color, attr.foreground.full, attr.foreground.ansi) { - (true, Some(tc), _) | (true, _, ColorSpec::TrueColor(tc)) => { + match (has_true_color, attr.foreground) { + (true, ColorAttribute::TrueColorWithPaletteFallback(tc, _)) + | (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => { CSI::Sgr(Sgr::Foreground(ColorSpec::TrueColor(tc))) .encode_escape(&mut WriteWrapper::new(out))?; } - (false, _, ColorSpec::TrueColor(_)) => { - // TrueColor was specified with no fallback :-( - } - (_, _, ColorSpec::Default) => { + (false, ColorAttribute::TrueColorWithDefaultFallback(_)) + | (_, ColorAttribute::Default) => { // Terminfo doesn't define a reset color to default, so // we use the ANSI code. CSI::Sgr(Sgr::Foreground(ColorSpec::Default)) .encode_escape(&mut WriteWrapper::new(out))?; } - (_, _, ColorSpec::PaletteIndex(idx)) => { + (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) + | (_, ColorAttribute::PaletteIndex(idx)) => { if let Some(set) = self.get_capability::() { set.expand().color(idx).to(WriteWrapper::new(out))?; @@ -231,21 +231,21 @@ impl Renderer for TerminfoRenderer { } if attr.background != current_attr.background { - match (has_true_color, attr.background.full, attr.background.ansi) { - (true, Some(tc), _) | (true, _, ColorSpec::TrueColor(tc)) => { + match (has_true_color, attr.background) { + (true, ColorAttribute::TrueColorWithPaletteFallback(tc, _)) + | (true, ColorAttribute::TrueColorWithDefaultFallback(tc)) => { CSI::Sgr(Sgr::Background(ColorSpec::TrueColor(tc))) .encode_escape(&mut WriteWrapper::new(out))?; } - (false, _, ColorSpec::TrueColor(_)) => { - // TrueColor was specified with no fallback :-( - } - (_, _, ColorSpec::Default) => { + (false, ColorAttribute::TrueColorWithDefaultFallback(_)) + | (_, ColorAttribute::Default) => { // Terminfo doesn't define a reset color to default, so // we use the ANSI code. CSI::Sgr(Sgr::Background(ColorSpec::Default)) .encode_escape(&mut WriteWrapper::new(out))?; } - (_, _, ColorSpec::PaletteIndex(idx)) => { + (false, ColorAttribute::TrueColorWithPaletteFallback(_, idx)) + | (_, ColorAttribute::PaletteIndex(idx)) => { if let Some(set) = self.get_capability::() { set.expand().color(idx).to(WriteWrapper::new(out))?; @@ -287,11 +287,8 @@ impl Renderer for TerminfoRenderer { } pending_attr = None; - if current_attr.background.full.is_none() - && (current_attr.background.ansi == ColorSpec::Default) - || self.caps.bce() - { - // The erase operation either respects "background color erase", + if current_attr.background == ColorAttribute::Default || self.caps.bce() { + // The erase operation respects "background color erase", // or we're clearing to the default background color, so we can // simply emit a clear screen op. if let Some(clr) = self.get_capability::() {