From cde8a629c5e73e4775d01f4252f4beff253001bd Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Wed, 18 Jun 2025 10:49:40 +0200 Subject: [PATCH] Restrict `config.show_banner` to valid options (#15985) --- crates/nu-cli/src/repl.rs | 8 +-- crates/nu-protocol/src/config/mod.rs | 6 +- crates/nu-protocol/src/config/output.rs | 68 +++++++++++++++++++ .../nu-utils/src/default_files/doc_config.nu | 5 +- 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index a4ca2903cc..f003f15b0e 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -22,8 +22,8 @@ use nu_color_config::StyleComputer; use nu_engine::env_to_strings; use nu_engine::exit::cleanup_exit; use nu_parser::{lex, parse, trim_quotes_str}; -use nu_protocol::shell_error; use nu_protocol::shell_error::io::IoError; +use nu_protocol::{BannerKind, shell_error}; use nu_protocol::{ HistoryConfig, HistoryFileFormat, PipelineData, ShellError, Span, Spanned, Value, config::NuCursorShape, @@ -145,8 +145,8 @@ pub fn evaluate_repl( if load_std_lib.is_none() { match engine_state.get_config().show_banner { - Value::Bool { val: false, .. } => {} - Value::String { ref val, .. } if val == "short" => { + BannerKind::None => {} + BannerKind::Short => { eval_source( engine_state, &mut unique_stack, @@ -156,7 +156,7 @@ pub fn evaluate_repl( false, ); } - _ => { + BannerKind::Full => { eval_source( engine_state, &mut unique_stack, diff --git a/crates/nu-protocol/src/config/mod.rs b/crates/nu-protocol/src/config/mod.rs index 42da2b3233..4f267d2597 100644 --- a/crates/nu-protocol/src/config/mod.rs +++ b/crates/nu-protocol/src/config/mod.rs @@ -17,7 +17,7 @@ pub use helper::extract_value; pub use history::{HistoryConfig, HistoryFileFormat}; pub use hooks::Hooks; pub use ls::LsConfig; -pub use output::ErrorStyle; +pub use output::{BannerKind, ErrorStyle}; pub use plugin_gc::{PluginGcConfig, PluginGcConfigs}; pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu}; pub use rm::RmConfig; @@ -61,7 +61,7 @@ pub struct Config { pub rm: RmConfig, pub shell_integration: ShellIntegrationConfig, pub buffer_editor: Value, - pub show_banner: Value, + pub show_banner: BannerKind, pub bracketed_paste: bool, pub render_right_prompt_on_last_line: bool, pub explore: HashMap, @@ -84,7 +84,7 @@ pub struct Config { impl Default for Config { fn default() -> Config { Config { - show_banner: Value::bool(true, Span::unknown()), + show_banner: BannerKind::default(), table: TableConfig::default(), rm: RmConfig::default(), diff --git a/crates/nu-protocol/src/config/output.rs b/crates/nu-protocol/src/config/output.rs index 082856bdce..c0e2a695e8 100644 --- a/crates/nu-protocol/src/config/output.rs +++ b/crates/nu-protocol/src/config/output.rs @@ -25,3 +25,71 @@ impl UpdateFromValue for ErrorStyle { config_update_string_enum(self, value, path, errors) } } + +/// Option: show_banner +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub enum BannerKind { + /// No banner on startup + None, + /// Abbreviated banner just containing the startup-time + Short, + /// The full banner including Ellie + #[default] + Full, +} + +impl IntoValue for BannerKind { + fn into_value(self, span: Span) -> Value { + match self { + // This uses a custom implementation to reflect common config + // bool: true, false was used for a long time + // string: short was added later + BannerKind::None => Value::bool(false, span), + BannerKind::Short => Value::string("short", span), + BannerKind::Full => Value::bool(true, span), + } + } +} + +impl UpdateFromValue for BannerKind { + fn update<'a>( + &mut self, + value: &'a Value, + path: &mut ConfigPath<'a>, + errors: &mut ConfigErrors, + ) { + match value { + Value::Bool { val, .. } => match val { + true => { + *self = BannerKind::Full; + } + false => { + *self = BannerKind::None; + } + }, + Value::String { val, .. } => match val.as_str() { + "true" => { + *self = BannerKind::Full; + } + "full" => { + *self = BannerKind::Full; + } + "short" => { + *self = BannerKind::Short; + } + "false" => { + *self = BannerKind::None; + } + "none" => { + *self = BannerKind::None; + } + _ => { + errors.invalid_value(path, "true/'full', 'short', false/'none'", value); + } + }, + _ => { + errors.invalid_value(path, "true/'full', 'short', false/'none'", value); + } + } + } +} diff --git a/crates/nu-utils/src/default_files/doc_config.nu b/crates/nu-utils/src/default_files/doc_config.nu index 252eb4718e..46bd9edecb 100644 --- a/crates/nu-utils/src/default_files/doc_config.nu +++ b/crates/nu-utils/src/default_files/doc_config.nu @@ -70,7 +70,10 @@ $env.config.history.isolation = true # Miscellaneous Settings # ---------------------- -# show_banner (bool): Enable or disable the welcome banner at startup +# show_banner (bool|string): Enable or disable the welcome banner at startup +# true | "full": show the full banner +# "short": just show the start-up time +# false | "none": don't show a banner $env.config.show_banner = true # rm.always_trash (bool):