From 67603e73e05cc6c3818a6b474b923f0e9e6ce971 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 22 Sep 2024 14:44:05 -0700 Subject: [PATCH] PromptInputLine: fixup per code review Apply my suggestions from code review. closes: https://github.com/wez/wezterm/pull/6007 --- .../lua/keyassignment/PromptInputLine.md | 2 +- termwiz/src/lineedit/mod.rs | 20 +++++++++++++++++-- wezterm-gui/src/overlay/prompt.rs | 6 ++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/config/lua/keyassignment/PromptInputLine.md b/docs/config/lua/keyassignment/PromptInputLine.md index 955e19d8184..890bd63bd05 100644 --- a/docs/config/lua/keyassignment/PromptInputLine.md +++ b/docs/config/lua/keyassignment/PromptInputLine.md @@ -22,7 +22,7 @@ upon the input. and/or use [wezterm.format](../wezterm/format.md). Defaults to: `"> "`. {{since('nightly', inline=True)}} * `initial_value` - optional. If provided, the initial content of the input field will be set to this value. The user may edit it prior to submitting - the input. + the input. {{since('nightly', inline=True)}} ## Example of interactively renaming the current tab diff --git a/termwiz/src/lineedit/mod.rs b/termwiz/src/lineedit/mod.rs index 878671b10d6..7980f17f987 100644 --- a/termwiz/src/lineedit/mod.rs +++ b/termwiz/src/lineedit/mod.rs @@ -299,6 +299,14 @@ impl<'term> LineEditor<'term> { /// accepted, or until an error is detected. /// Returns Ok(None) if the editor was cancelled eg: via CTRL-C. pub fn read_line(&mut self, host: &mut dyn LineEditorHost) -> Result> { + self.read_line_with_optional_initial_value(host, None) + } + + pub fn read_line_with_optional_initial_value( + &mut self, + host: &mut dyn LineEditorHost, + initial_value: Option<&str>, + ) -> Result> { ensure!( self.state == EditorState::Inactive, "recursive call to read_line!" @@ -311,7 +319,7 @@ impl<'term> LineEditor<'term> { self.terminal.set_raw_mode()?; self.state = EditorState::Editing; - let res = self.read_line_impl(host); + let res = self.read_line_impl(host, initial_value); self.state = EditorState::Inactive; if let Some(move_end) = self.move_to_editor_end.take() { @@ -810,7 +818,15 @@ impl<'term> LineEditor<'term> { Ok(()) } - fn read_line_impl(&mut self, host: &mut dyn LineEditorHost) -> Result> { + fn read_line_impl( + &mut self, + host: &mut dyn LineEditorHost, + initial_value: Option<&str>, + ) -> Result> { + self.line.clear(); + if let Some(value) = initial_value { + self.line.set_line_and_cursor(value, value.len()); + } self.history_pos = None; self.bottom_line = None; self.clear_completion(); diff --git a/wezterm-gui/src/overlay/prompt.rs b/wezterm-gui/src/overlay/prompt.rs index f67d93ba663..4c2449b8491 100644 --- a/wezterm-gui/src/overlay/prompt.rs +++ b/wezterm-gui/src/overlay/prompt.rs @@ -68,10 +68,8 @@ pub fn show_line_prompt_overlay( let mut host = PromptHost::new(); let mut editor = LineEditor::new(&mut term); editor.set_prompt(&args.prompt); - if let Some(value) = &args.initial_value { - editor.set_line_and_cursor(value, value.len()); - } - let line = editor.read_line(&mut host)?; + let line = + editor.read_line_with_optional_initial_value(&mut host, args.initial_value.as_deref())?; promise::spawn::spawn_into_main_thread(async move { trampoline(name, window, pane, line);