diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/nu-explore/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/nu-explore/src/explore.rs | 4 | ||||
-rw-r--r-- | crates/nu-explore/src/nu_common/lscolor.rs | 31 | ||||
-rw-r--r-- | crates/nu-explore/src/pager/mod.rs | 12 | ||||
-rw-r--r-- | crates/nu-explore/src/views/mod.rs | 3 | ||||
-rw-r--r-- | crates/nu-explore/src/views/record/mod.rs | 2 |
7 files changed, 43 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock index 990e9d5a1..1d6508ed1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3245,6 +3245,7 @@ dependencies = [ "nu-engine", "nu-json", "nu-parser", + "nu-path", "nu-pretty-hex", "nu-protocol", "nu-table", diff --git a/crates/nu-explore/Cargo.toml b/crates/nu-explore/Cargo.toml index f980467e3..fe9caf2aa 100644 --- a/crates/nu-explore/Cargo.toml +++ b/crates/nu-explore/Cargo.toml @@ -16,6 +16,7 @@ workspace = true [dependencies] nu-protocol = { path = "../nu-protocol", 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-engine = { path = "../nu-engine", version = "0.98.1" } nu-table = { path = "../nu-table", version = "0.98.1" } diff --git a/crates/nu-explore/src/explore.rs b/crates/nu-explore/src/explore.rs index d6a59ee08..5384e0fce 100644 --- a/crates/nu-explore/src/explore.rs +++ b/crates/nu-explore/src/explore.rs @@ -72,6 +72,9 @@ impl Command for Explore { explore_config.table.separator_style = lookup_color(&style_computer, "separator"); 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( &nu_config, @@ -80,6 +83,7 @@ impl Command for Explore { &lscolors, peek_value, tail, + &cwd, ); let result = run_pager(engine_state, &mut stack.clone(), input, config); diff --git a/crates/nu-explore/src/nu_common/lscolor.rs b/crates/nu-explore/src/nu_common/lscolor.rs index 491d2c3f3..393ace788 100644 --- a/crates/nu-explore/src/nu_common/lscolor.rs +++ b/crates/nu-explore/src/nu_common/lscolor.rs @@ -1,7 +1,10 @@ +use std::path::Path; + use super::NuText; use lscolors::LsColors; use nu_ansi_term::{Color, Style}; use nu_engine::env_to_string; +use nu_path::{expand_path_with, expand_to_real_path}; use nu_protocol::engine::{EngineState, Stack}; 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 -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() { if col_name != "name" { continue; @@ -23,7 +26,7 @@ pub fn lscolorize(header: &[String], data: &mut [Vec<NuText>], lscolors: &LsColo for row in data.iter_mut() { 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 { *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 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) } diff --git a/crates/nu-explore/src/pager/mod.rs b/crates/nu-explore/src/pager/mod.rs index d230f8a54..2141f89ce 100644 --- a/crates/nu-explore/src/pager/mod.rs +++ b/crates/nu-explore/src/pager/mod.rs @@ -143,6 +143,8 @@ pub struct PagerConfig<'a> { // If true, when quitting output the value of the cell the cursor was on pub peek_value: bool, pub tail: bool, + // Just a cached dir we are working in used for color manipulations + pub cwd: String, } impl<'a> PagerConfig<'a> { @@ -153,6 +155,7 @@ impl<'a> PagerConfig<'a> { lscolors: &'a LsColors, peek_value: bool, tail: bool, + cwd: &str, ) -> Self { Self { nu_config, @@ -161,6 +164,7 @@ impl<'a> PagerConfig<'a> { lscolors, peek_value, tail, + cwd: cwd.to_string(), } } } @@ -371,6 +375,7 @@ fn create_view_config<'a>(pager: &'a Pager<'_>) -> ViewConfig<'a> { cfg.explore_config, cfg.style_computer, cfg.lscolors, + &pager.config.cwd, ) } @@ -419,12 +424,7 @@ fn run_command( Command::View { mut cmd, stackable } => { // what we do we just replace the view. let value = view_stack.curr_view.as_mut().and_then(|p| p.view.exit()); - let view_cfg = ViewConfig::new( - pager.config.nu_config, - pager.config.explore_config, - pager.config.style_computer, - pager.config.lscolors, - ); + let view_cfg = create_view_config(pager); let new_view = cmd.spawn(engine_state, stack, value, &view_cfg)?; if let Some(view) = view_stack.curr_view.take() { diff --git a/crates/nu-explore/src/views/mod.rs b/crates/nu-explore/src/views/mod.rs index bdbf3129d..589134016 100644 --- a/crates/nu-explore/src/views/mod.rs +++ b/crates/nu-explore/src/views/mod.rs @@ -58,6 +58,7 @@ pub struct ViewConfig<'a> { pub explore_config: &'a ExploreConfig, pub style_computer: &'a StyleComputer<'a>, pub lscolors: &'a LsColors, + pub cwd: &'a str, } impl<'a> ViewConfig<'a> { @@ -66,12 +67,14 @@ impl<'a> ViewConfig<'a> { explore_config: &'a ExploreConfig, style_computer: &'a StyleComputer<'a>, lscolors: &'a LsColors, + cwd: &'a str, ) -> Self { Self { nu_config, explore_config, style_computer, lscolors, + cwd, } } } diff --git a/crates/nu-explore/src/views/record/mod.rs b/crates/nu-explore/src/views/record/mod.rs index 4f617686f..f77d22a62 100644 --- a/crates/nu-explore/src/views/record/mod.rs +++ b/crates/nu-explore/src/views/record/mod.rs @@ -127,7 +127,7 @@ impl RecordView { if layer.record_text.is_none() { let mut data = 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); } |