remove config use_grid_icons, move to parameter of grid command (#13788)

# Description

After looking at #13751 I noticed that the config setting
`use_grid_icons` seems out of place. So, I removed it from the config
and made it a parameter to the `grid` command.
This commit is contained in:
Darren Schroeder
2024-09-05 18:25:43 -05:00
committed by GitHub
parent 870eb2530c
commit d0c2adabf7
4 changed files with 27 additions and 16 deletions

View File

@ -33,6 +33,11 @@ impl Command for Griddle {
Some('w'),
)
.switch("color", "draw output with color", Some('c'))
.switch(
"icons",
"draw output with icons (assumes nerd font is used)",
Some('i'),
)
.named(
"separator",
SyntaxShape::String,
@ -61,12 +66,13 @@ prints out the list properly."#
let width_param: Option<i64> = call.get_flag(engine_state, stack, "width")?;
let color_param: bool = call.has_flag(engine_state, stack, "color")?;
let separator_param: Option<String> = call.get_flag(engine_state, stack, "separator")?;
let icons_param: bool = call.has_flag(engine_state, stack, "icons")?;
let config = &stack.get_config(engine_state);
let env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?),
None => None,
};
let use_grid_icons = config.use_grid_icons;
let use_color: bool = color_param && config.use_ansi_coloring;
let cwd = engine_state.cwd(Some(stack))?;
@ -82,7 +88,7 @@ prints out the list properly."#
use_color,
separator_param,
env_str,
use_grid_icons,
icons_param,
cwd.as_ref(),
)?)
} else {
@ -100,7 +106,7 @@ prints out the list properly."#
use_color,
separator_param,
env_str,
use_grid_icons,
icons_param,
cwd.as_ref(),
)?)
} else {
@ -123,7 +129,7 @@ prints out the list properly."#
use_color,
separator_param,
env_str,
use_grid_icons,
icons_param,
cwd.as_ref(),
)?)
}
@ -162,6 +168,11 @@ prints out the list properly."#
example: "[[name patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | grid",
result: Some(Value::test_string("0.1.0 │ 0.1.1 │ 0.2.0\n")),
},
Example {
description: "Render a table with 'name' column in it to a grid with icons and colors",
example: "[[name patch]; [Cargo.toml false] [README.md true] [SECURITY.md false]] | grid --icons --color",
result: None,
},
]
}
}
@ -174,7 +185,7 @@ fn create_grid_output(
use_color: bool,
separator_param: Option<String>,
env_str: Option<String>,
use_grid_icons: bool,
icons_param: bool,
cwd: &Path,
) -> Result<PipelineData, ShellError> {
let ls_colors = get_ls_colors(env_str);
@ -201,7 +212,7 @@ fn create_grid_output(
// only output value if the header name is 'name'
if header == "name" {
if use_color {
if use_grid_icons {
if icons_param {
let no_ansi = nu_utils::strip_ansi_unlikely(&value);
let path = cwd.join(no_ansi.as_ref());
let icon = icon_for_file(&path, call.head)?;
@ -232,6 +243,14 @@ fn create_grid_output(
cell.alignment = Alignment::Left;
grid.add(cell);
}
} else if icons_param {
let no_ansi = nu_utils::strip_ansi_unlikely(&value);
let path = cwd.join(no_ansi.as_ref());
let icon = icon_for_file(&path, call.head)?;
let item = format!("{} {}", String::from(icon), value);
let mut cell = Cell::from(item);
cell.alignment = Alignment::Left;
grid.add(cell);
} else {
let mut cell = Cell::from(value);
cell.alignment = Alignment::Left;