Fix issue with ls | explore coloring of file names (#13952)

close #13936

The fix seem to be exactly what you've @fdncred  described.
But I'd recheck that everything is good.


![image](https://github.com/user-attachments/assets/5d9ce02b-9545-4a96-9718-b19d2e5810b8)

Take care.
Have a great week.
This commit is contained in:
Maxim Zhiburt 2024-09-29 22:03:56 +03:00 committed by GitHub
parent 8200831b07
commit fc61416c79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 43 additions and 11 deletions

1
Cargo.lock generated
View File

@ -3245,6 +3245,7 @@ dependencies = [
"nu-engine", "nu-engine",
"nu-json", "nu-json",
"nu-parser", "nu-parser",
"nu-path",
"nu-pretty-hex", "nu-pretty-hex",
"nu-protocol", "nu-protocol",
"nu-table", "nu-table",

View File

@ -16,6 +16,7 @@ workspace = true
[dependencies] [dependencies]
nu-protocol = { path = "../nu-protocol", version = "0.98.1" } nu-protocol = { path = "../nu-protocol", version = "0.98.1" }
nu-parser = { path = "../nu-parser", version = "0.98.1" } nu-parser = { path = "../nu-parser", version = "0.98.1" }
nu-path = { path = "../nu-path", version = "0.98.1" }
nu-color-config = { path = "../nu-color-config", version = "0.98.1" } nu-color-config = { path = "../nu-color-config", version = "0.98.1" }
nu-engine = { path = "../nu-engine", version = "0.98.1" } nu-engine = { path = "../nu-engine", version = "0.98.1" }
nu-table = { path = "../nu-table", version = "0.98.1" } nu-table = { path = "../nu-table", version = "0.98.1" }

View File

@ -72,6 +72,9 @@ impl Command for Explore {
explore_config.table.separator_style = lookup_color(&style_computer, "separator"); explore_config.table.separator_style = lookup_color(&style_computer, "separator");
let lscolors = create_lscolors(engine_state, stack); let lscolors = create_lscolors(engine_state, stack);
let cwd = engine_state.cwd(Some(stack)).map_or(String::new(), |path| {
path.to_str().unwrap_or("").to_string()
});
let config = PagerConfig::new( let config = PagerConfig::new(
&nu_config, &nu_config,
@ -80,6 +83,7 @@ impl Command for Explore {
&lscolors, &lscolors,
peek_value, peek_value,
tail, tail,
&cwd,
); );
let result = run_pager(engine_state, &mut stack.clone(), input, config); let result = run_pager(engine_state, &mut stack.clone(), input, config);

View File

@ -1,7 +1,10 @@
use std::path::Path;
use super::NuText; use super::NuText;
use lscolors::LsColors; use lscolors::LsColors;
use nu_ansi_term::{Color, Style}; use nu_ansi_term::{Color, Style};
use nu_engine::env_to_string; use nu_engine::env_to_string;
use nu_path::{expand_path_with, expand_to_real_path};
use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::engine::{EngineState, Stack};
use nu_utils::get_ls_colors; use nu_utils::get_ls_colors;
@ -14,7 +17,7 @@ pub fn create_lscolors(engine_state: &EngineState, stack: &Stack) -> LsColors {
} }
/// Colorizes any columns named "name" in the table using LS_COLORS /// Colorizes any columns named "name" in the table using LS_COLORS
pub fn lscolorize(header: &[String], data: &mut [Vec<NuText>], lscolors: &LsColors) { pub fn lscolorize(header: &[String], data: &mut [Vec<NuText>], cwd: &str, lscolors: &LsColors) {
for (col, col_name) in header.iter().enumerate() { for (col, col_name) in header.iter().enumerate() {
if col_name != "name" { if col_name != "name" {
continue; continue;
@ -23,7 +26,7 @@ pub fn lscolorize(header: &[String], data: &mut [Vec<NuText>], lscolors: &LsColo
for row in data.iter_mut() { for row in data.iter_mut() {
let (path, text_style) = &mut row[col]; let (path, text_style) = &mut row[col];
let style = get_path_style(path, lscolors); let style = get_path_style(path, cwd, lscolors);
if let Some(style) = style { if let Some(style) = style {
*text_style = text_style.style(style); *text_style = text_style.style(style);
} }
@ -31,9 +34,29 @@ pub fn lscolorize(header: &[String], data: &mut [Vec<NuText>], lscolors: &LsColo
} }
} }
fn get_path_style(path: &str, ls_colors: &LsColors) -> Option<Style> { fn get_path_style(path: &str, cwd: &str, ls_colors: &LsColors) -> Option<Style> {
let stripped_path = nu_utils::strip_ansi_unlikely(path); let stripped_path = nu_utils::strip_ansi_unlikely(path);
let style = ls_colors.style_for_str(stripped_path.as_ref()); let mut style = ls_colors.style_for_str(stripped_path.as_ref());
let is_likely_dir = style.is_none();
if is_likely_dir {
let mut meta = std::fs::symlink_metadata(path).ok();
if meta.is_none() {
let mut expanded_path = expand_to_real_path(path);
let try_cwd = expanded_path.as_path() == Path::new(path);
if try_cwd {
let cwd_path = format!("./{}", path);
expanded_path = expand_path_with(cwd_path, cwd, false);
}
meta = std::fs::symlink_metadata(expanded_path.as_path()).ok();
style = ls_colors.style_for_path_with_metadata(expanded_path.as_path(), meta.as_ref());
} else {
style = ls_colors.style_for_path_with_metadata(path, meta.as_ref());
}
}
style.map(lsstyle_to_nu_style) style.map(lsstyle_to_nu_style)
} }

View File

@ -143,6 +143,8 @@ pub struct PagerConfig<'a> {
// If true, when quitting output the value of the cell the cursor was on // If true, when quitting output the value of the cell the cursor was on
pub peek_value: bool, pub peek_value: bool,
pub tail: bool, pub tail: bool,
// Just a cached dir we are working in used for color manipulations
pub cwd: String,
} }
impl<'a> PagerConfig<'a> { impl<'a> PagerConfig<'a> {
@ -153,6 +155,7 @@ impl<'a> PagerConfig<'a> {
lscolors: &'a LsColors, lscolors: &'a LsColors,
peek_value: bool, peek_value: bool,
tail: bool, tail: bool,
cwd: &str,
) -> Self { ) -> Self {
Self { Self {
nu_config, nu_config,
@ -161,6 +164,7 @@ impl<'a> PagerConfig<'a> {
lscolors, lscolors,
peek_value, peek_value,
tail, tail,
cwd: cwd.to_string(),
} }
} }
} }
@ -371,6 +375,7 @@ fn create_view_config<'a>(pager: &'a Pager<'_>) -> ViewConfig<'a> {
cfg.explore_config, cfg.explore_config,
cfg.style_computer, cfg.style_computer,
cfg.lscolors, cfg.lscolors,
&pager.config.cwd,
) )
} }
@ -419,12 +424,7 @@ fn run_command(
Command::View { mut cmd, stackable } => { Command::View { mut cmd, stackable } => {
// what we do we just replace the view. // what we do we just replace the view.
let value = view_stack.curr_view.as_mut().and_then(|p| p.view.exit()); let value = view_stack.curr_view.as_mut().and_then(|p| p.view.exit());
let view_cfg = ViewConfig::new( let view_cfg = create_view_config(pager);
pager.config.nu_config,
pager.config.explore_config,
pager.config.style_computer,
pager.config.lscolors,
);
let new_view = cmd.spawn(engine_state, stack, value, &view_cfg)?; let new_view = cmd.spawn(engine_state, stack, value, &view_cfg)?;
if let Some(view) = view_stack.curr_view.take() { if let Some(view) = view_stack.curr_view.take() {

View File

@ -58,6 +58,7 @@ pub struct ViewConfig<'a> {
pub explore_config: &'a ExploreConfig, pub explore_config: &'a ExploreConfig,
pub style_computer: &'a StyleComputer<'a>, pub style_computer: &'a StyleComputer<'a>,
pub lscolors: &'a LsColors, pub lscolors: &'a LsColors,
pub cwd: &'a str,
} }
impl<'a> ViewConfig<'a> { impl<'a> ViewConfig<'a> {
@ -66,12 +67,14 @@ impl<'a> ViewConfig<'a> {
explore_config: &'a ExploreConfig, explore_config: &'a ExploreConfig,
style_computer: &'a StyleComputer<'a>, style_computer: &'a StyleComputer<'a>,
lscolors: &'a LsColors, lscolors: &'a LsColors,
cwd: &'a str,
) -> Self { ) -> Self {
Self { Self {
nu_config, nu_config,
explore_config, explore_config,
style_computer, style_computer,
lscolors, lscolors,
cwd,
} }
} }
} }

View File

@ -127,7 +127,7 @@ impl RecordView {
if layer.record_text.is_none() { if layer.record_text.is_none() {
let mut data = let mut data =
convert_records_to_string(&layer.record_values, cfg.nu_config, cfg.style_computer); convert_records_to_string(&layer.record_values, cfg.nu_config, cfg.style_computer);
lscolorize(&layer.column_names, &mut data, cfg.lscolors); lscolorize(&layer.column_names, &mut data, cfg.cwd, cfg.lscolors);
layer.record_text = Some(data); layer.record_text = Some(data);
} }