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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 16 deletions

View File

@ -33,6 +33,11 @@ impl Command for Griddle {
Some('w'), Some('w'),
) )
.switch("color", "draw output with color", Some('c')) .switch("color", "draw output with color", Some('c'))
.switch(
"icons",
"draw output with icons (assumes nerd font is used)",
Some('i'),
)
.named( .named(
"separator", "separator",
SyntaxShape::String, SyntaxShape::String,
@ -61,12 +66,13 @@ prints out the list properly."#
let width_param: Option<i64> = call.get_flag(engine_state, stack, "width")?; let width_param: Option<i64> = call.get_flag(engine_state, stack, "width")?;
let color_param: bool = call.has_flag(engine_state, stack, "color")?; let color_param: bool = call.has_flag(engine_state, stack, "color")?;
let separator_param: Option<String> = call.get_flag(engine_state, stack, "separator")?; 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 config = &stack.get_config(engine_state);
let env_str = match stack.get_env_var(engine_state, "LS_COLORS") { let env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?), Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?),
None => None, None => None,
}; };
let use_grid_icons = config.use_grid_icons;
let use_color: bool = color_param && config.use_ansi_coloring; let use_color: bool = color_param && config.use_ansi_coloring;
let cwd = engine_state.cwd(Some(stack))?; let cwd = engine_state.cwd(Some(stack))?;
@ -82,7 +88,7 @@ prints out the list properly."#
use_color, use_color,
separator_param, separator_param,
env_str, env_str,
use_grid_icons, icons_param,
cwd.as_ref(), cwd.as_ref(),
)?) )?)
} else { } else {
@ -100,7 +106,7 @@ prints out the list properly."#
use_color, use_color,
separator_param, separator_param,
env_str, env_str,
use_grid_icons, icons_param,
cwd.as_ref(), cwd.as_ref(),
)?) )?)
} else { } else {
@ -123,7 +129,7 @@ prints out the list properly."#
use_color, use_color,
separator_param, separator_param,
env_str, env_str,
use_grid_icons, icons_param,
cwd.as_ref(), 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", 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")), 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, use_color: bool,
separator_param: Option<String>, separator_param: Option<String>,
env_str: Option<String>, env_str: Option<String>,
use_grid_icons: bool, icons_param: bool,
cwd: &Path, cwd: &Path,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let ls_colors = get_ls_colors(env_str); 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' // only output value if the header name is 'name'
if header == "name" { if header == "name" {
if use_color { if use_color {
if use_grid_icons { if icons_param {
let no_ansi = nu_utils::strip_ansi_unlikely(&value); let no_ansi = nu_utils::strip_ansi_unlikely(&value);
let path = cwd.join(no_ansi.as_ref()); let path = cwd.join(no_ansi.as_ref());
let icon = icon_for_file(&path, call.head)?; let icon = icon_for_file(&path, call.head)?;
@ -232,6 +243,14 @@ fn create_grid_output(
cell.alignment = Alignment::Left; cell.alignment = Alignment::Left;
grid.add(cell); 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 { } else {
let mut cell = Cell::from(value); let mut cell = Cell::from(value);
cell.alignment = Alignment::Left; cell.alignment = Alignment::Left;

File diff suppressed because one or more lines are too long

View File

@ -47,7 +47,6 @@ pub struct Config {
pub table: TableConfig, pub table: TableConfig,
pub ls: LsConfig, pub ls: LsConfig,
pub color_config: HashMap<String, Value>, pub color_config: HashMap<String, Value>,
pub use_grid_icons: bool,
pub footer_mode: FooterMode, pub footer_mode: FooterMode,
pub float_precision: i64, pub float_precision: i64,
pub recursion_limit: i64, pub recursion_limit: i64,
@ -104,7 +103,6 @@ impl Default for Config {
cursor_shape: CursorShapeConfig::default(), cursor_shape: CursorShapeConfig::default(),
color_config: HashMap::new(), color_config: HashMap::new(),
use_grid_icons: true,
footer_mode: FooterMode::RowCount(25), footer_mode: FooterMode::RowCount(25),
float_precision: 2, float_precision: 2,
buffer_editor: Value::nothing(Span::unknown()), buffer_editor: Value::nothing(Span::unknown()),
@ -526,9 +524,6 @@ impl Value {
*value = config.color_config.clone().into_value(span); *value = config.color_config.clone().into_value(span);
} }
} }
"use_grid_icons" => {
process_bool_config(value, &mut errors, &mut config.use_grid_icons);
}
"footer_mode" => { "footer_mode" => {
process_string_enum( process_string_enum(
&mut config.footer_mode, &mut config.footer_mode,

View File

@ -227,7 +227,6 @@ $env.config = {
} }
color_config: $dark_theme # if you want a more interesting theme, you can replace the empty record with `$dark_theme`, `$light_theme` or another custom record color_config: $dark_theme # if you want a more interesting theme, you can replace the empty record with `$dark_theme`, `$light_theme` or another custom record
use_grid_icons: true
footer_mode: 25 # always, never, number_of_rows, auto footer_mode: 25 # always, never, number_of_rows, auto
float_precision: 2 # the precision for displaying floats in tables float_precision: 2 # the precision for displaying floats in tables
buffer_editor: null # command that will be used to edit the current line buffer with ctrl+o, if unset fallback to $env.EDITOR and $env.VISUAL buffer_editor: null # command that will be used to edit the current line buffer with ctrl+o, if unset fallback to $env.EDITOR and $env.VISUAL