Skip to content

Commit

Permalink
Fluentify the ANSI color API
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 19, 2024
1 parent 7327371 commit f346cff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 158 deletions.
52 changes: 40 additions & 12 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::constants::{
BOLD, FAINT, GREEN, INVERSE_VIDEO, NORMAL, NORMAL_INTENSITY, NO_INVERSE_VIDEO, RED,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Color {
Default,
Expand All @@ -14,15 +18,15 @@ pub enum Weight {

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct AnsiStyle {
pub inverse: bool,
pub weight: Weight,
pub color: Color,
pub weight: Weight,
pub inverse: bool,
}

pub const ANSI_STYLE_NORMAL: AnsiStyle = AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Color::Default,
weight: Weight::Normal,
inverse: false,
};

impl AnsiStyle {
Expand All @@ -35,43 +39,67 @@ impl AnsiStyle {

if self == &ANSI_STYLE_NORMAL {
// Special case for resetting to default style
return String::from("\x1b[0m");
return String::from(NORMAL);
}

let mut return_me = String::new();

if self.inverse && !before.inverse {
// Inverse on
return_me.push_str("\x1b[7m");
return_me.push_str(INVERSE_VIDEO);
}
if !self.inverse && before.inverse {
// Inverse off
return_me.push_str("\x1b[27m");
return_me.push_str(NO_INVERSE_VIDEO);
}

if self.weight != before.weight {
if before.weight != Weight::Normal {
// Turn off bold or faint
return_me.push_str("\x1b[22m");
return_me.push_str(NORMAL_INTENSITY);
}
if self.weight == Weight::Faint {
return_me.push_str("\x1b[2m");
return_me.push_str(FAINT);
}
if self.weight == Weight::Bold {
return_me.push_str("\x1b[1m");
return_me.push_str(BOLD);
}
}

if self.color != before.color {
match self.color {
Color::Default => return_me.push_str("\x1b[39m"),
Color::Red => return_me.push_str("\x1b[31m"),
Color::Green => return_me.push_str("\x1b[32m"),
Color::Red => return_me.push_str(RED),
Color::Green => return_me.push_str(GREEN),
}
}

return return_me;
}

pub const fn with_color(&self, color: Color) -> AnsiStyle {
return AnsiStyle {
color,
weight: self.weight,
inverse: self.inverse,
};
}

pub const fn with_inverse(&self, inverse: bool) -> AnsiStyle {
return AnsiStyle {
color: self.color,
weight: self.weight,
inverse,
};
}

pub const fn with_weight(&self, weight: Weight) -> AnsiStyle {
return AnsiStyle {
color: self.color,
weight,
inverse: self.inverse,
};
}
}

// Modifies the input so that all ANSI escape codes are removed
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub const NEW: &str = "\x1b[32m"; // Green
pub const PARSE_ERROR: &str = "\x1b[33m\x1b[7m"; // Inverse yellow

pub const INVERSE_VIDEO: &str = "\x1b[7m";
pub const NO_INVERSE_VIDEO: &str = "\x1b[27m";

pub const NO_EOF_NEWLINE_COLOR: &str = "\x1b[2m"; // Faint

Expand Down
180 changes: 34 additions & 146 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cmp;

use crate::ansi::AnsiStyle;
use crate::ansi::Color::Default;
use crate::ansi::Color::Green;
use crate::ansi::Color::Red;
use crate::ansi::Weight;
Expand Down Expand Up @@ -36,173 +35,78 @@ pub(crate) struct LineStyle {

pub(crate) const LINE_STYLE_OLD: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Red,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Red,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
prefix_style: ANSI_STYLE_NORMAL.with_color(Red),
plain_style: ANSI_STYLE_NORMAL.with_color(Red),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_OLD_FAINT: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Faint,
color: Red,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Faint,
color: Red,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Faint,
color: Red,
},
prefix_style: ANSI_STYLE_NORMAL.with_color(Red).with_weight(Weight::Faint),
plain_style: ANSI_STYLE_NORMAL.with_color(Red).with_weight(Weight::Faint),
highlighted_style: ANSI_STYLE_NORMAL
.with_color(Red)
.with_weight(Weight::Faint)
.with_inverse(true),
}
};

pub(crate) const LINE_STYLE_NEW: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Green,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Green,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Green,
},
prefix_style: ANSI_STYLE_NORMAL.with_color(Green),
plain_style: ANSI_STYLE_NORMAL.with_color(Green),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_ADDS_ONLY: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Faint,
color: Green,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Default,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Green,
},
prefix_style: ANSI_STYLE_NORMAL
.with_color(Green)
.with_weight(Weight::Faint),
plain_style: ANSI_STYLE_NORMAL,
highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_CONFLICT_BASE: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Red,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
prefix_style: ANSI_STYLE_NORMAL.with_inverse(true),
plain_style: ANSI_STYLE_NORMAL.with_color(Red),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_CONFLICT_OLD: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Red,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
prefix_style: ANSI_STYLE_NORMAL.with_inverse(true),
plain_style: ANSI_STYLE_NORMAL.with_color(Red),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_CONFLICT_NEW: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Green,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Green,
},
prefix_style: ANSI_STYLE_NORMAL.with_inverse(true),
plain_style: ANSI_STYLE_NORMAL.with_color(Green),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_OLD_FILENAME: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Bold,
color: Default,
},
prefix_style: ANSI_STYLE_NORMAL.with_weight(Weight::Bold),
plain_style: ANSI_STYLE_NORMAL,
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

pub(crate) const LINE_STYLE_NEW_FILENAME: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: false,
weight: Weight::Bold,
color: Default,
},
prefix_style: ANSI_STYLE_NORMAL.with_weight(Weight::Bold),
plain_style: ANSI_STYLE_NORMAL,
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Green,
},
highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
}
};

Expand Down Expand Up @@ -231,29 +135,13 @@ fn render_row(line_style: &LineStyle, prefix: &str, row: &[StyledToken]) -> Stri
// Render tokens
for token in row {
let new_style = match token.style {
Style::Context => AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Default,
},
Style::Lowlighted => AnsiStyle {
inverse: false,
weight: Weight::Faint,
color: Default,
},
Style::Bright => AnsiStyle {
inverse: false,
weight: Weight::Bold,
color: Default,
},
Style::Context => ANSI_STYLE_NORMAL,
Style::Lowlighted => ANSI_STYLE_NORMAL.with_weight(Weight::Faint),
Style::Bright => ANSI_STYLE_NORMAL.with_weight(Weight::Bold),
Style::Plain => line_style.plain_style,
Style::PlainChange => line_style.plain_style,
Style::HighlightedChange => line_style.highlighted_style,
Style::Error => AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
Style::Error => ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
};

rendered.push_str(&new_style.from(&current_style));
Expand Down

0 comments on commit f346cff

Please sign in to comment.