From c636c30a19a17f440fb31445a50af0af8838b903 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 8 Oct 2021 09:53:26 -0500 Subject: [PATCH] added a switch to enable coloring --- crates/nu-command/src/viewers/griddle.rs | 44 +++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 77cdb92e8f..42a0e58f33 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -20,12 +20,14 @@ impl Command for Griddle { } fn signature(&self) -> nu_protocol::Signature { - Signature::build("grid").named( - "columns", - SyntaxShape::Int, - "number of columns wide", - Some('c'), - ) + Signature::build("grid") + .named( + "width", + SyntaxShape::Int, + "number of columns wide", + Some('w'), + ) + .switch("color", "draw output with color", Some('c')) } fn extra_usage(&self) -> &str { @@ -43,14 +45,15 @@ prints out the list properly."# call: &Call, input: Value, ) -> Result { - let columns_param: Option = call.get_flag(context, "columns")?; + let width_param: Option = call.get_flag(context, "width")?; + let color_param: bool = call.has_flag("color"); 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, columns_param)) + Ok(create_grid_output2(items, call, width_param, color_param)) } else { Ok(Value::Nothing { span: call.head }) } @@ -59,7 +62,7 @@ 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, columns_param)) + Ok(create_grid_output2(items, call, width_param, color_param)) } else { // dbg!(data); Ok(Value::Nothing { span: call.head }) @@ -73,7 +76,7 @@ prints out the list properly."# items.push((i, c, v.into_string())) } - Ok(create_grid_output2(items, call, columns_param)) + Ok(create_grid_output2(items, call, width_param, color_param)) } x => { // dbg!("other value"); @@ -87,7 +90,8 @@ prints out the list properly."# fn create_grid_output2( items: Vec<(usize, String, String)>, call: &Call, - columns_param: Option, + width_param: Option, + color_param: bool, ) -> Value { let ls_colors = LsColors::from_env().unwrap_or_default(); let mut grid = Grid::new(GridOptions { @@ -98,15 +102,21 @@ fn create_grid_output2( for (_row_index, header, value) in items { // only output value if the header name is 'name' if header == "name" { - let style = ls_colors.style_for_path(value.clone()); - let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default(); - let mut cell = Cell::from(ansi_style.apply(value).to_string()); - cell.alignment = Alignment::Right; - grid.add(cell); + if color_param { + let style = ls_colors.style_for_path(value.clone()); + let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default(); + let mut cell = Cell::from(ansi_style.apply(value).to_string()); + cell.alignment = Alignment::Right; + grid.add(cell); + } else { + let mut cell = Cell::from(value); + cell.alignment = Alignment::Right; + grid.add(cell); + } } } - let cols = if let Some(col) = columns_param { + 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