From 42113a767a1bb5300e6f31731515b356041f145e Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 8 Oct 2021 10:15:07 -0500 Subject: [PATCH] allow one to specify a custom separator --- crates/nu-command/src/viewers/griddle.rs | 55 ++++++++++++++++++------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 42a0e58f33..55ad98c4e7 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -28,6 +28,12 @@ impl Command for Griddle { Some('w'), ) .switch("color", "draw output with color", Some('c')) + .named( + "separator", + SyntaxShape::String, + "character to separate grid with", + Some('s'), + ) } fn extra_usage(&self) -> &str { @@ -47,13 +53,20 @@ prints out the list properly."# ) -> Result { let width_param: Option = call.get_flag(context, "width")?; let color_param: bool = call.has_flag("color"); + let seperator_param: Option = call.get_flag(context, "separator")?; match input { Value::List { vals, .. } => { // dbg!("value::list"); let data = convert_to_list2(vals); if let Some(items) = data { - Ok(create_grid_output2(items, call, width_param, color_param)) + Ok(create_grid_output2( + items, + call, + width_param, + color_param, + seperator_param, + )) } else { Ok(Value::Nothing { span: call.head }) } @@ -62,7 +75,13 @@ prints out the list properly."# // dbg!("value::stream"); let data = convert_to_list2(stream); if let Some(items) = data { - Ok(create_grid_output2(items, call, width_param, color_param)) + Ok(create_grid_output2( + items, + call, + width_param, + color_param, + seperator_param, + )) } else { // dbg!(data); Ok(Value::Nothing { span: call.head }) @@ -76,7 +95,13 @@ prints out the list properly."# items.push((i, c, v.into_string())) } - Ok(create_grid_output2(items, call, width_param, color_param)) + Ok(create_grid_output2( + items, + call, + width_param, + color_param, + seperator_param, + )) } x => { // dbg!("other value"); @@ -92,11 +117,25 @@ fn create_grid_output2( call: &Call, width_param: Option, color_param: bool, + separator_param: Option, ) -> Value { let ls_colors = LsColors::from_env().unwrap_or_default(); + let cols = if let Some(col) = width_param { + col.parse::().unwrap_or(80) + } else if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() { + w + } else { + 80u16 + }; + let sep = if let Some(seperator) = separator_param { + seperator + } else { + " │ ".to_string() + }; + let mut grid = Grid::new(GridOptions { direction: Direction::TopToBottom, - filling: Filling::Text(" | ".into()), + filling: Filling::Text(sep), }); for (_row_index, header, value) in items { @@ -116,14 +155,6 @@ fn create_grid_output2( } } - let cols = if let Some(col) = width_param { - col.parse::().unwrap_or(80) - } else if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() { - w - } else { - 80u16 - }; - if let Some(grid_display) = grid.fit_into_width(cols as usize) { Value::String { val: grid_display.to_string(),