added a switch to enable coloring

This commit is contained in:
Darren Schroeder 2021-10-08 09:53:26 -05:00
parent 5ddf0d209d
commit c636c30a19

View file

@ -20,12 +20,14 @@ impl Command for Griddle {
} }
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("grid").named( Signature::build("grid")
"columns", .named(
"width",
SyntaxShape::Int, SyntaxShape::Int,
"number of columns wide", "number of columns wide",
Some('c'), Some('w'),
) )
.switch("color", "draw output with color", Some('c'))
} }
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
@ -43,14 +45,15 @@ prints out the list properly."#
call: &Call, call: &Call,
input: Value, input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> { ) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let columns_param: Option<String> = call.get_flag(context, "columns")?; let width_param: Option<String> = call.get_flag(context, "width")?;
let color_param: bool = call.has_flag("color");
match input { match input {
Value::List { vals, .. } => { Value::List { vals, .. } => {
// dbg!("value::list"); // dbg!("value::list");
let data = convert_to_list2(vals); let data = convert_to_list2(vals);
if let Some(items) = data { if let Some(items) = data {
Ok(create_grid_output2(items, call, columns_param)) Ok(create_grid_output2(items, call, width_param, color_param))
} else { } else {
Ok(Value::Nothing { span: call.head }) Ok(Value::Nothing { span: call.head })
} }
@ -59,7 +62,7 @@ prints out the list properly."#
// dbg!("value::stream"); // dbg!("value::stream");
let data = convert_to_list2(stream); let data = convert_to_list2(stream);
if let Some(items) = data { if let Some(items) = data {
Ok(create_grid_output2(items, call, columns_param)) Ok(create_grid_output2(items, call, width_param, color_param))
} else { } else {
// dbg!(data); // dbg!(data);
Ok(Value::Nothing { span: call.head }) Ok(Value::Nothing { span: call.head })
@ -73,7 +76,7 @@ prints out the list properly."#
items.push((i, c, v.into_string())) 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 => { x => {
// dbg!("other value"); // dbg!("other value");
@ -87,7 +90,8 @@ prints out the list properly."#
fn create_grid_output2( fn create_grid_output2(
items: Vec<(usize, String, String)>, items: Vec<(usize, String, String)>,
call: &Call, call: &Call,
columns_param: Option<String>, width_param: Option<String>,
color_param: bool,
) -> Value { ) -> Value {
let ls_colors = LsColors::from_env().unwrap_or_default(); let ls_colors = LsColors::from_env().unwrap_or_default();
let mut grid = Grid::new(GridOptions { let mut grid = Grid::new(GridOptions {
@ -98,15 +102,21 @@ fn create_grid_output2(
for (_row_index, header, value) in items { for (_row_index, header, value) in items {
// only output value if the header name is 'name' // only output value if the header name is 'name'
if header == "name" { if header == "name" {
if color_param {
let style = ls_colors.style_for_path(value.clone()); let style = ls_colors.style_for_path(value.clone());
let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default(); let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default();
let mut cell = Cell::from(ansi_style.apply(value).to_string()); let mut cell = Cell::from(ansi_style.apply(value).to_string());
cell.alignment = Alignment::Right; cell.alignment = Alignment::Right;
grid.add(cell); 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::<u16>().unwrap_or(80) col.parse::<u16>().unwrap_or(80)
} else if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() { } else if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
w w