Skip to content

Commit

Permalink
Add History::delete for FileBackedHistory
Browse files Browse the repository at this point in the history
- Add `History::delete` for `FileBackedHistory`
- Adapt `history` to print id rather than just consecutive numbers
- Add `history delete <id>` to demo

See also nushell/nushell#11629
  • Loading branch information
Hofer-Julian committed Jan 31, 2024
1 parent 9f0095f commit 9aad030
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
14 changes: 13 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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::<i64>() {
line_editor.history_mut().delete(HistoryItemId::new(id))?;
continue;
}
}
println!("Invalid command. Use: history delete <id>");
continue;
}
// Get this history session identifier
if buffer.trim() == "history sessionid" {
line_editor.print_history_session_id()?;
Expand Down
5 changes: 3 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
21 changes: 14 additions & 7 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9aad030

Please sign in to comment.