diff --git a/examples/demo.rs b/examples/demo.rs index ad1f808a..74e03c12 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -16,7 +16,7 @@ use { #[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))] use reedline::FileBackedHistory; -use reedline::{CursorConfig, MenuBuilder}; +use reedline::{CursorConfig, HistoryItemId, MenuBuilder}; fn main() -> reedline::Result<()> { println!("Ctrl-D to quit"); @@ -175,6 +175,18 @@ fn main() -> reedline::Result<()> { line_editor.print_history_session()?; continue; } + // Delete history entry of a certain id + if buffer.trim().starts_with("history delete-item") { + let parts: Vec<&str> = buffer.split_whitespace().collect(); + if parts.len() == 3 { + if let Ok(id) = parts[2].parse::() { + line_editor.history_mut().delete(HistoryItemId::new(id))?; + continue; + } + } + println!("Invalid command. Use: history delete "); + continue; + } // Get this history session identifier if buffer.trim() == "history sessionid" { line_editor.print_history_session_id()?; diff --git a/src/engine.rs b/src/engine.rs index e7bcceb7..dd2a12fb 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -536,8 +536,9 @@ impl Reedline { .search(SearchQuery::everything(SearchDirection::Forward, None)) .expect("todo: error handling"); - for (i, entry) in history.iter().enumerate() { - self.print_line(&format!("{}\t{}", i, entry.command_line))?; + for entry in history.iter() { + let Some(id) = entry.id else { continue }; + self.print_line(&format!("{}\t{}", id, entry.command_line))?; } Ok(()) } diff --git a/src/history/file_backed.rs b/src/history/file_backed.rs index ffff840b..125fd4c3 100644 --- a/src/history/file_backed.rs +++ b/src/history/file_backed.rs @@ -205,13 +205,20 @@ impl History for FileBackedHistory { Ok(()) } - fn delete(&mut self, _h: super::HistoryItemId) -> Result<()> { - Err(ReedlineError( - ReedlineErrorVariants::HistoryFeatureUnsupported { - history: "FileBackedHistory", - feature: "removing entries", - }, - )) + fn delete(&mut self, h: super::HistoryItemId) -> Result<()> { + let id = h.0 as usize; + let num_entries = self.entries.len(); + // Check if the id is valid + if id >= num_entries { + return Err(ReedlineError(ReedlineErrorVariants::OtherHistoryError( + "Given id is out of range.", + ))); + } + + // Remove the item with the specified id + self.entries.remove(id); + + Ok(()) } /// Writes unwritten history contents to disk.