Skip to content

Commit fdf456e

Browse files
refactor: 💡 render notes (#11)
1 parent b7a1ffc commit fdf456e

File tree

1 file changed

+85
-66
lines changed

1 file changed

+85
-66
lines changed

‎src/write.rs‎

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -992,69 +992,7 @@ impl<S: Span> Report<'_, S> {
992992

993993
// Note
994994
if is_final_group {
995-
for (i, note) in self.notes.iter().enumerate() {
996-
if !self.config.compact {
997-
Self::write_simple_margin(
998-
&mut w,
999-
&SimpleMarginWriteCtx {
1000-
idx: 0,
1001-
is_line: false,
1002-
is_ellipsis: false,
1003-
line_no_width,
1004-
draw: &draw,
1005-
config: &self.config,
1006-
s,
1007-
},
1008-
)?;
1009-
writeln!(w)?;
1010-
}
1011-
let note_prefix = format!("{} {}", "Note", i + 1);
1012-
let note_prefix_len = if self.notes.len() > 1 {
1013-
note_prefix.len()
1014-
} else {
1015-
4
1016-
};
1017-
let mut lines = note.lines();
1018-
if let Some(line) = lines.next() {
1019-
Self::write_simple_margin(
1020-
&mut w,
1021-
&SimpleMarginWriteCtx {
1022-
idx: 0,
1023-
is_line: false,
1024-
is_ellipsis: false,
1025-
line_no_width,
1026-
draw: &draw,
1027-
config: &self.config,
1028-
s,
1029-
},
1030-
)?;
1031-
if self.notes.len() > 1 {
1032-
writeln!(
1033-
w,
1034-
"{}: {}",
1035-
note_prefix.fg(self.config.note_color(), s),
1036-
line
1037-
)?;
1038-
} else {
1039-
writeln!(w, "{}: {}", "Note".fg(self.config.note_color(), s), line)?;
1040-
}
1041-
}
1042-
for line in lines {
1043-
Self::write_simple_margin(
1044-
&mut w,
1045-
&SimpleMarginWriteCtx {
1046-
idx: 0,
1047-
is_line: false,
1048-
is_ellipsis: false,
1049-
line_no_width,
1050-
draw: &draw,
1051-
config: &self.config,
1052-
s,
1053-
},
1054-
)?;
1055-
writeln!(w, "{:>pad$}{}", "", line, pad = note_prefix_len + 2)?;
1056-
}
1057-
}
995+
self.render_notes(&mut w, s, line_no_width, &draw)?;
1058996
}
1059997

1060998
// Tail of report
@@ -1076,11 +1014,85 @@ impl<S: Span> Report<'_, S> {
10761014

10771015
if groups_len == 0 {
10781016
self.render_help(&mut w, s, line_no_width, &draw)?;
1017+
self.render_notes(&mut w, s, line_no_width, &draw)?;
10791018
}
10801019

10811020
Ok(())
10821021
}
10831022

1023+
fn render_notes<W: Write>(
1024+
&self,
1025+
w: &mut W,
1026+
s: StreamType,
1027+
line_no_width: usize,
1028+
draw: &draw::Characters,
1029+
) -> Result<(), io::Error> {
1030+
for (i, note) in self.notes.iter().enumerate() {
1031+
if !self.config.compact {
1032+
Self::write_simple_margin(
1033+
w,
1034+
&SimpleMarginWriteCtx {
1035+
idx: 0,
1036+
is_line: false,
1037+
is_ellipsis: false,
1038+
line_no_width,
1039+
draw,
1040+
config: &self.config,
1041+
s,
1042+
},
1043+
)?;
1044+
writeln!(w)?;
1045+
}
1046+
let note_prefix = format!("{} {}", "Note", i + 1);
1047+
let note_prefix_len = if self.notes.len() > 1 {
1048+
note_prefix.len()
1049+
} else {
1050+
4
1051+
};
1052+
let mut lines = note.lines();
1053+
if let Some(line) = lines.next() {
1054+
Self::write_simple_margin(
1055+
w,
1056+
&SimpleMarginWriteCtx {
1057+
idx: 0,
1058+
is_line: false,
1059+
is_ellipsis: false,
1060+
line_no_width,
1061+
draw,
1062+
config: &self.config,
1063+
s,
1064+
},
1065+
)?;
1066+
if self.notes.len() > 1 {
1067+
writeln!(
1068+
w,
1069+
"{}: {}",
1070+
note_prefix.fg(self.config.note_color(), s),
1071+
line
1072+
)?;
1073+
} else {
1074+
writeln!(w, "{}: {}", "Note".fg(self.config.note_color(), s), line)?;
1075+
}
1076+
}
1077+
for line in lines {
1078+
Self::write_simple_margin(
1079+
w,
1080+
&SimpleMarginWriteCtx {
1081+
idx: 0,
1082+
is_line: false,
1083+
is_ellipsis: false,
1084+
line_no_width,
1085+
draw,
1086+
config: &self.config,
1087+
s,
1088+
},
1089+
)?;
1090+
writeln!(w, "{:>pad$}{}", "", line, pad = note_prefix_len + 2)?;
1091+
}
1092+
}
1093+
Ok(())
1094+
}
1095+
10841096
fn render_help<W: Write>(
10851097
&self,
10861098
w: &mut W,
@@ -1820,14 +1832,16 @@ mod tests {
18201832
}
18211833

18221834
#[test]
1823-
fn zero_label_help_message() {
1835+
fn zero_label_help_and_note_message() {
18241836
let source = "apple == orange;";
18251837
let msg = remove_trailing(
18261838
Report::build(ReportKind::Error, 0..0)
18271839
.with_config(no_color_and_ascii())
18281840
.with_message("can't compare apples with oranges")
18291841
.with_help("No need to try, they can't be compared.")
1830-
.with_help("Yeah, really, please stop.\nIt has no resemblance.")
1842+
.with_help("No need to try, they can't be compared2.")
1843+
.with_note("Yeah, really, please stop.\nIt has no resemblance.")
1844+
.with_note("Yeah, really, please stop.\nIt has no resemblance2.")
18311845
.finish()
18321846
.write_to_string(Source::from(source)),
18331847
);
@@ -1836,8 +1850,13 @@ mod tests {
18361850
|
18371851
| Help 1: No need to try, they can't be compared.
18381852
|
1839-
| Help 2: Yeah, really, please stop.
1853+
| Help 2: No need to try, they can't be compared2.
1854+
|
1855+
| Note 1: Yeah, really, please stop.
18401856
| It has no resemblance.
1857+
|
1858+
| Note 2: Yeah, really, please stop.
1859+
| It has no resemblance2.
18411860
")
18421861
}
18431862
}

0 commit comments

Comments
 (0)