Skip to content

Commit

Permalink
Add option to anonymize line numbers (#3)
Browse files Browse the repository at this point in the history
Add option to anonymize line numbers
  • Loading branch information
oli-obk authored Jun 13, 2019
2 parents 6543d2a + d6c36a6 commit d62b321
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/expected_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion examples/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion examples/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion examples/multislice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
8 changes: 4 additions & 4 deletions src/display_list/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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![
Expand All @@ -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![
Expand Down
42 changes: 32 additions & 10 deletions src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand All @@ -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,
}
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
73 changes: 58 additions & 15 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), " |");
}
Expand All @@ -37,7 +37,7 @@ fn test_source_content() {
},
]);

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

assert_eq!(
dlf.format(&dl),
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -270,7 +270,7 @@ fn test_fold_line() {
},
]);

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

assert_eq!(
dlf.format(&dl),
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -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),
Expand All @@ -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");
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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");
}

0 comments on commit d62b321

Please sign in to comment.