tidy up true color fallback handling

This commit is contained in:
Wez Furlong committed 2018-07-20 06:59:32 -07:00
1 parent 9c1fad733d
commit 4090417b29
2 files changed
+42 -35

No files matched your search

+25 -15
View File
@@ -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<RgbColor> 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<RgbColor>,
/// 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<AnsiColor> for ColorAttribute {
fn from(col: AnsiColor) -> Self {
Self {
full: None,
ansi: ColorSpec::PaletteIndex(col as u8),
}
ColorAttribute::PaletteIndex(col as u8)
}
}
impl From<ColorSpec> 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),
}
}
}
+17 -20
View File
@@ -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::<cap::SetAForeground>()
{
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::<cap::SetABackground>()
{
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::<cap::ClearScreen>() {