Fix to md errors (#2729)

* Fix to md errors

* Fix variable name and avoid typecasts
This commit is contained in:
Joseph T. Lyons 2020-11-06 12:40:53 -05:00 committed by GitHub
parent 80b39454ff
commit 0ee054b14d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 7 deletions

View File

@ -70,6 +70,8 @@ async fn to_md(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
column_widths.push(escaped_header_string.len());
escaped_headers.push(escaped_header_string);
}
} else {
column_widths = vec![0; headers.len()]
}
let mut escaped_rows: Vec<Vec<String>> = Vec::new();
@ -101,7 +103,15 @@ async fn to_md(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
escaped_rows.push(escaped_row);
}
let output_string = get_output_string(&escaped_headers, &escaped_rows, &column_widths, pretty);
let output_string = if (column_widths.is_empty() || column_widths.iter().all(|x| *x == 0))
&& escaped_rows.is_empty()
{
String::from("")
} else {
get_output_string(&escaped_headers, &escaped_rows, &column_widths, pretty)
.trim()
.to_string()
};
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(output_string).into_value(name_tag),
@ -183,12 +193,16 @@ fn get_output_string(
}
fn get_padded_string(text: String, desired_length: usize, padding_character: char) -> 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)
)
}

View File

@ -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#"