Skip to content

Commit

Permalink
Create ansi module
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jul 30, 2020
1 parent a2e4702 commit f135ad1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -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";
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate bitflags;
extern crate error_chain;

mod align;
mod ansi;
mod bat;
mod cli;
mod color;
Expand Down
15 changes: 6 additions & 9 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String>,
pub plus_lines: Vec<String>,
Expand Down Expand Up @@ -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
Expand All @@ -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(),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
);
}
Expand Down

0 comments on commit f135ad1

Please sign in to comment.