From 11a9144e84d135eadead0e423ba65759c8be70a7 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 16 Feb 2021 09:47:47 -0600 Subject: [PATCH] update mock table for easier table testing (#3065) --- crates/nu-table/src/main.rs | 82 ++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/crates/nu-table/src/main.rs b/crates/nu-table/src/main.rs index ba1f2ec52f..cb0f0ef45a 100644 --- a/crates/nu-table/src/main.rs +++ b/crates/nu-table/src/main.rs @@ -4,27 +4,71 @@ use std::collections::HashMap; fn main() { let args: Vec<_> = std::env::args().collect(); + // Width in terminal characters let width = args[1].parse::().expect("Need a width in columns"); - let msg = args[2..] - .iter() - .map(|x| StyledString::new(x.to_owned(), TextStyle::basic_left())) - .collect(); - - let t = Table::new( - vec![ - StyledString::new("Test me".to_owned(), TextStyle::default_header()), - StyledString::new( - "Long column \n name with carriage returns and a lot of text\n check it out" - .to_owned(), - TextStyle::default_header(), - ), - StyledString::new("Another".to_owned(), TextStyle::default_header()), - ], - vec![msg; 2], - Theme::compact(), - ); + // The mocked up table data + let (table_headers, row_data) = make_table_data(); + // The table headers + let headers = vec_of_str_to_vec_of_styledstr(&table_headers, true); + // The table rows + let rows = vec_of_str_to_vec_of_styledstr(&row_data, false); + // The table itself + let table = Table::new(headers, vec![rows; 3], Theme::rounded()); // FIXME: Config isn't available from here so just put these here to compile let color_hm: HashMap = HashMap::new(); - draw_table(&t, width, &color_hm); + // Draw the table + draw_table(&table, width, &color_hm); +} + +fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) { + let mut table_headers = vec![]; + table_headers.push("category"); + table_headers.push("description"); + table_headers.push("emoji"); + table_headers.push("ios_version"); + table_headers.push("unicode_version"); + table_headers.push("aliases"); + table_headers.push("tags"); + table_headers.push("category2"); + table_headers.push("description2"); + table_headers.push("emoji2"); + table_headers.push("ios_version2"); + table_headers.push("unicode_version2"); + table_headers.push("aliases2"); + table_headers.push("tags2"); + + let mut row_data = vec![]; + row_data.push("Smileys & Emotion"); + row_data.push("grinning face"); + row_data.push("😀"); + row_data.push("6"); + row_data.push("6.1"); + row_data.push("grinning"); + row_data.push("smile"); + row_data.push("Smileys & Emotion"); + row_data.push("grinning face"); + row_data.push("😀"); + row_data.push("6"); + row_data.push("6.1"); + row_data.push("grinning"); + row_data.push("smile"); + + (table_headers, row_data) +} + +fn vec_of_str_to_vec_of_styledstr(data: &[&str], is_header: bool) -> Vec { + let mut v = vec![]; + + for x in data.iter() { + if is_header { + v.push(StyledString::new( + String::from(*x), + TextStyle::default_header(), + )) + } else { + v.push(StyledString::new(String::from(*x), TextStyle::basic_left())) + } + } + v }