diff --git a/repline/src/editor.rs b/repline/src/editor.rs index 3a8941c..f2e173b 100644 --- a/repline/src/editor.rs +++ b/repline/src/editor.rs @@ -237,9 +237,18 @@ impl<'a> Editor<'a> { self.head.len() + self.tail.len() } + /// Returns true if the cursor is at the beginning + pub fn at_start(&self) -> bool { + self.head.is_empty() + } + /// Returns true if the cursor is at the end + pub fn at_end(&self) -> bool { + self.tail.is_empty() + } + /// Returns true if the buffer is empty. pub fn is_empty(&self) -> bool { - self.head.is_empty() && self.tail.is_empty() + self.at_start() && self.at_end() } /// Returns true if the buffer ends with a given pattern diff --git a/repline/src/repline.rs b/repline/src/repline.rs index 427ce9d..df19416 100644 --- a/repline/src/repline.rs +++ b/repline/src/repline.rs @@ -5,7 +5,7 @@ use crate::{editor::Editor, error::*, iter::*, raw::raw}; use std::{ collections::VecDeque, - io::{stdout, Bytes, Read, Result, Write}, + io::{Bytes, Read, Result, Write, stdout}, }; /// Prompts the user, reads the lines. Not much more to it than that. @@ -81,7 +81,12 @@ impl<'a, R: Read> Repline<'a, R> { // ignore newlines, process line feeds. Not sure how cross-platform this is. '\n' => {} '\r' => { - self.ed.push('\n', stdout)?; + if self.ed.at_end() { + self.ed.push('\n', stdout)?; + } else { + self.ed.end(stdout)?; + writeln!(stdout)?; + } return Ok(self.ed.to_string()); } // Ctrl+Backspace in my terminal