diff --git a/repline/src/editor.rs b/repline/src/editor.rs index a8e19d1..3a8941c 100644 --- a/repline/src/editor.rs +++ b/repline/src/editor.rs @@ -112,6 +112,17 @@ impl<'a> Editor<'a> { Ok(()) } + pub fn print_err(&self, w: &mut W, err: impl Display) -> ReplResult<()> { + queue!( + w, + SavePosition, + Clear(ClearType::UntilNewLine), + Print(err), + RestorePosition + )?; + Ok(()) + } + /// Prints the characters after the cursor on the current line. pub fn print_tail(&self, w: &mut W) -> ReplResult<()> { let Self { tail, .. } = self; diff --git a/repline/src/prebaked.rs b/repline/src/prebaked.rs index ba2aa15..fba802c 100644 --- a/repline/src/prebaked.rs +++ b/repline/src/prebaked.rs @@ -49,7 +49,7 @@ where F: FnMut(&str) -> Result> { Ok(Response::Deny) => rl.deny(), Ok(Response::Break) => break, Ok(Response::Continue) => continue, - Err(e) => print!("\x1b[40G\x1b[A\x1bJ\x1b[91m{e}\x1b[0m\x1b[B"), + Err(e) => rl.print_inline(format_args!("\x1b[40G\x1b[91m{e}\x1b[0m"))?, } } Ok(()) diff --git a/repline/src/repline.rs b/repline/src/repline.rs index 4dc19e7..62c4733 100644 --- a/repline/src/repline.rs +++ b/repline/src/repline.rs @@ -111,6 +111,15 @@ impl<'a, R: Read> Repline<'a, R> { } } } + /// Prints a message without moving the cursor + pub fn print_inline(&mut self, value: impl std::fmt::Display) -> ReplResult<()> { + let mut stdout = stdout().lock(); + self.print_err(&mut stdout, value) + } + /// Prints a message (ideally an error) without moving the cursor + fn print_err(&mut self, w: &mut W, value: impl std::fmt::Display) -> ReplResult<()> { + self.ed.print_err(w, value) + } /// Handle ANSI Escape fn escape(&mut self, w: &mut W) -> ReplResult<()> { match self.input.next().ok_or(Error::EndOfInput)?? {