repline: Word-deletion, and proper history reloading!

This commit is contained in:
John 2024-04-16 23:47:10 -05:00
parent eee9e99aed
commit 0e8b4f68c3

View File

@ -241,6 +241,10 @@ impl<'a, R: Read> Repline<'a, R> {
self.ed.push('\n', stdout)?; self.ed.push('\n', stdout)?;
return Ok(self.ed.to_string()); return Ok(self.ed.to_string());
} }
// Ctrl+Backspace in my terminal
'\x17' => {
self.ed.erase_word(stdout)?;
}
// Escape sequence // Escape sequence
'\x1b' => self.escape(stdout)?, '\x1b' => self.escape(stdout)?,
// backspace // backspace
@ -282,10 +286,7 @@ impl<'a, R: Read> Repline<'a, R> {
self.restore_history(w)? self.restore_history(w)?
} }
'B' => { 'B' => {
self.hindex = self self.hindex = self.hindex.saturating_add(1).min(self.history.len());
.hindex
.saturating_add(1)
.min(self.history.len().saturating_sub(1));
self.restore_history(w)? self.restore_history(w)?
} }
'C' => self.ed.cursor_forward(1, w)?, 'C' => self.ed.cursor_forward(1, w)?,
@ -308,19 +309,13 @@ impl<'a, R: Read> Repline<'a, R> {
/// Restores the currently selected history /// Restores the currently selected history
pub fn restore_history<W: Write>(&mut self, w: &mut W) -> ReplResult<()> { pub fn restore_history<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
let Self { history, hindex, ed, .. } = self; let Self { history, hindex, ed, .. } = self;
if !(0..history.len()).contains(hindex) {
return Ok(());
};
ed.undraw(w)?; ed.undraw(w)?;
ed.clear(); ed.clear();
ed.print_head(w)?; ed.print_head(w)?;
ed.extend( if let Some(history) = history.get(*hindex) {
history ed.extend(history.chars(), w)?
.get(*hindex) }
.expect("history should contain index") Ok(())
.chars(),
w,
)
} }
/// Append line to history and clear it /// Append line to history and clear it
@ -538,6 +533,10 @@ pub mod editor {
} }
.ok_or(Error::EndOfInput) .ok_or(Error::EndOfInput)
} }
pub fn erase_word<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
while self.pop(w)?.filter(|c| !c.is_whitespace()).is_some() {}
Ok(())
}
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.head.len() + self.tail.len() self.head.len() + self.tail.len()
} }