From 471e4c6596f144523a72c3b232b35768c5139bc1 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 21 Mar 2025 07:57:11 -0400 Subject: [PATCH] feat: render default value if provided in right prompt --- .../nu-command/src/platform/input/input_.rs | 19 +++++++++++-------- .../src/platform/input/reedline_prompt.rs | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/nu-command/src/platform/input/input_.rs b/crates/nu-command/src/platform/input/input_.rs index 92412fff38..2f49f67ff8 100644 --- a/crates/nu-command/src/platform/input/input_.rs +++ b/crates/nu-command/src/platform/input/input_.rs @@ -4,8 +4,6 @@ use nu_engine::command_prelude::*; use nu_protocol::shell_error::io::IoError; use reedline::{Reedline, Signal}; -use std::io::Write; - #[derive(Clone)] pub struct Input; @@ -84,24 +82,29 @@ impl Command for Input { return self.legacy_input(engine_state, stack, call, _input); } - // FIXME: print default val if present + // Here we will render the default prompt to the right let default_val: Option = call.get_flag(engine_state, stack, "default")?; - + let right_prompt = match (&prompt_str, &default_val) { + (Some(_prompt), Some(val)) => format!("(default: {val})").to_string(), + _ => "".to_string(), + }; let mut buf = String::new(); let prompt = ReedlinePrompt { left_prompt: prompt_str.unwrap_or("".to_string()), + // Breaking change, the default is now in the right prompt + right_prompt, indicator: "".to_string(), // TODO: Add support for custom prompt indicators // for now, and backwards compat, we just use the empty // string }; - let mut line_editor = Reedline::create().with_ansi_colors(false); // Disable ansi colors for now + let mut line_editor = Reedline::create(); + // Disable ansi colors for now, for backwards compat. This will be configurable in the + // future + line_editor = line_editor.with_ansi_colors(false); // TODO handle options loop { - if i64::try_from(buf.len()).unwrap_or(0) >= numchar.item { - break; - } match line_editor.read_line(&prompt) { Ok(Signal::Success(buffer)) => { buf.push_str(&buffer); diff --git a/crates/nu-command/src/platform/input/reedline_prompt.rs b/crates/nu-command/src/platform/input/reedline_prompt.rs index b2e08f766a..37e74fef77 100644 --- a/crates/nu-command/src/platform/input/reedline_prompt.rs +++ b/crates/nu-command/src/platform/input/reedline_prompt.rs @@ -16,6 +16,7 @@ pub static DEFAULT_MULTILINE_INDICATOR: &str = "::: "; pub struct ReedlinePrompt { /// What segment should be rendered in the left (main) prompt pub left_prompt: String, + pub right_prompt: String, pub indicator: String, } @@ -25,7 +26,7 @@ impl Prompt for ReedlinePrompt { } fn render_prompt_right(&self) -> Cow { - Cow::Borrowed("") + Cow::Borrowed(&self.right_prompt) } fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow {