Use cwd in grid (#12947)

# Description
Fixes #12946. The `grid` command does not use the cwd when trying to get
the icon or color for a file/path.
This commit is contained in:
Ian Manske 2024-05-23 20:38:47 +00:00 committed by GitHub
parent 0b5a4c0d95
commit bf07806b1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ use nu_engine::{command_prelude::*, env_to_string};
use nu_protocol::Config; use nu_protocol::Config;
use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions}; use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
use nu_utils::get_ls_colors; use nu_utils::get_ls_colors;
use std::path::Path;
use terminal_size::{Height, Width}; use terminal_size::{Height, Width};
#[derive(Clone)] #[derive(Clone)]
@ -67,6 +68,7 @@ prints out the list properly."#
}; };
let use_grid_icons = config.use_grid_icons; 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))?;
match input { match input {
PipelineData::Value(Value::List { vals, .. }, ..) => { PipelineData::Value(Value::List { vals, .. }, ..) => {
@ -81,6 +83,7 @@ prints out the list properly."#
separator_param, separator_param,
env_str, env_str,
use_grid_icons, use_grid_icons,
&cwd,
)?) )?)
} else { } else {
Ok(PipelineData::empty()) Ok(PipelineData::empty())
@ -98,6 +101,7 @@ prints out the list properly."#
separator_param, separator_param,
env_str, env_str,
use_grid_icons, use_grid_icons,
&cwd,
)?) )?)
} else { } else {
// dbg!(data); // dbg!(data);
@ -120,6 +124,7 @@ prints out the list properly."#
separator_param, separator_param,
env_str, env_str,
use_grid_icons, use_grid_icons,
&cwd,
)?) )?)
} }
x => { x => {
@ -161,6 +166,7 @@ prints out the list properly."#
} }
} }
#[allow(clippy::too_many_arguments)]
fn create_grid_output( fn create_grid_output(
items: Vec<(usize, String, String)>, items: Vec<(usize, String, String)>,
call: &Call, call: &Call,
@ -169,6 +175,7 @@ fn create_grid_output(
separator_param: Option<String>, separator_param: Option<String>,
env_str: Option<String>, env_str: Option<String>,
use_grid_icons: bool, use_grid_icons: bool,
cwd: &Path,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let ls_colors = get_ls_colors(env_str); let ls_colors = get_ls_colors(env_str);
@ -196,8 +203,8 @@ fn create_grid_output(
if use_color { if use_color {
if use_grid_icons { if use_grid_icons {
let no_ansi = nu_utils::strip_ansi_unlikely(&value); let no_ansi = nu_utils::strip_ansi_unlikely(&value);
let path = std::path::Path::new(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)?;
let ls_colors_style = ls_colors.style_for_path(path); let ls_colors_style = ls_colors.style_for_path(path);
let icon_style = match ls_colors_style { let icon_style = match ls_colors_style {