From e8c20390e0cf9169f5cba7cf0905371146169bbc Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:16:54 -0500 Subject: [PATCH] fix ls_colors coloring in grid and ls (#13935) # Description After PR https://github.com/nushell/nushell/pull/12953, LS_COLORS coloring broke in the `grid` and `ls` commands because the full path to the files were not available. This PR restores the coloring. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/system/run_external.rs | 10 ++++++++- crates/nu-command/src/viewers/griddle.rs | 4 +++- crates/nu-command/src/viewers/table.rs | 22 +++++++++++++------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 80c1c94e23..4a790fc9b4 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -623,8 +623,16 @@ mod test { #[test] fn test_write_pipeline_data() { - let engine_state = EngineState::new(); + let mut engine_state = EngineState::new(); let stack = Stack::new(); + let cwd = std::env::current_dir() + .unwrap() + .into_os_string() + .into_string() + .unwrap(); + + // set the PWD environment variable as it's required now + engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data())); let mut buf = vec![]; let input = PipelineData::Empty; diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index dccd856512..f75864b388 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -237,7 +237,9 @@ fn create_grid_output( cell.alignment = Alignment::Left; grid.add(cell); } else { - let style = ls_colors.style_for_path(value.clone()); + let no_ansi = nu_utils::strip_ansi_unlikely(&value); + let path = cwd.join(no_ansi.as_ref()); + let style = ls_colors.style_for_path(path.clone()); let ansi_style = style.map(Style::to_nu_ansi_term_style).unwrap_or_default(); let mut cell = Cell::from(ansi_style.paint(value).to_string()); cell.alignment = Alignment::Left; diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index dc52c4e094..5bdb847212 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -5,6 +5,7 @@ use lscolors::{LsColors, Style}; use nu_color_config::{color_from_hex, StyleComputer, TextStyle}; use nu_engine::{command_prelude::*, env_to_string}; +use nu_path::form::Absolute; use nu_pretty_hex::HexConfig; use nu_protocol::{ ByteStream, Config, DataSource, ListStream, PipelineMetadata, Signals, TableMode, ValueIterator, @@ -125,7 +126,7 @@ impl Command for Table { let val = Value::list(supported_table_modes(), Span::test_data()); return Ok(val.into_pipeline_data()); } - + let cwd = engine_state.cwd(Some(stack))?; let cfg = parse_table_config(call, engine_state, stack)?; let input = CmdInput::new(engine_state, stack, call, input); @@ -135,7 +136,7 @@ impl Command for Table { let _ = nu_utils::enable_vt_processing(); } - handle_table_command(input, cfg) + handle_table_command(input, cfg, cwd) } fn examples(&self) -> Vec { @@ -367,6 +368,7 @@ impl<'a> CmdInput<'a> { fn handle_table_command( mut input: CmdInput<'_>, cfg: TableConfig, + cwd: nu_path::PathBuf, ) -> Result { let span = input.data.span().unwrap_or(input.call.head); match input.data { @@ -389,11 +391,11 @@ fn handle_table_command( let stream = ListStream::new(vals.into_iter(), span, signals); input.data = PipelineData::Empty; - handle_row_stream(input, cfg, stream, metadata) + handle_row_stream(input, cfg, stream, metadata, cwd) } PipelineData::ListStream(stream, metadata) => { input.data = PipelineData::Empty; - handle_row_stream(input, cfg, stream, metadata) + handle_row_stream(input, cfg, stream, metadata, cwd) } PipelineData::Value(Value::Record { val, .. }, ..) => { input.data = PipelineData::Empty; @@ -413,7 +415,7 @@ fn handle_table_command( let stream = ListStream::new(val.into_range_iter(span, Signals::empty()), span, signals); input.data = PipelineData::Empty; - handle_row_stream(input, cfg, stream, metadata) + handle_row_stream(input, cfg, stream, metadata, cwd) } x => Ok(x), } @@ -605,6 +607,7 @@ fn handle_row_stream( cfg: TableConfig, stream: ListStream, metadata: Option, + cwd: nu_path::PathBuf, ) -> Result { let stream = match metadata.as_ref() { // First, `ls` sources: @@ -634,7 +637,9 @@ fn handle_row_stream( if let Some(value) = record.to_mut().get_mut("name") { let span = value.span(); if let Value::String { val, .. } = value { - if let Some(val) = render_path_name(val, &config, &ls_colors, span) { + if let Some(val) = + render_path_name(val, &config, &ls_colors, cwd.clone(), span) + { *value = val; } } @@ -1008,15 +1013,16 @@ fn render_path_name( path: &str, config: &Config, ls_colors: &LsColors, + cwd: nu_path::PathBuf, span: Span, ) -> Option { if !config.ls.use_ls_colors { return None; } + let fullpath = cwd.join(path); let stripped_path = nu_utils::strip_ansi_unlikely(path); - - let metadata = std::fs::symlink_metadata(stripped_path.as_ref()); + let metadata = std::fs::symlink_metadata(fullpath); let has_metadata = metadata.is_ok(); let style = ls_colors.style_for_path_with_metadata(stripped_path.as_ref(), metadata.ok().as_ref());