From f135ad19b0f24b875f5ae3305712da771aeee997 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Wed, 29 Jul 2020 23:20:08 -0400 Subject: [PATCH] Create ansi module --- src/ansi.rs | 3 +++ src/main.rs | 1 + src/paint.rs | 15 ++++++--------- src/tests/test_example_diffs.rs | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 src/ansi.rs diff --git a/src/ansi.rs b/src/ansi.rs new file mode 100644 index 000000000..01b2e0e6c --- /dev/null +++ b/src/ansi.rs @@ -0,0 +1,3 @@ +pub const ANSI_CSI_CLEAR_TO_EOL: &str = "\x1b[0K"; +pub const ANSI_CSI_CLEAR_TO_BOL: &str = "\x1b[1K"; +pub const ANSI_SGR_RESET: &str = "\x1b[0m"; diff --git a/src/main.rs b/src/main.rs index 689543265..b2e842d2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate bitflags; extern crate error_chain; mod align; +mod ansi; mod bat; mod cli; mod color; diff --git a/src/paint.rs b/src/paint.rs index 713f8f43c..da158326e 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -9,6 +9,7 @@ use syntect::highlighting::Style as SyntectStyle; use syntect::parsing::{SyntaxReference, SyntaxSet}; use unicode_segmentation::UnicodeSegmentation; +use crate::ansi; use crate::config::{self, delta_unreachable}; use crate::delta::State; use crate::edits; @@ -17,10 +18,6 @@ use crate::features::side_by_side; use crate::paint::superimpose_style_sections::superimpose_style_sections; use crate::style::Style; -pub const ANSI_CSI_CLEAR_TO_EOL: &str = "\x1b[0K"; -pub const ANSI_CSI_CLEAR_TO_BOL: &str = "\x1b[1K"; -pub const ANSI_SGR_RESET: &str = "\x1b[0m"; - pub struct Painter<'a> { pub minus_lines: Vec, pub plus_lines: Vec, @@ -318,12 +315,12 @@ impl<'a> Painter<'a> { line.push_str(&ansi_term::ANSIStrings(&[fill_style.paint("")]).to_string()); if line .to_lowercase() - .ends_with(&ANSI_SGR_RESET.to_lowercase()) + .ends_with(&ansi::ANSI_SGR_RESET.to_lowercase()) { - line.truncate(line.len() - ANSI_SGR_RESET.len()); + line.truncate(line.len() - ansi::ANSI_SGR_RESET.len()); } - line.push_str(ANSI_CSI_CLEAR_TO_EOL); - line.push_str(ANSI_SGR_RESET); + line.push_str(ansi::ANSI_CSI_CLEAR_TO_EOL); + line.push_str(ansi::ANSI_SGR_RESET); } /// Use ANSI sequences to visually mark the current line as empty. If `marker` is None then the @@ -334,7 +331,7 @@ impl<'a> Painter<'a> { pub fn mark_empty_line(empty_line_style: &Style, line: &mut String, marker: Option<&str>) { line.push_str( &empty_line_style - .paint(marker.unwrap_or(ANSI_CSI_CLEAR_TO_BOL)) + .paint(marker.unwrap_or(ansi::ANSI_CSI_CLEAR_TO_BOL)) .to_string(), ); } diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index d1392c692..f148ada1b 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -2,7 +2,7 @@ mod tests { use console::strip_ansi_codes; - use crate::paint; + use crate::ansi; use crate::style; use crate::tests::ansi_test_utils::ansi_test_utils; use crate::tests::integration_test_utils::integration_test_utils; @@ -1090,7 +1090,7 @@ impl<'a> Alignment<'a> { │ line, &style .ansi_term_style - .paint(paint::ANSI_CSI_CLEAR_TO_EOL) + .paint(ansi::ANSI_CSI_CLEAR_TO_EOL) .to_string() ); } else { @@ -1099,7 +1099,7 @@ impl<'a> Alignment<'a> { │ line, &style .ansi_term_style - .paint(paint::ANSI_CSI_CLEAR_TO_BOL) + .paint(ansi::ANSI_CSI_CLEAR_TO_BOL) .to_string() ); }