use super::App;
impl App {
pub fn add_to_command_history(&mut self, command: String) {
if command.is_empty() {
return;
}
if let Some(pos) = self.command_history.iter().position(|x| x == &command) {
self.command_history.remove(pos);
}
self.command_history.push(command);
if self.command_history.len() > 10 {
self.command_history.remove(0);
}
self.command_history_index = None;
}
pub fn add_to_search_history(&mut self, search: String) {
if search.is_empty() {
return;
}
if let Some(pos) = self.search_history.iter().position(|x| x == &search) {
self.search_history.remove(pos);
}
self.search_history.push(search);
if self.search_history.len() > 10 {
self.search_history.remove(0);
}
self.search_history_index = None;
}
pub fn get_previous_command(&mut self) -> Option<String> {
if self.command_history.is_empty() {
return None;
}
let index = match self.command_history_index {
None => self.command_history.len() - 1,
Some(i) if i > 0 => i - 1,
Some(i) => i,
};
self.command_history_index = Some(index);
self.command_history.get(index).cloned()
}
pub fn get_next_command(&mut self) -> Option<String> {
if self.command_history.is_empty() {
return None;
}
match self.command_history_index {
None => None,
Some(i) if i + 1 < self.command_history.len() => {
self.command_history_index = Some(i + 1);
self.command_history.get(i + 1).cloned()
}
Some(_) => {
self.command_history_index = None;
Some(String::new())
}
}
}
pub fn get_previous_search(&mut self) -> Option<String> {
if self.search_history.is_empty() {
return None;
}
let index = match self.search_history_index {
None => self.search_history.len() - 1,
Some(i) if i > 0 => i - 1,
Some(i) => i,
};
self.search_history_index = Some(index);
self.search_history.get(index).cloned()
}
pub fn get_next_search(&mut self) -> Option<String> {
if self.search_history.is_empty() {
return None;
}
match self.search_history_index {
None => None,
Some(i) if i + 1 < self.search_history.len() => {
self.search_history_index = Some(i + 1);
self.search_history.get(i + 1).cloned()
}
Some(_) => {
self.search_history_index = None;
Some(String::new())
}
}
}
}