From 0ee054b14d9e4facbce8956ffbdf4e0184201e57 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Fri, 6 Nov 2020 12:40:53 -0500 Subject: [PATCH] Fix `to md` errors (#2729) * Fix to md errors * Fix variable name and avoid typecasts --- crates/nu-cli/src/commands/to_md.rs | 22 ++++++++-- .../tests/format_conversions/markdown.rs | 42 +++++++++++++++++-- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/crates/nu-cli/src/commands/to_md.rs b/crates/nu-cli/src/commands/to_md.rs index 7db240b4c..3b810c73b 100644 --- a/crates/nu-cli/src/commands/to_md.rs +++ b/crates/nu-cli/src/commands/to_md.rs @@ -70,6 +70,8 @@ async fn to_md(args: CommandArgs, registry: &CommandRegistry) -> Result> = Vec::new(); @@ -101,7 +103,15 @@ async fn to_md(args: CommandArgs, registry: &CommandRegistry) -> Result String { + let repeat_length = if text.len() > desired_length { + 0 + } else { + desired_length - text.len() + }; + format!( "{}{}", text, - padding_character - .to_string() - .repeat(desired_length - text.len()) + padding_character.to_string().repeat(repeat_length) ) } diff --git a/crates/nu-cli/tests/format_conversions/markdown.rs b/crates/nu-cli/tests/format_conversions/markdown.rs index 5f0888b55..5816f3bd4 100644 --- a/crates/nu-cli/tests/format_conversions/markdown.rs +++ b/crates/nu-cli/tests/format_conversions/markdown.rs @@ -1,7 +1,31 @@ use nu_test_support::{nu, pipeline}; #[test] -fn out_md_simple() { +fn md_empty() { + let actual = nu!( + cwd: ".", pipeline( + r#" + echo "{}" | from json | to md + "# + )); + + assert_eq!(actual.out, ""); +} + +#[test] +fn md_empty_pretty() { + let actual = nu!( + cwd: ".", pipeline( + r#" + echo "{}" | from json | to md -p + "# + )); + + assert_eq!(actual.out, ""); +} + +#[test] +fn md_simple() { let actual = nu!( cwd: ".", pipeline( r#" @@ -13,7 +37,19 @@ fn out_md_simple() { } #[test] -fn out_md_table() { +fn md_simple_pretty() { + let actual = nu!( + cwd: ".", pipeline( + r#" + echo 3 | to md -p + "# + )); + + assert_eq!(actual.out, "3"); +} + +#[test] +fn md_table() { let actual = nu!( cwd: ".", pipeline( r#" @@ -25,7 +61,7 @@ fn out_md_table() { } #[test] -fn out_md_table_pretty() { +fn md_table_pretty() { let actual = nu!( cwd: ".", pipeline( r#"