From d12bdf149455cd6fe7c485e14e876144d175eb74 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 23 May 2025 10:55:38 +0800 Subject: [PATCH] fix the case of trying to retrieve shell history when none exists fortunately rust handles this with a panic because it has strict bounds checking, so this cleans up that panic. --- services/bao-console/src/repl.rs | 17 ++++++++++++----- services/bao-console/src/shell.rs | 8 +++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/services/bao-console/src/repl.rs b/services/bao-console/src/repl.rs index ece4d1496..82d428fc2 100644 --- a/services/bao-console/src/repl.rs +++ b/services/bao-console/src/repl.rs @@ -55,11 +55,18 @@ impl Repl { // An index of -1 gives the most recent command. // -2 would give the second most recent command - pub(crate) fn get_history(&mut self, index: isize) -> &str { - &self.history[(self.history.len() as isize + index) - .max(0) - .min(self.history.len().saturating_sub(1) as isize) as usize] - .text + pub(crate) fn get_history(&mut self, index: isize) -> Option<&str> { + if self.history.len() != 0 { + Some( + &self.history[(self.history.len() as isize + index) + .max(0) + .min(self.history.len().saturating_sub(1) as isize) + as usize] + .text, + ) + } else { + None + } } /// update the loop, in response to various inputs diff --git a/services/bao-console/src/shell.rs b/services/bao-console/src/shell.rs index d32b08df1..19e38eb97 100644 --- a/services/bao-console/src/shell.rs +++ b/services/bao-console/src/shell.rs @@ -69,9 +69,11 @@ fn shell() { print!("\r{}", spaces); } input.clear(); - input.push_str(repl.get_history(history_index)); - // print the new input - println!("{}", &input); + if let Some(s) = repl.get_history(history_index) { + input.push_str(s); + // print the new input + println!("{}", &input); + } } else if k != '\u{0000}' && k != '\n' && k != '\r' { input.push(k); } else if k == '\n' || k == '\r' {