Skip to content

Commit

Permalink
fix(cli/fmt): make fmt output more readable (denoland#7534)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored Sep 18, 2020
1 parent 525012b commit 581fd0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cli/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ pub fn white_on_green(s: &str) -> impl fmt::Display {
style(&s, style_spec)
}

pub fn black_on_green(s: &str) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_bg(Some(Green)).set_fg(Some(Black));
style(&s, style_spec)
}

pub fn yellow(s: &str) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Yellow));
Expand Down
14 changes: 7 additions & 7 deletions cli/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn fmt_add_text(x: &str) -> String {
}

fn fmt_add_text_highlight(x: &str) -> String {
format!("{}", colors::white_on_green(x))
format!("{}", colors::black_on_green(x))
}

fn fmt_rem() -> String {
Expand All @@ -41,9 +41,9 @@ fn write_line_diff(
for (i, s) in split {
write!(
diff,
"{:0width$}{} ",
"{:width$}{} ",
*orig_line + i,
colors::gray("|"),
colors::gray(" |"),
width = line_number_width
)?;
write!(diff, "{}", fmt_rem())?;
Expand All @@ -55,9 +55,9 @@ fn write_line_diff(
for (i, s) in split {
write!(
diff,
"{:0width$}{} ",
"{:width$}{} ",
*edit_line + i,
colors::gray("|"),
colors::gray(" |"),
width = line_number_width
)?;
write!(diff, "{}", fmt_add())?;
Expand Down Expand Up @@ -160,13 +160,13 @@ fn test_diff() {
colors::strip_ansi_codes(
&diff(simple_console_log_unfmt, simple_console_log_fmt).unwrap()
),
"1| -console.log('Hello World')\n1| +console.log(\"Hello World\");\n"
"1 | -console.log('Hello World')\n1 | +console.log(\"Hello World\");\n"
);

let line_number_unfmt = "\n\n\n\nconsole.log(\n'Hello World'\n)";
let line_number_fmt = "console.log(\n\"Hello World\"\n);";
assert_eq!(
colors::strip_ansi_codes(&diff(line_number_unfmt, line_number_fmt).unwrap()),
"1| -\n2| -\n3| -\n4| -\n5| -console.log(\n1| +console.log(\n6| -'Hello World'\n2| +\"Hello World\"\n7| -)\n3| +);\n"
"1 | -\n2 | -\n3 | -\n4 | -\n5 | -console.log(\n1 | +console.log(\n6 | -'Hello World'\n2 | +\"Hello World\"\n7 | -)\n3 | +);\n"
)
}

0 comments on commit 581fd0f

Please sign in to comment.