Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d62b321

Browse files
authored
Add option to anonymize line numbers (#3)
Add option to anonymize line numbers
2 parents 6543d2a + d6c36a6 commit d62b321

File tree

8 files changed

+99
-34
lines changed

8 files changed

+99
-34
lines changed

‎examples/expected_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ fn main() {
3737
};
3838

3939
let dl = DisplayList::from(snippet);
40-
let dlf = DisplayListFormatter::new(true);
40+
let dlf = DisplayListFormatter::new(true,false);
4141
println!("{}", dlf.format(&dl));
4242
}

‎examples/footer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ fn main() {
3434
};
3535

3636
let dl = DisplayList::from(snippet);
37-
let dlf = DisplayListFormatter::new(true);
37+
let dlf = DisplayListFormatter::new(true,false);
3838
println!("{}", dlf.format(&dl));
3939
}

‎examples/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ fn main() {
5555
};
5656

5757
let dl = DisplayList::from(snippet);
58-
let dlf = DisplayListFormatter::new(true);
58+
let dlf = DisplayListFormatter::new(true,false);
5959
println!("{}", dlf.format(&dl));
6060
}

‎examples/multislice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ fn main() {
3131
};
3232

3333
let dl = DisplayList::from(snippet);
34-
let dlf = DisplayListFormatter::new(true);
34+
let dlf = DisplayListFormatter::new(true,false);
3535
println!("{}", dlf.format(&dl));
3636
}

