From f1f7ebe1be22308994808ba6573bd7bb97c6fd82 Mon Sep 17 00:00:00 2001 From: Benoit de Chezelles Date: Sun, 14 Apr 2024 12:38:35 +0200 Subject: [PATCH] cargo fmt --- src/engine.rs | 3 ++- src/painting/painter.rs | 46 +++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index ed1c4a2d..047b76bd 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -672,7 +672,8 @@ impl Reedline { /// Helper implementing the logic for [`Reedline::read_line()`] to be wrapped /// in a `raw_mode` context. fn read_line_helper(&mut self, prompt: &dyn Prompt) -> Result { - self.painter.initialize_prompt_position(self.suspended_state.as_ref())?; + self.painter + .initialize_prompt_position(self.suspended_state.as_ref())?; if self.suspended_state.is_some() { // Last editor was suspended to run a ExecuteHostCommand event, // we are resuming operation now. diff --git a/src/painting/painter.rs b/src/painting/painter.rs index d2dfac9f..e0216b50 100644 --- a/src/painting/painter.rs +++ b/src/painting/painter.rs @@ -73,7 +73,7 @@ fn select_prompt_row( if painter_state.previous_prompt_rows_range.contains(&row) { // Cursor is still in the range of the previous prompt, re-use it. let start_row = *painter_state.previous_prompt_rows_range.start(); - return PromptRowSelector::UseExistingPrompt { start_row } + return PromptRowSelector::UseExistingPrompt { start_row }; } else { // There was some output or cursor is outside of the range of previous prompt make a // fresh new prompt. @@ -138,7 +138,10 @@ impl Painter { /// /// Not to be used for resizes during a running line editor, use /// [`Painter::handle_resize()`] instead - pub(crate) fn initialize_prompt_position(&mut self, suspended_state: Option<&PainterSuspendedState>) -> Result<()> { + pub(crate) fn initialize_prompt_position( + &mut self, + suspended_state: Option<&PainterSuspendedState>, + ) -> Result<()> { // Update the terminal size self.terminal_size = { let size = terminal::size()?; @@ -150,10 +153,9 @@ impl Painter { size } }; - match select_prompt_row(suspended_state, cursor::position()?) { - PromptRowSelector::UseExistingPrompt { start_row } => { - self.prompt_start_row = start_row - } + let prompt_selector = select_prompt_row(suspended_state, cursor::position()?); + self.prompt_start_row = match prompt_selector { + PromptRowSelector::UseExistingPrompt { start_row } => start_row, PromptRowSelector::MakeNewPrompt { new_row } => { // If we are on the last line and would move beyond the last line, we need to make // room for the prompt. @@ -161,12 +163,12 @@ impl Painter { // origin, causing issues after repaints. if new_row == self.screen_height() { self.print_crlf()?; - self.prompt_start_row = new_row.saturating_sub(1) + new_row.saturating_sub(1) } else { - self.prompt_start_row = new_row + new_row } } - } + }; Ok(()) } @@ -671,24 +673,32 @@ mod tests { #[test] fn test_select_new_prompt_with_no_state_no_output() { - let selector = select_prompt_row(None, (0, 12)); - assert_eq!(selector, PromptRowSelector::MakeNewPrompt { new_row: 12 }) + assert_eq!( + select_prompt_row(None, (0, 12)), + PromptRowSelector::MakeNewPrompt { new_row: 12 } + ); } #[test] fn test_select_new_prompt_with_no_state_but_output() { - let selector = select_prompt_row(None, (3, 12)); - assert_eq!(selector, PromptRowSelector::MakeNewPrompt { new_row: 13 }) + assert_eq!( + select_prompt_row(None, (3, 12)), + PromptRowSelector::MakeNewPrompt { new_row: 13 } + ); } #[test] fn test_select_existing_prompt() { let state = PainterSuspendedState { - previous_prompt_rows_range: 11..=13 + previous_prompt_rows_range: 11..=13, }; - let selector = select_prompt_row(Some(&state), (0, 12)); - assert_eq!(selector, PromptRowSelector::UseExistingPrompt { start_row: 11 }); - let selector = select_prompt_row(Some(&state), (3, 12)); - assert_eq!(selector, PromptRowSelector::UseExistingPrompt { start_row: 11 }); + assert_eq!( + select_prompt_row(Some(&state), (0, 12)), + PromptRowSelector::UseExistingPrompt { start_row: 11 } + ); + assert_eq!( + select_prompt_row(Some(&state), (3, 12)), + PromptRowSelector::UseExistingPrompt { start_row: 11 } + ); } }