Skip to content

Commit

Permalink
PromptInputLine: fixup per code review
Browse files Browse the repository at this point in the history
Apply my suggestions from code review.

closes: #6007
  • Loading branch information
wez committed Sep 22, 2024
1 parent b59cc5b commit 67603e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/config/lua/keyassignment/PromptInputLine.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 18 additions & 2 deletions termwiz/src/lineedit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>> {
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<Option<String>> {
ensure!(
self.state == EditorState::Inactive,
"recursive call to read_line!"
Expand All @@ -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() {
Expand Down Expand Up @@ -810,7 +818,15 @@ impl<'term> LineEditor<'term> {
Ok(())
}

fn read_line_impl(&mut self, host: &mut dyn LineEditorHost) -> Result<Option<String>> {
fn read_line_impl(
&mut self,
host: &mut dyn LineEditorHost,
initial_value: Option<&str>,
) -> Result<Option<String>> {
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();
Expand Down
6 changes: 2 additions & 4 deletions wezterm-gui/src/overlay/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 67603e7

Please sign in to comment.