From 2f94ddd23fb00141105973df0006692cbd77d708 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 18 Jul 2025 05:35:00 -0400 Subject: [PATCH] repline: QoL improvement: inserting newline works! --- repline/src/editor.rs | 1 + repline/src/repline.rs | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/repline/src/editor.rs b/repline/src/editor.rs index 280a03f..903028b 100644 --- a/repline/src/editor.rs +++ b/repline/src/editor.rs @@ -125,6 +125,7 @@ impl<'a> Editor<'a> { /// Writes a character at the cursor, shifting the text around as necessary. pub fn push(&mut self, c: char, w: &mut W) -> ReplResult<()> { self.head.push_back(c); + queue!(w, Clear(ClearType::UntilNewLine))?; self.putchar(c, w)?; match c { '\n' => self.redraw_tail(w), diff --git a/repline/src/repline.rs b/repline/src/repline.rs index 662b476..8a9548f 100644 --- a/repline/src/repline.rs +++ b/repline/src/repline.rs @@ -86,12 +86,10 @@ 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.cursor_line_end(stdout)?; + return Ok(self.ed.to_string()); } - return Ok(self.ed.to_string()); } // Ctrl+Backspace in my terminal '\x17' => self.ed.erase_word(stdout)?, @@ -147,12 +145,11 @@ impl<'a, R: Read> Repline<'a, R> { 'A' if self.ed.at_start() && self.hindex > 0 => { self.hindex -= 1; self.restore_history(w)?; - self.ed.cursor_start(w)?; } 'A' => self.ed.cursor_up(w)?, - 'B' if self.ed.at_end() && self.hindex < self.history.len() => { - self.restore_history(w)?; + 'B' if self.ed.at_end() && self.hindex < self.history.len().saturating_sub(1) => { self.hindex += 1; + self.restore_history(w)?; } 'B' => self.ed.cursor_down(w)?, 'C' => self.ed.cursor_forward(w)?, @@ -187,7 +184,10 @@ impl<'a, R: Read> Repline<'a, R> { let Self { history, hindex, ed, .. } = self; if let Some(history) = history.get(*hindex) { ed.restore(history, w)?; - ed.print_err(format_args!(" History {hindex} restored!"), w)?; + ed.print_err( + format_args!("\t\x1b[30mHistory {hindex} restored!\x1b[0m"), + w, + )?; } Ok(()) } @@ -197,9 +197,12 @@ impl<'a, R: Read> Repline<'a, R> { while buf.ends_with(char::is_whitespace) { buf.pop(); } - if !self.history.contains(&buf) { - self.history.push_back(buf) - } + if let Some(idx) = self.history.iter().position(|v| *v == buf) { + self.history + .remove(idx) + .expect("should have just found this"); + }; + self.history.push_back(buf); while self.history.len() > 20 { self.history.pop_front(); }