Compare commits

...

2 Commits

Author SHA1 Message Date
tranquillity-codes a0f4ebc150 Fix yet another missing history bounds check
6 days ago
tranquillity-codes f7dea3928a Add EoT handling
6 days ago

@ -105,6 +105,10 @@ impl SequenceState {
self.out_buf.push(InputChar::Backspace);
Result::Ok(())
}
else if chr == 0x04 {
self.out_buf.push(InputChar::EoT);
Result::Ok(())
}
// Unrecognized
else {
Result::Err(AcceptError::Unimplemented(chr))

@ -1,6 +1,6 @@
#![allow(unused)]
use std::mem::MaybeUninit;
use std::{io::Read, mem::MaybeUninit};
use crate::reader::esc_parse::{InputChar, bell, hide_cursor, show_cursor};
@ -24,7 +24,8 @@ pub enum InitTTYError {
#[derive(Debug)]
pub enum ReadError {
EscapeError(esc_parse::AcceptError)
EscapeError(esc_parse::AcceptError),
Exited,
}
#[derive(Debug)]
@ -35,6 +36,7 @@ pub enum EditCommand {
CursorBack,
CursorForward,
DeleteBack,
Exit,
}
macro_rules! edit_cmd {
@ -82,10 +84,10 @@ impl ReaderState {
self.redraw();
}
fn current_hist(&mut self) -> String {
fn current_hist(&mut self) -> Option<String> {
let i: usize = self.history_inx.unwrap();
let line = self.history.get(self.history.len().saturating_sub(i+1));
return line.unwrap().to_string();
return line.map(|s| s.to_string());
}
fn set_current_hist(&mut self, str: String) {
@ -95,7 +97,7 @@ impl ReaderState {
}
fn insert_at_cursor_hist(&mut self, txt: String) {
let mut line = self.current_hist();
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
line.insert_str(self.cursor, txt.as_str());
self.history_reset();
self.set_line(line.clone());
@ -145,6 +147,9 @@ impl ReaderState {
if chr == InputChar::Backspace {
return edit_cmd!(DeleteBack);
}
if chr == InputChar::EoT {
return edit_cmd!(Exit);
}
}
}
}
@ -172,6 +177,7 @@ impl ReaderState {
pub fn read_line(&mut self) -> Result<String, ReadError> {
use EditCommand::*;
loop {
self.set_prompt(format!("{:?}> ", self.history_inx));
let cmd = self.input_line();
if cmd.is_err() {
return Err(cmd.err().unwrap());
@ -197,7 +203,8 @@ impl ReaderState {
},
HistoryNext => {
self.history_front();
}
},
Exit => return Err(ReadError::Exited)
}
}
}
@ -226,7 +233,7 @@ impl ReaderState {
pub fn history_back(&mut self) {
let h = self.history.clone();
let mut i = self.history_inx;
if i.map_or(false, |v| v+1 == h.len()) {
if i.map_or(h.len() == 0, |v| v+1 == h.len()) {
bell();
return;
}
@ -314,7 +321,7 @@ impl ReaderState {
pub fn backspace(&mut self) {
if self.history_inx.is_some() {
if self.cursor > 0 {
let mut line = self.current_hist();
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
line.remove(self.cursor-1);
self.move_left();
self.history_reset();

Loading…
Cancel
Save