From 5042f19d1b4035b67b0fb58a95dd1784caeaf9ce Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 8 Feb 2024 12:29:28 -0800 Subject: [PATCH] colored file-like completions (#11702) # Description `ls` and other file completions uses `LS_COLORS`. ![maim-2024 01 31 21 34 31](https://github.com/nushell/nushell/assets/15631555/d5c3813f-77b5-4391-aa0b-4b2125e5aca5) # User-Facing Changes # Tests + Formatting # After Submitting --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> --- Cargo.lock | 1 + crates/nu-cli/Cargo.toml | 1 + crates/nu-cli/src/completions/completer.rs | 30 ++++++++++++------- .../src/completions/completion_common.rs | 30 +++++++++++++++++-- .../src/completions/directory_completions.rs | 21 +++++++++---- .../src/completions/dotnu_completions.rs | 21 +++++++++---- .../src/completions/file_completions.rs | 21 +++++++++---- crates/nu-command/src/viewers/table.rs | 11 +++---- crates/nu-protocol/src/config/mod.rs | 6 ++++ .../src/sample_config/default_config.nu | 1 + 10 files changed, 107 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45201cc688..f51840db71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2843,6 +2843,7 @@ dependencies = [ "fuzzy-matcher", "is_executable", "log", + "lscolors", "miette", "nu-ansi-term", "nu-cmd-base", diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index b07ce265f9..62c531e70b 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -34,6 +34,7 @@ fuzzy-matcher = "0.3" is_executable = "1.0" log = "0.4" miette = { version = "7.0", features = ["fancy-no-backtrace"] } +lscolors = { version = "0.17", default-features = false, features = ["nu-ansi-term"] } once_cell = "1.18" percent-encoding = "2" pathdiff = "0.2" diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index 917ffb9c7f..2459fca804 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -266,8 +266,10 @@ impl NuCompleter { || prev_expr_str == b"overlay use" || prev_expr_str == b"source-env" { - let mut completer = - DotNuCompletion::new(self.engine_state.clone()); + let mut completer = DotNuCompletion::new( + self.engine_state.clone(), + self.stack.clone(), + ); return self.process_completion( &mut completer, @@ -278,8 +280,10 @@ impl NuCompleter { pos, ); } else if prev_expr_str == b"ls" { - let mut completer = - FileCompletion::new(self.engine_state.clone()); + let mut completer = FileCompletion::new( + self.engine_state.clone(), + self.stack.clone(), + ); return self.process_completion( &mut completer, @@ -313,8 +317,10 @@ impl NuCompleter { ); } FlatShape::Directory => { - let mut completer = - DirectoryCompletion::new(self.engine_state.clone()); + let mut completer = DirectoryCompletion::new( + self.engine_state.clone(), + self.stack.clone(), + ); return self.process_completion( &mut completer, @@ -326,8 +332,10 @@ impl NuCompleter { ); } FlatShape::Filepath | FlatShape::GlobPattern => { - let mut completer = - FileCompletion::new(self.engine_state.clone()); + let mut completer = FileCompletion::new( + self.engine_state.clone(), + self.stack.clone(), + ); return self.process_completion( &mut completer, @@ -374,8 +382,10 @@ impl NuCompleter { } // Check for file completion - let mut completer = - FileCompletion::new(self.engine_state.clone()); + let mut completer = FileCompletion::new( + self.engine_state.clone(), + self.stack.clone(), + ); out = self.process_completion( &mut completer, &working_set, diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index f4af6849f5..3d12f2aea4 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -1,6 +1,10 @@ use crate::completions::{matches, CompletionOptions}; +use nu_ansi_term::Style; +use nu_engine::env_to_string; use nu_path::home_dir; +use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::{engine::StateWorkingSet, Span}; +use nu_utils::get_ls_colors; use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP}; fn complete_rec( @@ -92,10 +96,21 @@ pub fn complete_item( partial: &str, cwd: &str, options: &CompletionOptions, -) -> Vec<(nu_protocol::Span, String)> { + engine_state: &EngineState, + stack: &Stack, +) -> Vec<(nu_protocol::Span, String, Option