Skip to content

Commit

Permalink
gnu_legacy: with GNU, write foreground first, else background first.
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 13, 2023
1 parent f1c83ff commit 2346341
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,42 @@ impl Style {
// The foreground and background colors, if specified, need to be
// handled specially because the number codes are more complicated.
// (see `write_background_code` and `write_foreground_code`)
if let Some(bg) = self.background {
if written_anything {
write!(f, ";")?;

#[cfg(feature = "gnu_legacy")]
// write foreground first, else background first.
{
if let Some(fg) = self.foreground {
if written_anything {
write!(f, ";")?;
}
written_anything = true;
fg.write_foreground_code(f)?;
}
written_anything = true;
bg.write_background_code(f)?;
}

if let Some(fg) = self.foreground {
if written_anything {
write!(f, ";")?;
if let Some(bg) = self.background {
if written_anything {
write!(f, ";")?;
}
bg.write_background_code(f)?;
}
fg.write_foreground_code(f)?;
}
#[cfg(not(feature = "gnu_legacy"))]
{
if let Some(bg) = self.background {
if written_anything {
write!(f, ";")?;
}
written_anything = true;
bg.write_background_code(f)?;
}

if let Some(fg) = self.foreground {
if written_anything {
write!(f, ";")?;
}
fg.write_foreground_code(f)?;
}
}
// All the codes end with an `m`, because reasons.
write!(f, "m")?;

Expand Down Expand Up @@ -397,6 +418,8 @@ macro_rules! test {
mod test {
use crate::style::Color::*;
use crate::style::Style;
use crate::Color;
use std::default::Default;

test!(plain: Style::default(); "text/plain" => "text/plain");
test!(red: Red; "hi" => "\x1B[31mhi\x1B[0m");
Expand Down Expand Up @@ -452,13 +475,28 @@ mod test {
assert_eq!(White.normal().infix(Blue.normal()).to_string(), "\x1B[34m");
assert_eq!(Blue.bold().infix(Blue.bold()).to_string(), "");
}

#[test]
fn test_write_prefix_no_gnu_compat_order() {
let style = Style {
foreground: Some(Color::Red),
background: Some(Color::Blue),
..Default::default()
};
assert_eq!(
style.paint("file").to_string(),
"\u{1b}[44;31mfile\u{1b}[0m".to_string()
);
}
}

#[cfg(test)]
#[cfg(feature = "gnu_legacy")]
mod gnu_legacy_test {
use crate::style::Color::*;
use crate::style::Style;
use crate::Color;
use std::default::Default;

test!(plain: Style::default(); "text/plain" => "text/plain");
test!(red: Red; "hi" => "\x1B[31mhi\x1B[0m");
Expand Down Expand Up @@ -499,4 +537,17 @@ mod gnu_legacy_test {
test!(hidden: Style::new().hidden(); "hi" => "\x1B[08mhi\x1B[0m");
test!(stricken: Style::new().strikethrough(); "hi" => "\x1B[09mhi\x1B[0m");
test!(lr_on_lr: LightRed.on(LightRed); "hi" => "\x1B[101;91mhi\x1B[0m");

#[test]
fn test_write_prefix_gnu_compat_order() {
let style = Style {
foreground: Some(Color::Red),
background: Some(Color::Blue),
..Default::default()
};
assert_eq!(
style.paint("file").to_string(),
"\u{1b}[31;44mfile\u{1b}[0m".to_string()
);
}
}

0 comments on commit 2346341

Please sign in to comment.