repline: change error formatting

This commit is contained in:
John 2025-02-20 22:05:43 -06:00
parent 088cd4d1e4
commit eebabf02fb
3 changed files with 21 additions and 1 deletions

View File

@ -112,6 +112,17 @@ impl<'a> Editor<'a> {
Ok(())
}
pub fn print_err<W: Write>(&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<W: Write>(&self, w: &mut W) -> ReplResult<()> {
let Self { tail, .. } = self;

View File

@ -49,7 +49,7 @@ where F: FnMut(&str) -> Result<Response, Box<dyn Error>> {
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(())

View File

@ -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<W: Write>(&mut self, w: &mut W, value: impl std::fmt::Display) -> ReplResult<()> {
self.ed.print_err(w, value)
}
/// Handle ANSI Escape
fn escape<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
match self.input.next().ok_or(Error::EndOfInput)?? {