‎src/display_list/structs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub enum DisplayMarkType {
131131
/// use annotate_snippets::display_list::*;
132132
/// use annotate_snippets::formatter::DisplayListFormatter;
133133
///
134-
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
134+
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
135135
///
136136
/// let dl = DisplayList {
137137
/// body: vec![
@@ -161,7 +161,7 @@ pub enum DisplayMarkType {
161161
/// use annotate_snippets::display_list::*;
162162
/// use annotate_snippets::formatter::DisplayListFormatter;
163163
///
164-
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
164+
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
165165
///
166166
/// let dl = DisplayList {
167167
/// body: vec![
@@ -214,7 +214,7 @@ pub enum DisplayHeaderType {
214214
/// use annotate_snippets::display_list::*;
215215
/// use annotate_snippets::formatter::DisplayListFormatter;
216216
///
217-
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
217+
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
218218
///
219219
/// let dl = DisplayList {
220220
/// body: vec![
@@ -236,7 +236,7 @@ pub enum DisplayHeaderType {
236236
/// use annotate_snippets::display_list::*;
237237
/// use annotate_snippets::formatter::DisplayListFormatter;
238238
///
239-
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
239+
/// let dlf = DisplayListFormatter::new(false, false); // Don't use colors
240240
///
241241
/// let dl = DisplayList {
242242
/// body: vec![

‎src/formatter/mod.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ fn repeat_char(c: char, n: usize) -> String {
2020
s.repeat(n)
2121
}
2222

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

5356
impl DisplayListFormatter {
54-
/// Constructor for the struct. The argument `color` selects
55-
/// the stylesheet depending on the user preferences and `ansi_term`
56-
/// crate availability.
57-
pub fn new(color: bool) -> Self {
57+
const ANONYMIZED_LINE_NUM: &'static str = "LL";
58+
59+
/// Constructor for the struct.
60+
///
61+
/// The argument `color` selects the stylesheet depending on the user preferences and
62+
/// `ansi_term` crate availability.
63+
///
64+
/// The argument `anonymized_line_numbers` will replace line numbers in the left column with
65+
/// the text `LL`. This can be useful to enable when running UI tests, such as in the Rust
66+
/// test suite.
67+
pub fn new(color: bool, anonymized_line_numbers: bool) -> Self {
5868
if color {
5969
Self {
6070
#[cfg(feature = "ansi_term")]
6171
stylesheet: Box::new(AnsiTermStylesheet {}),
6272
#[cfg(not(feature = "ansi_term"))]
6373
stylesheet: Box::new(NoColorStylesheet {}),
74+
anonymized_line_numbers,
6475
}
6576
} else {
6677
Self {
6778
stylesheet: Box::new(NoColorStylesheet {}),
79+
anonymized_line_numbers,
6880
}
6981
}
7082
}
@@ -75,7 +87,13 @@ impl DisplayListFormatter {
7587
DisplayLine::Source {
7688
lineno: Some(lineno),
7789
..
78-
} => cmp::max(lineno.to_string().len(), max),
90+
} => {
91+
if self.anonymized_line_numbers {
92+
Self::ANONYMIZED_LINE_NUM.len()
93+
} else {
94+
cmp::max(lineno.to_string().len(), max)
95+
}
96+
},
7997
_ => max,
8098
});
8199
let inline_marks_width = dl.body.iter().fold(0, |max, line| match line {
@@ -285,7 +303,11 @@ impl DisplayListFormatter {
285303
inline_marks,
286304
line,
287305
} => {
288-
let lineno = self.format_lineno(*lineno, lineno_width);
306+
let lineno = if self.anonymized_line_numbers {
307+
Self::ANONYMIZED_LINE_NUM.to_string()
308+
} else {
309+
self.format_lineno(*lineno, lineno_width)
310+
};
289311
let marks = self.format_inline_marks(inline_marks, inline_marks_width);
290312
let lf = self.format_source_line(line);
291313
let lineno_color = self.stylesheet.get_style(StyleClass::LineNo);

‎tests/fixtures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn test_fixtures() {
4646
let expected_out = read_file(&path_out).expect("Failed to read file");
4747

4848
let dl = DisplayList::from(snippet);
49-
let dlf = DisplayListFormatter::new(true);
49+
let dlf = DisplayListFormatter::new(true,false);
5050
let actual_out = dlf.format(&dl);
5151
println!("{}", expected_out);
5252
println!("{}", actual_out.trim_end());

‎tests/formatter.rs

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn test_source_empty() {
1111
line: DisplaySourceLine::Empty,
1212
}]);
1313

14-
let dlf = DisplayListFormatter::new(false);
14+
let dlf = DisplayListFormatter::new(false,false);
1515

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

40-
let dlf = DisplayListFormatter::new(false);
40+
let dlf = DisplayListFormatter::new(false,false);
4141

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

68-
let dlf = DisplayListFormatter::new(false);
68+
let dlf = DisplayListFormatter::new(false,false);
6969

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

112-
let dlf = DisplayListFormatter::new(false);
112+
let dlf = DisplayListFormatter::new(false,false);
113113

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

244-
let dlf = DisplayListFormatter::new(false);
244+
let dlf = DisplayListFormatter::new(false,false);
245245

246246
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");
247247
}
@@ -270,7 +270,7 @@ fn test_fold_line() {
270270
},
271271
]);
272272

273-
let dlf = DisplayListFormatter::new(false);
273+
let dlf = DisplayListFormatter::new(false,false);
274274

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

289-
let dlf = DisplayListFormatter::new(false);
289+
let dlf = DisplayListFormatter::new(false,false);
290290

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

302-
let dlf = DisplayListFormatter::new(false);
302+
let dlf = DisplayListFormatter::new(false,false);
303303

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

315-
let dlf = DisplayListFormatter::new(false);
315+
let dlf = DisplayListFormatter::new(false,false);
316316

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

335-
let dlf = DisplayListFormatter::new(false);
335+
let dlf = DisplayListFormatter::new(false,false);
336336

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

369-
let dlf = DisplayListFormatter::new(false);
369+
let dlf = DisplayListFormatter::new(false,false);
370370

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

392-
let dlf = DisplayListFormatter::new(false);
392+
let dlf = DisplayListFormatter::new(false,false);
393393

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

426-
let dlf = DisplayListFormatter::new(false);
426+
let dlf = DisplayListFormatter::new(false,false);
427427

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

475-
let dlf = DisplayListFormatter::new(false);
475+
let dlf = DisplayListFormatter::new(false,false);
476476

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

494-
let dlf = DisplayListFormatter::new(false);
494+
let dlf = DisplayListFormatter::new(false,false);
495495

496496
assert_eq!(dlf.format(&dl), " | |",);
497497
}
498+
499+
#[test]
500+
fn test_anon_lines() {
501+
let dl = DisplayList::from(vec![
502+
DisplayLine::Source {
503+
lineno: Some(56),
504+
inline_marks: vec![],
505+
line: DisplaySourceLine::Content {
506+
text: "This is an example".to_string(),
507+
range: (0, 19),
508+
},
509+
},
510+
DisplayLine::Source {
511+
lineno: Some(57),
512+
inline_marks: vec![],
513+
line: DisplaySourceLine::Content {
514+
text: "of content lines".to_string(),
515+
range: (0, 19),
516+
},
517+
},
518+
]);
519+
520+
let dlf = DisplayListFormatter::new(false, true);
521+
522+
assert_eq!(
523+
dlf.format(&dl),
524+
"LL | This is an example\nLL | of content lines"
525+
);
526+
}
527+
528+
#[test]
529+
fn test_raw_origin_initial_pos_anon_lines() {
530+
let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
531+
path: "src/test.rs".to_string(),
532+
pos: Some((23, 15)),
533+
header_type: DisplayHeaderType::Initial,
534+
})]);
535+
536+
let dlf = DisplayListFormatter::new(false, true);
537+
538+
// Using anonymized_line_numbers should not affect the inital position
539+
assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
540+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /