From 24c612a87617b49fa4ce129070b12d65d4f8aa67 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 6 Aug 2024 07:42:06 +0800 Subject: [PATCH 1/2] feat: add ClearDisplay event Signed-off-by: tison --- src/engine.rs | 16 ++++++++++++++++ src/enums.rs | 4 ++++ src/painting/painter.rs | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/engine.rs b/src/engine.rs index 3c4fa2a0..c958a73a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -666,6 +666,13 @@ impl Reedline { self.painter.paint_line(msg) } + /// + pub fn clear_display(&mut self) -> Result<()> { + self.painter.clear_display()?; + + Ok(()) + } + /// Clear the screen by printing enough whitespace to start the prompt or /// other output back at the first line of the terminal. pub fn clear_screen(&mut self) -> Result<()> { @@ -848,6 +855,10 @@ impl Reedline { self.input_mode = InputMode::Regular; Ok(EventStatus::Exits(Signal::CtrlC)) } + ReedlineEvent::ClearDisplay => { + self.painter.clear_display()?; + Ok(EventStatus::Handled) + } ReedlineEvent::ClearScreen => { self.painter.clear_screen()?; Ok(EventStatus::Handled) @@ -1072,6 +1083,11 @@ impl Reedline { self.editor.reset_undo_stack(); Ok(EventStatus::Exits(Signal::CtrlC)) } + ReedlineEvent::ClearDisplay => { + self.deactivate_menus(); + self.painter.clear_display()?; + Ok(EventStatus::Handled) + } ReedlineEvent::ClearScreen => { self.deactivate_menus(); self.painter.clear_screen()?; diff --git a/src/enums.rs b/src/enums.rs index b56896be..75324b6b 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -552,6 +552,9 @@ pub enum ReedlineEvent { /// Bubble up [`Signal::CtrlC`] CtrlC, + /// Clears the display and set prompt to first line + ClearDisplay, + /// Clears the screen and sets prompt to first line ClearScreen, @@ -654,6 +657,7 @@ impl Display for ReedlineEvent { ReedlineEvent::HistoryHintWordComplete => write!(f, "HistoryHintWordComplete"), ReedlineEvent::CtrlD => write!(f, "CtrlD"), ReedlineEvent::CtrlC => write!(f, "CtrlC"), + ReedlineEvent::ClearDisplay => write!(f, "ClearDisplay"), ReedlineEvent::ClearScreen => write!(f, "ClearScreen"), ReedlineEvent::ClearScrollback => write!(f, "ClearScrollback"), ReedlineEvent::Enter => write!(f, "Enter"), diff --git a/src/painting/painter.rs b/src/painting/painter.rs index 233e98f8..f0f6234c 100644 --- a/src/painting/painter.rs +++ b/src/painting/painter.rs @@ -507,6 +507,16 @@ impl Painter { self.stdout.flush() } + /// + pub(crate) fn clear_display(&mut self) -> Result<()> { + self.stdout.queue(cursor::Hide)?; + self.stdout.queue(Clear(ClearType::All))?; + self.stdout.queue(MoveTo(0, 0))?; + self.stdout.queue(cursor::Show)?; + self.stdout.flush()?; + self.initialize_prompt_position(None) + } + /// Clear the screen by printing enough whitespace to start the prompt or /// other output back at the first line of the terminal. pub(crate) fn clear_screen(&mut self) -> Result<()> { From ad9c315b305c9f0f80583b8789a4428fb5b2e700 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 7 Aug 2024 08:39:49 +0800 Subject: [PATCH 2/2] take 2 - reimpl clear_screen Signed-off-by: tison --- src/engine.rs | 16 ---------------- src/enums.rs | 4 ---- src/painting/painter.rs | 29 +++++++---------------------- 3 files changed, 7 insertions(+), 42 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index c958a73a..3c4fa2a0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -666,13 +666,6 @@ impl Reedline { self.painter.paint_line(msg) } - /// - pub fn clear_display(&mut self) -> Result<()> { - self.painter.clear_display()?; - - Ok(()) - } - /// Clear the screen by printing enough whitespace to start the prompt or /// other output back at the first line of the terminal. pub fn clear_screen(&mut self) -> Result<()> { @@ -855,10 +848,6 @@ impl Reedline { self.input_mode = InputMode::Regular; Ok(EventStatus::Exits(Signal::CtrlC)) } - ReedlineEvent::ClearDisplay => { - self.painter.clear_display()?; - Ok(EventStatus::Handled) - } ReedlineEvent::ClearScreen => { self.painter.clear_screen()?; Ok(EventStatus::Handled) @@ -1083,11 +1072,6 @@ impl Reedline { self.editor.reset_undo_stack(); Ok(EventStatus::Exits(Signal::CtrlC)) } - ReedlineEvent::ClearDisplay => { - self.deactivate_menus(); - self.painter.clear_display()?; - Ok(EventStatus::Handled) - } ReedlineEvent::ClearScreen => { self.deactivate_menus(); self.painter.clear_screen()?; diff --git a/src/enums.rs b/src/enums.rs index 75324b6b..b56896be 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -552,9 +552,6 @@ pub enum ReedlineEvent { /// Bubble up [`Signal::CtrlC`] CtrlC, - /// Clears the display and set prompt to first line - ClearDisplay, - /// Clears the screen and sets prompt to first line ClearScreen, @@ -657,7 +654,6 @@ impl Display for ReedlineEvent { ReedlineEvent::HistoryHintWordComplete => write!(f, "HistoryHintWordComplete"), ReedlineEvent::CtrlD => write!(f, "CtrlD"), ReedlineEvent::CtrlC => write!(f, "CtrlC"), - ReedlineEvent::ClearDisplay => write!(f, "ClearDisplay"), ReedlineEvent::ClearScreen => write!(f, "ClearScreen"), ReedlineEvent::ClearScrollback => write!(f, "ClearScrollback"), ReedlineEvent::Enter => write!(f, "Enter"), diff --git a/src/painting/painter.rs b/src/painting/painter.rs index f0f6234c..8365f072 100644 --- a/src/painting/painter.rs +++ b/src/painting/painter.rs @@ -507,36 +507,21 @@ impl Painter { self.stdout.flush() } - /// - pub(crate) fn clear_display(&mut self) -> Result<()> { - self.stdout.queue(cursor::Hide)?; - self.stdout.queue(Clear(ClearType::All))?; - self.stdout.queue(MoveTo(0, 0))?; - self.stdout.queue(cursor::Show)?; - self.stdout.flush()?; - self.initialize_prompt_position(None) - } - /// Clear the screen by printing enough whitespace to start the prompt or /// other output back at the first line of the terminal. pub(crate) fn clear_screen(&mut self) -> Result<()> { - self.stdout.queue(cursor::Hide)?; - let (_, num_lines) = terminal::size()?; - for _ in 0..2 * num_lines { - self.stdout.queue(Print("\n"))?; - } - self.stdout.queue(MoveTo(0, 0))?; - self.stdout.queue(cursor::Show)?; - - self.stdout.flush()?; + self.stdout + .queue(Clear(ClearType::All))? + .queue(MoveTo(0, 0))? + .flush()?; self.initialize_prompt_position(None) } pub(crate) fn clear_scrollback(&mut self) -> Result<()> { self.stdout - .queue(crossterm::terminal::Clear(ClearType::All))? - .queue(crossterm::terminal::Clear(ClearType::Purge))? - .queue(cursor::MoveTo(0, 0))? + .queue(Clear(ClearType::All))? + .queue(Clear(ClearType::Purge))? + .queue(MoveTo(0, 0))? .flush()?; self.initialize_prompt_position(None) }