summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-05-22 15:21:10 -0400
committerDan Davison <dandavison7@gmail.com>2020-05-22 22:45:47 -0400
commitac64b1236bb85cb693b71a0d3552bff1d298da22 (patch)
tree9ffe16c979361a9580018019aca6d82e0a558e8f /src
parent2245582fbef18d94b0bef2fec3389f710dadc8d9 (diff)
Use new Style struct, wrapping ansi_term::Style
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs8
-rw-r--r--src/config.rs32
-rw-r--r--src/delta.rs2
-rw-r--r--src/main.rs15
-rw-r--r--src/paint.rs94
-rw-r--r--src/style.rs31
-rw-r--r--src/tests/ansi_test_utils.rs5
7 files changed, 117 insertions, 70 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 3f92f6f0..5847e600 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -537,28 +537,28 @@ mod tests {
assert_eq!(config.theme.unwrap().name.as_ref().unwrap(), expected_theme);
}
assert_eq!(
- config.minus_style.background.unwrap(),
+ config.minus_style.ansi_term_style.background.unwrap(),
style::get_minus_background_color_default(
expected_mode == Mode::Light,
is_true_color
)
);
assert_eq!(
- config.minus_emph_style.background.unwrap(),
+ config.minus_emph_style.ansi_term_style.background.unwrap(),
style::get_minus_emph_background_color_default(
expected_mode == Mode::Light,
is_true_color
)
);
assert_eq!(
- config.plus_style.background.unwrap(),
+ config.plus_style.ansi_term_style.background.unwrap(),
style::get_plus_background_color_default(
expected_mode == Mode::Light,
is_true_color
)
);
assert_eq!(
- config.plus_emph_style.background.unwrap(),
+ config.plus_emph_style.ansi_term_style.background.unwrap(),
style::get_plus_emph_background_color_default(
expected_mode == Mode::Light,
is_true_color
diff --git a/src/config.rs b/src/config.rs
index 04286733..3f1356ac 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,7 +1,7 @@
use std::process;
use std::str::FromStr;
-use ansi_term::{Color, Style};
+use ansi_term::Color;
use syntect::highlighting::Color as SyntectColor;
use syntect::highlighting::Style as SyntectStyle;
use syntect::highlighting::{Theme, ThemeSet};
@@ -11,7 +11,7 @@ use crate::bat::output::PagingMode;
use crate::bat::terminal::to_ansi_color;
use crate::cli;
use crate::env;
-use crate::style;
+use crate::style::{self, Style};
use crate::syntect_color;
pub struct Config<'a> {
@@ -242,27 +242,31 @@ pub fn parse_style_string(
let mut seen_background = false;
for s in style_string.to_lowercase().split_whitespace() {
if s == "blink" {
- style.is_blink = true;
+ style.ansi_term_style.is_blink = true;
} else if s == "bold" {
- style.is_bold = true;
+ style.ansi_term_style.is_bold = true;
} else if s == "dimmed" {
- style.is_dimmed = true;
+ style.ansi_term_style.is_dimmed = true;
} else if s == "hidden" {
- style.is_hidden = true;
+ style.ansi_term_style.is_hidden = true;
} else if s == "italic" {
- style.is_italic = true;
+ style.ansi_term_style.is_italic = true;
} else if s == "reverse" {
- style.is_reverse = true;
+ style.ansi_term_style.is_reverse = true;
} else if s == "strikethrough" {
- style.is_strikethrough = true;
+ style.ansi_term_style.is_strikethrough = true;
} else if s == "underline" {
- style.is_underline = true;
+ style.ansi_term_style.is_underline = true;
} else if !seen_foreground {
- style.foreground =
- color_from_rgb_or_ansi_code_with_default(s, foreground_default, true_color);
+ if s == "syntax" {
+ style.is_syntax_highlighted = true;
+ } else {
+ style.ansi_term_style.foreground =
+ color_from_rgb_or_ansi_code_with_default(s, foreground_default, true_color);
+ }
seen_foreground = true;
} else if !seen_background {
- style.background =
+ style.ansi_term_style.background =
color_from_rgb_or_ansi_code_with_default(s, background_default, true_color);
seen_background = true;
} else {
@@ -309,8 +313,6 @@ fn color_from_rgb_or_ansi_code_with_default(
None
} else if arg == "auto" {
default
- } else if arg == "syntax" {
- Some(style::SYNTAX_HIGHLIGHTING_COLOR)
} else {
Some(color_from_rgb_or_ansi_code(&arg, true_color))
}
diff --git a/src/delta.rs b/src/delta.rs
index 65eb1b27..2930e458 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -1,6 +1,5 @@
use std::io::Write;
-use ansi_term::Style;
use bytelines::ByteLines;
use console::strip_ansi_codes;
use std::io::BufRead;
@@ -11,6 +10,7 @@ use crate::config::Config;
use crate::draw;
use crate::paint::Painter;
use crate::parse;
+use crate::style::Style;
#[derive(Clone, Debug, PartialEq)]
pub enum State {
diff --git a/src/main.rs b/src/main.rs
index 46885dae..495f82b1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,7 @@ mod tests;
use std::io::{self, ErrorKind, Read, Write};
use std::process;
-use ansi_term::{Color, Style};
+use ansi_term::{self, Color};
use atty;
use bytelines::ByteLinesReader;
use structopt::StructOpt;
@@ -79,10 +79,13 @@ fn show_background_colors(config: &config::Config) {
--minus-emph-color=\"{minus_emph_color}\" \
--plus-color=\"{plus_color}\" \
--plus-emph-color=\"{plus_emph_color}\"",
- minus_color = get_painted_rgb_string(config.minus_style.background.unwrap()),
- minus_emph_color = get_painted_rgb_string(config.minus_emph_style.background.unwrap()),
- plus_color = get_painted_rgb_string(config.plus_style.background.unwrap()),
- plus_emph_color = get_painted_rgb_string(config.plus_emph_style.background.unwrap()),
+ minus_color =
+ get_painted_rgb_string(config.minus_style.ansi_term_style.background.unwrap()),
+ minus_emph_color =
+ get_painted_rgb_string(config.minus_emph_style.ansi_term_style.background.unwrap()),
+ plus_color = get_painted_rgb_string(config.plus_style.ansi_term_style.background.unwrap()),
+ plus_emph_color =
+ get_painted_rgb_string(config.plus_emph_style.ansi_term_style.background.unwrap()),
)
}
@@ -119,7 +122,7 @@ index f38589a..0f1bb83 100644
let stdout = io::stdout();
let mut stdout = stdout.lock();
- let style = Style::new().bold();
+ let style = ansi_term::Style::new().bold();
let assets = HighlightingAssets::new();
diff --git a/src/paint.rs b/src/paint.rs
index 3b947318..f59028b7 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -1,6 +1,6 @@
use std::io::Write;
-use ansi_term::{self, Style};
+use ansi_term;
use syntect::easy::HighlightLines;
use syntect::highlighting::Style as SyntectStyle;
use syntect::parsing::{SyntaxReference, SyntaxSet};
@@ -9,7 +9,7 @@ use crate::config;
use crate::delta::State;
use crate::edits;
use crate::paint::superimpose_style_sections::superimpose_style_sections;
-use crate::style::SyntaxHighlightable;
+use crate::style::Style;
pub const ANSI_CSI_ERASE_IN_LINE: &str = "\x1b[K";
pub const ANSI_SGR_RESET: &str = "\x1b[0m";
@@ -119,7 +119,7 @@ impl<'a> Painter<'a> {
{
let mut ansi_strings = Vec::new();
if prefix != "" {
- ansi_strings.push(default_style.paint(prefix));
+ ansi_strings.push(default_style.ansi_term_style.paint(prefix));
}
let mut dropped_prefix = prefix == ""; // TODO: Hack
for (style, mut text) in superimpose_style_sections(
@@ -134,9 +134,9 @@ impl<'a> Painter<'a> {
}
dropped_prefix = true;
}
- ansi_strings.push(style.paint(text));
+ ansi_strings.push(style.ansi_term_style.paint(text));
}
- ansi_strings.push(default_style.paint(""));
+ ansi_strings.push(default_style.ansi_term_style.paint(""));
let line = &mut ansi_term::ANSIStrings(&ansi_strings).to_string();
let background_color_extends_to_terminal_width =
match background_color_extends_to_terminal_width {
@@ -174,13 +174,13 @@ impl<'a> Painter<'a> {
}
match state {
State::HunkMinus => {
- config.minus_style.is_syntax_highlighted()
- || config.minus_emph_style.is_syntax_highlighted()
+ config.minus_style.is_syntax_highlighted
+ || config.minus_emph_style.is_syntax_highlighted
}
- State::HunkZero => config.zero_style.is_syntax_highlighted(),
+ State::HunkZero => config.zero_style.is_syntax_highlighted,
State::HunkPlus => {
- config.plus_style.is_syntax_highlighted()
- || config.plus_emph_style.is_syntax_highlighted()
+ config.plus_style.is_syntax_highlighted
+ || config.plus_emph_style.is_syntax_highlighted
}
State::HunkMeta => true,
_ => panic!(
@@ -228,11 +228,10 @@ impl<'a> Painter<'a> {
}
mod superimpose_style_sections {
- use ansi_term::Style;
use syntect::highlighting::Style as SyntectStyle;
use crate::bat::terminal::to_ansi_color;
- use crate::style::SyntaxHighlightable;
+ use crate::style::Style;
pub fn superimpose_style_sections(
sections_1: &[(SyntectStyle, &str)],
@@ -287,9 +286,12 @@ mod superimpose_style_sections {
null_syntect_style: SyntectStyle,
) -> Vec<(Style, String)> {
let make_superimposed_style = |(syntect_style, style): (SyntectStyle, Style)| {
- if style.is_syntax_highlighted() && syntect_style != null_syntect_style {
+ if style.is_syntax_highlighted && syntect_style != null_syntect_style {
Style {
- foreground: Some(to_ansi_color(syntect_style.foreground, true_color)),
+ ansi_term_style: ansi_term::Style {
+ foreground: Some(to_ansi_color(syntect_style.foreground, true_color)),
+ ..style.ansi_term_style
+ },
..style
}
} else {
@@ -328,11 +330,13 @@ mod superimpose_style_sections {
use lazy_static::lazy_static;
use super::*;
- use ansi_term::{Color, Style};
+ use ansi_term::{self, Color};
use syntect::highlighting::Color as SyntectColor;
use syntect::highlighting::FontStyle as SyntectFontStyle;
use syntect::highlighting::Style as SyntectStyle;
+ use crate::style::Style;
+
lazy_static! {
static ref SYNTAX_STYLE: SyntectStyle = SyntectStyle {
foreground: SyntectColor::BLACK,
@@ -341,26 +345,43 @@ mod superimpose_style_sections {
};
}
lazy_static! {
- static ref STYLE: Style = Style {
- foreground: Some(Color::White),
- background: Some(Color::White),
- is_underline: true,
- ..Style::new()
+ static ref SYNTAX_HIGHLIGHTED_STYLE: Style = Style {
+ ansi_term_style: ansi_term::Style {
+ foreground: Some(Color::White),
+ background: Some(Color::White),
+ is_underline: true,
+ ..ansi_term::Style::new()
+ },
+ is_syntax_highlighted: true,
+ };
+ }
+ lazy_static! {
+ static ref NON_SYNTAX_HIGHLIGHTED_STYLE: Style = Style {
+ ansi_term_style: ansi_term::Style {
+ foreground: Some(Color::White),
+ background: Some(Color::White),
+ is_underline: true,
+ ..ansi_term::Style::new()
+ },
+ is_syntax_highlighted: false,
};
}
lazy_static! {
static ref SUPERIMPOSED_STYLE: Style = Style {
- foreground: Some(to_ansi_color(SyntectColor::BLACK, true)),
- background: Some(Color::White),
- is_underline: true,
- ..Style::new()
+ ansi_term_style: ansi_term::Style {
+ foreground: Some(to_ansi_color(SyntectColor::BLACK, true)),
+ background: Some(Color::White),
+ is_underline: true,
+ ..ansi_term::Style::new()
+ },
+ is_syntax_highlighted: true,
};
}
#[test]
fn test_superimpose_style_sections_1() {
let sections_1 = vec![(*SYNTAX_STYLE, "ab")];
- let sections_2 = vec![(*STYLE, "ab")];
+ let sections_2 = vec![(*SYNTAX_HIGHLIGHTED_STYLE, "ab")];
let superimposed = vec![(*SUPERIMPOSED_STYLE, "ab".to_string())];
assert_eq!(
superimpose_style_sections(&sections_1, &sections_2, true, SyntectStyle::default()),
@@ -371,7 +392,10 @@ mod superimpose_style_sections {
#[test]
fn test_superimpose_style_sections_2() {
let sections_1 = vec![(*SYNTAX_STYLE, "ab")];
- let sections_2 = vec![(*STYLE, "a"), (*STYLE, "b")];
+ let sections_2 = vec![
+ (*SYNTAX_HIGHLIGHTED_STYLE, "a"),
+ (*SYNTAX_HIGHLIGHTED_STYLE, "b"),
+ ];
let superimposed = vec![(*SUPERIMPOSED_STYLE, String::from("ab"))];
assert_eq!(
superimpose_style_sections(&sections_1, &sections_2, true, SyntectStyle::default()),
@@ -380,6 +404,17 @@ mod superimpose_style_sections {
}
#[test]
+ fn test_superimpose_style_sections_3() {
+ let sections_1 = vec![(*SYNTAX_STYLE, "ab")];
+ let sections_2 = vec![(*NON_SYNTAX_HIGHLIGHTED_STYLE, "ab")];
+ let superimposed = vec![(*NON_SYNTAX_HIGHLIGHTED_STYLE, "ab".to_string())];
+ assert_eq!(
+ superimpose_style_sections(&sections_1, &sections_2, true, SyntectStyle::default()),
+ superimposed
+ );
+ }
+
+ #[test]
fn test_explode() {
let arbitrary = 0;
assert_eq!(
@@ -391,8 +426,11 @@ mod superimpose_style_sections {
#[test]
fn test_superimpose() {
let x = (*SYNTAX_STYLE, 'a');
- let pairs = vec![(&x, (*STYLE, 'a'))];
- assert_eq!(superimpose(pairs), vec![((*SYNTAX_STYLE, *STYLE), 'a')]);
+ let pairs = vec![(&x, (*SYNTAX_HIGHLIGHTED_STYLE, 'a'))];
+ assert_eq!(
+ superimpose(pairs),
+ vec![((*SYNTAX_STYLE, *SYNTAX_HIGHLIGHTED_STYLE), 'a')]
+ );
}
}
}
diff --git a/src/style.rs b/src/style.rs
index ede5d3da..17a265f3 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -1,4 +1,19 @@
-use ansi_term::{Color, Style};
+use ansi_term::{self, Color};
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Style {
+ pub ansi_term_style: ansi_term::Style,
+ pub is_syntax_highlighted: bool,
+}
+
+impl Style {
+ pub fn new() -> Self {
+ Self {
+ ansi_term_style: ansi_term::Style::new(),
+ is_syntax_highlighted: false,
+ }
+ }
+}
pub const LIGHT_THEMES: [&str; 5] = [
"GitHub",
@@ -86,17 +101,3 @@ const DARK_THEME_PLUS_COLOR_256: Color = Color::Fixed(22);
const DARK_THEME_PLUS_EMPH_COLOR: Color = Color::RGB(0x00, 0x60, 0x00);
const DARK_THEME_PLUS_EMPH_COLOR_256: Color = Color::Fixed(28);
-
-/// A special color value to signify that the foreground color of a style should be derived from
-/// syntax highlighting.
-pub const SYNTAX_HIGHLIGHTING_COLOR: Color = Color::White; // TODO
-
-pub trait SyntaxHighlightable {
- fn is_syntax_highlighted(&self) -> bool;
-}
-
-impl SyntaxHighlightable for Style {
- fn is_syntax_highlighted(&self) -> bool {
- self.foreground == Some(SYNTAX_HIGHLIGHTING_COLOR)
- }
-}
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs
index 7e2ce6bd..b1fc10d8 100644
--- a/src/tests/ansi_test_utils.rs
+++ b/src/tests/ansi_test_utils.rs
@@ -20,7 +20,10 @@ pub mod ansi_test_utils {
pub fn get_color_variants(string: &str, config: &Config) -> (String, String) {
let string_without_any_color = strip_ansi_codes(string).to_string();
- let string_with_plus_color_only = config.plus_style.paint(&string_without_any_color);
+ let string_with_plus_color_only = config
+ .plus_style
+ .ansi_term_style
+ .paint(&string_without_any_color);
(
string_without_any_color.to_string(),
string_with_plus_color_only.to_string(),