Fix yet another missing history bounds check

master
itycodes 6 hours ago
parent b2d211a7dc
commit c249c3a7bc

@ -84,10 +84,10 @@ impl ReaderState {
self.redraw(); self.redraw();
} }
fn current_hist(&mut self) -> String { fn current_hist(&mut self) -> Option<String> {
let i: usize = self.history_inx.unwrap(); let i: usize = self.history_inx.unwrap();
let line = self.history.get(self.history.len().saturating_sub(i+1)); 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) { fn set_current_hist(&mut self, str: String) {
@ -97,7 +97,7 @@ impl ReaderState {
} }
fn insert_at_cursor_hist(&mut self, txt: String) { 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()); line.insert_str(self.cursor, txt.as_str());
self.history_reset(); self.history_reset();
self.set_line(line.clone()); self.set_line(line.clone());
@ -177,6 +177,7 @@ impl ReaderState {
pub fn read_line(&mut self) -> Result<String, ReadError> { pub fn read_line(&mut self) -> Result<String, ReadError> {
use EditCommand::*; use EditCommand::*;
loop { loop {
self.set_prompt(format!("{:?}> ", self.history_inx));
let cmd = self.input_line(); let cmd = self.input_line();
if cmd.is_err() { if cmd.is_err() {
return Err(cmd.err().unwrap()); return Err(cmd.err().unwrap());
@ -232,7 +233,7 @@ impl ReaderState {
pub fn history_back(&mut self) { pub fn history_back(&mut self) {
let h = self.history.clone(); let h = self.history.clone();
let mut i = self.history_inx; 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(); bell();
return; return;
} }
@ -320,7 +321,7 @@ impl ReaderState {
pub fn backspace(&mut self) { pub fn backspace(&mut self) {
if self.history_inx.is_some() { if self.history_inx.is_some() {
if self.cursor > 0 { 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); line.remove(self.cursor-1);
self.move_left(); self.move_left();
self.history_reset(); self.history_reset();

Loading…
Cancel
Save