From f24877ba081f754251188f902f1bed18aa270bcf Mon Sep 17 00:00:00 2001 From: Panagiotis Ganelis <50522617+PanGan21@users.noreply.github.com> Date: Thu, 29 Feb 2024 03:44:11 +0100 Subject: [PATCH] fix: process empty headers in `to md` command (#12012) fixes #12006 # Description Process empty headers as well in `to md` command. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/formats/to/md.rs | 44 +++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/formats/to/md.rs b/crates/nu-command/src/formats/to/md.rs index 25b3237290..e80f3aae39 100644 --- a/crates/nu-command/src/formats/to/md.rs +++ b/crates/nu-command/src/formats/to/md.rs @@ -151,7 +151,21 @@ fn collect_headers(headers: &[String]) -> (Vec, Vec) { fn table(input: PipelineData, pretty: bool, config: &Config) -> String { let vec_of_values = input.into_iter().collect::>(); - let headers = merge_descriptors(&vec_of_values); + let mut headers = merge_descriptors(&vec_of_values); + + let mut empty_header_index = 0; + for value in &vec_of_values { + if let Value::Record { val, .. } = value { + for column in val.columns() { + if column.is_empty() && !headers.contains(&String::new()) { + headers.insert(empty_header_index, String::new()); + empty_header_index += 1; + break; + } + empty_header_index += 1; + } + } + } let (escaped_headers, mut column_widths) = collect_headers(&headers); @@ -416,4 +430,32 @@ mod tests { "#) ); } + + #[test] + fn test_empty_column_header() { + let value = Value::test_list(vec![ + Value::test_record(record! { + "" => Value::test_string("1"), + "foo" => Value::test_string("2"), + }), + Value::test_record(record! { + "" => Value::test_string("3"), + "foo" => Value::test_string("4"), + }), + ]); + + assert_eq!( + table( + value.clone().into_pipeline_data(), + false, + &Config::default() + ), + one(r#" + ||foo| + |-|-| + |1|2| + |3|4| + "#) + ); + } }