From b2b5b89a925401c7d0d6d3d4fa075c9506ab4e2d Mon Sep 17 00:00:00 2001 From: Piepmatz Date: Fri, 27 Dec 2024 13:58:18 +0100 Subject: [PATCH] Add command to get evaluated color setting (#14683) # Description In #14647 I added the option `"auto"` to be a valid option for `$env.config.use_ansi_coloring`. That improves the decision making whether ansi colors should be used or not but that makes it hard for custom commands to respect that value as the config might now be a non-boolean value. To retrieve that evaluated value I added a new command called `config use-colors` that returns an evaluated boolean that may be used to decide if colors should be used or not. # User-Facing Changes Scripts that previously just checked `$env.config.use_ansi_coloring` should now use `config use-colors` for their color decision making. # Tests + Formatting This PR essentially only runs `UseAnsiColoring::get`, and that is highly tested in the #14647, so I don't think this needs further testing. - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting I'm not sure if we have any docs about that ansi coloring setup. If we have, we should update these. --- crates/nu-command/src/default_context.rs | 1 + .../src/env/config/config_use_colors.rs | 41 +++++++++++++++++++ crates/nu-command/src/env/config/mod.rs | 2 + crates/nu-command/src/env/mod.rs | 1 + 4 files changed, 45 insertions(+) create mode 100644 crates/nu-command/src/env/config/config_use_colors.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 168ee78027..df704fe369 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -354,6 +354,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { ConfigFlatten, ConfigMeta, ConfigReset, + ConfigUseColors, }; // Math diff --git a/crates/nu-command/src/env/config/config_use_colors.rs b/crates/nu-command/src/env/config/config_use_colors.rs new file mode 100644 index 0000000000..28537535ee --- /dev/null +++ b/crates/nu-command/src/env/config/config_use_colors.rs @@ -0,0 +1,41 @@ +use nu_engine::command_prelude::*; + +#[derive(Clone)] +pub struct ConfigUseColors; + +impl Command for ConfigUseColors { + fn name(&self) -> &str { + "config use-colors" + } + + fn signature(&self) -> Signature { + Signature::build(self.name()) + .category(Category::Env) + .input_output_type(Type::Nothing, Type::Bool) + } + + fn description(&self) -> &str { + "Get the configuration for color output." + } + + fn extra_description(&self) -> &str { + r#"Use this command instead of checking `$env.config.use_ansi_coloring` to properly handle the "auto" setting, including environment variables that influence its behavior."# + } + + fn run( + &self, + engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + let use_ansi_coloring = engine_state + .get_config() + .use_ansi_coloring + .get(engine_state); + Ok(PipelineData::Value( + Value::bool(use_ansi_coloring, call.head), + None, + )) + } +} diff --git a/crates/nu-command/src/env/config/mod.rs b/crates/nu-command/src/env/config/mod.rs index d596eb57b9..2981aff313 100644 --- a/crates/nu-command/src/env/config/mod.rs +++ b/crates/nu-command/src/env/config/mod.rs @@ -3,9 +3,11 @@ mod config_env; mod config_flatten; mod config_nu; mod config_reset; +mod config_use_colors; pub use config_::ConfigMeta; pub use config_env::ConfigEnv; pub use config_flatten::ConfigFlatten; pub use config_nu::ConfigNu; pub use config_reset::ConfigReset; +pub use config_use_colors::ConfigUseColors; diff --git a/crates/nu-command/src/env/mod.rs b/crates/nu-command/src/env/mod.rs index 3f9312ed13..62a9e4d116 100644 --- a/crates/nu-command/src/env/mod.rs +++ b/crates/nu-command/src/env/mod.rs @@ -9,6 +9,7 @@ pub use config::ConfigFlatten; pub use config::ConfigMeta; pub use config::ConfigNu; pub use config::ConfigReset; +pub use config::ConfigUseColors; pub use export_env::ExportEnv; pub use load_env::LoadEnv; pub use source_env::SourceEnv;