Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to anonymize line numbers #3

Merged
merged 2 commits into from Jun 13, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -37,6 +37,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
@@ -34,6 +34,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
@@ -55,6 +55,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
@@ -31,6 +31,6 @@ fn main() {
};

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
@@ -131,7 +131,7 @@ pub enum DisplayMarkType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
@@ -161,7 +161,7 @@ pub enum DisplayMarkType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
@@ -214,7 +214,7 @@ pub enum DisplayHeaderType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
@@ -236,7 +236,7 @@ pub enum DisplayHeaderType {
/// use annotate_snippets::display_list::*;
/// use annotate_snippets::formatter::DisplayListFormatter;
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
///
/// let dl = DisplayList {
/// body: vec![
@@ -20,17 +20,19 @@ fn repeat_char(c: char, n: usize) -> String {
s.repeat(n)
}

/// DisplayListFormatter' constructor accepts a single argument which
/// allows the formatter to optionally apply colors and emphasis
/// using `ansi_term` crate.
/// DisplayListFormatter' constructor accepts two arguments:
///
/// * `color` allows the formatter to optionally apply colors and emphasis
/// using the `ansi_term` crate.
/// * `anonymized_line_numbers` will replace line numbers in the left column with the text `LL`.
///
/// Example:
///
/// ```
/// use annotate_snippets::formatter::DisplayListFormatter;
/// use annotate_snippets::display_list::{DisplayList, DisplayLine, DisplaySourceLine};
///
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors, Don't anonymize line numbers
///
/// let dl = DisplayList {
/// body: vec![
@@ -48,23 +50,33 @@ fn repeat_char(c: char, n: usize) -> String {
/// ```
pub struct DisplayListFormatter {
stylesheet: Box<dyn Stylesheet>,
anonymized_line_numbers: bool,
}

impl DisplayListFormatter {
/// Constructor for the struct. The argument `color` selects
/// the stylesheet depending on the user preferences and `ansi_term`
/// crate availability.
pub fn new(color: bool) -> Self {
const ANONYMIZED_LINE_NUM: &'static str = "LL";

/// Constructor for the struct.
///
/// The argument `color` selects the stylesheet depending on the user preferences and
/// `ansi_term` crate availability.
///
/// The argument `anonymized_line_numbers` will replace line numbers in the left column with
/// the text `LL`. This can be useful to enable when running UI tests, such as in the Rust
/// test suite.
pub fn new(color: bool, anonymized_line_numbers: bool) -> Self {
if color {
Self {
#[cfg(feature = "ansi_term")]
stylesheet: Box::new(AnsiTermStylesheet {}),
#[cfg(not(feature = "ansi_term"))]
stylesheet: Box::new(NoColorStylesheet {}),
anonymized_line_numbers,
}
} else {
Self {
stylesheet: Box::new(NoColorStylesheet {}),
anonymized_line_numbers,
}
}
}
@@ -75,7 +87,13 @@ impl DisplayListFormatter {
DisplayLine::Source {
lineno: Some(lineno),
..
} => cmp::max(lineno.to_string().len(), max),
} => {
if self.anonymized_line_numbers {
Self::ANONYMIZED_LINE_NUM.len()
} else {
cmp::max(lineno.to_string().len(), max)
}
},
_ => max,
});
let inline_marks_width = dl.body.iter().fold(0, |max, line| match line {
@@ -285,7 +303,11 @@ impl DisplayListFormatter {
inline_marks,
line,
} => {
let lineno = self.format_lineno(*lineno, lineno_width);
let lineno = if self.anonymized_line_numbers {
Self::ANONYMIZED_LINE_NUM.to_string()
} else {
self.format_lineno(*lineno, lineno_width)
};
let marks = self.format_inline_marks(inline_marks, inline_marks_width);
let lf = self.format_source_line(line);
let lineno_color = self.stylesheet.get_style(StyleClass::LineNo);
@@ -46,7 +46,7 @@ fn test_fixtures() {
let expected_out = read_file(&path_out).expect("Failed to read file");

let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true);
let dlf = DisplayListFormatter::new(true, false);
let actual_out = dlf.format(&dl);
println!("{}", expected_out);
println!("{}", actual_out.trim_end());
@@ -11,7 +11,7 @@ fn test_source_empty() {
line: DisplaySourceLine::Empty,
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " |");
}
@@ -37,7 +37,7 @@ fn test_source_content() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -65,7 +65,7 @@ fn test_source_annotation_standalone_singleline() {
},
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | ^^^^^ Example string");
}
@@ -109,7 +109,7 @@ fn test_source_annotation_standalone_multiline() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -241,7 +241,7 @@ fn test_source_annotation_standalone_multi_annotation() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | ----- info: Example string\n | Second line\n | warning: This is a note\n | Second line of the warning\n | ----- info: This is an info\n | ----- help: This is help\n | This is an annotation of type none");
}
@@ -270,7 +270,7 @@ fn test_fold_line() {
},
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -286,7 +286,7 @@ fn test_raw_origin_initial_nopos() {
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "--> src/test.rs");
}
@@ -299,7 +299,7 @@ fn test_raw_origin_initial_pos() {
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
}
@@ -312,7 +312,7 @@ fn test_raw_origin_continuation() {
header_type: DisplayHeaderType::Continuation,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "::: src/test.rs:23:15");
}
@@ -332,7 +332,7 @@ fn test_raw_annotation_unaligned() {
continuation: false,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), "error[E0001]: This is an error");
}
@@ -366,7 +366,7 @@ fn test_raw_annotation_unaligned_multiline() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -389,7 +389,7 @@ fn test_raw_annotation_aligned() {
continuation: false,
})]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " = error[E0001]: This is an error");
}
@@ -423,7 +423,7 @@ fn test_raw_annotation_aligned_multiline() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -472,7 +472,7 @@ fn test_different_annotation_types() {
}),
]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(
dlf.format(&dl),
@@ -491,7 +491,50 @@ fn test_inline_marks_empty_line() {
line: DisplaySourceLine::Empty,
}]);

let dlf = DisplayListFormatter::new(false);
let dlf = DisplayListFormatter::new(false, false);

assert_eq!(dlf.format(&dl), " | |",);
}

#[test]
fn test_anon_lines() {
let dl = DisplayList::from(vec![
DisplayLine::Source {
lineno: Some(56),
inline_marks: vec![],
line: DisplaySourceLine::Content {
text: "This is an example".to_string(),
range: (0, 19),
},
},
DisplayLine::Source {
lineno: Some(57),
inline_marks: vec![],
line: DisplaySourceLine::Content {
text: "of content lines".to_string(),
range: (0, 19),
},
},
]);

let dlf = DisplayListFormatter::new(false, true);

assert_eq!(
dlf.format(&dl),
"LL | This is an example\nLL | of content lines"
);
}

#[test]
fn test_raw_origin_initial_pos_anon_lines() {
let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
path: "src/test.rs".to_string(),
pos: Some((23, 15)),
header_type: DisplayHeaderType::Initial,
})]);

let dlf = DisplayListFormatter::new(false, true);

// Using anonymized_line_numbers should not affect the inital position
assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.