repline: Change newline behavior when not at end of input

TODO: This screws with the naming convention of home()/end()
This commit is contained in:
John 2025-05-17 20:32:24 -04:00
parent dc1c9bdd6d
commit a023551d9f
2 changed files with 17 additions and 3 deletions

View File

@ -237,9 +237,18 @@ impl<'a> Editor<'a> {
self.head.len() + self.tail.len() 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. /// Returns true if the buffer is empty.
pub fn is_empty(&self) -> bool { 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 /// Returns true if the buffer ends with a given pattern

View File

@ -5,7 +5,7 @@
use crate::{editor::Editor, error::*, iter::*, raw::raw}; use crate::{editor::Editor, error::*, iter::*, raw::raw};
use std::{ use std::{
collections::VecDeque, 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. /// 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. // ignore newlines, process line feeds. Not sure how cross-platform this is.
'\n' => {} '\n' => {}
'\r' => { '\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()); return Ok(self.ed.to_string());
} }
// Ctrl+Backspace in my terminal // Ctrl+Backspace in my terminal