Skip to content

Commit 9aad030

Browse files
committed
Add History::delete for FileBackedHistory
- 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
1 parent 9f0095f commit 9aad030

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

examples/demo.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use {
1616

1717
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
1818
use reedline::FileBackedHistory;
19-
use reedline::{CursorConfig, MenuBuilder};
19+
use reedline::{CursorConfig, HistoryItemId, MenuBuilder};
2020

2121
fn main() -> reedline::Result<()> {
2222
println!("Ctrl-D to quit");
@@ -175,6 +175,18 @@ fn main() -> reedline::Result<()> {
175175
line_editor.print_history_session()?;
176176
continue;
177177
}
178+
// Delete history entry of a certain id
179+
if buffer.trim().starts_with("history delete-item") {
180+
let parts: Vec<&str> = buffer.split_whitespace().collect();
181+
if parts.len() == 3 {
182+
if let Ok(id) = parts[2].parse::<i64>() {
183+
line_editor.history_mut().delete(HistoryItemId::new(id))?;
184+
continue;
185+
}
186+
}
187+
println!("Invalid command. Use: history delete <id>");
188+
continue;
189+
}
178190
// Get this history session identifier
179191
if buffer.trim() == "history sessionid" {
180192
line_editor.print_history_session_id()?;

src/engine.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ impl Reedline {
536536
.search(SearchQuery::everything(SearchDirection::Forward, None))
537537
.expect("todo: error handling");
538538

539-
for (i, entry) in history.iter().enumerate() {
540-
self.print_line(&format!("{}\t{}", i, entry.command_line))?;
539+
for entry in history.iter() {
540+
let Some(id) = entry.id else { continue };
541+
self.print_line(&format!("{}\t{}", id, entry.command_line))?;
541542
}
542543
Ok(())
543544
}

src/history/file_backed.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,20 @@ impl History for FileBackedHistory {
205205
Ok(())
206206
}
207207

208-
fn delete(&mut self, _h: super::HistoryItemId) -> Result<()> {
209-
Err(ReedlineError(
210-
ReedlineErrorVariants::HistoryFeatureUnsupported {
211-
history: "FileBackedHistory",
212-
feature: "removing entries",
213-
},
214-
))
208+
fn delete(&mut self, h: super::HistoryItemId) -> Result<()> {
209+
let id = h.0 as usize;
210+
let num_entries = self.entries.len();
211+
// Check if the id is valid
212+
if id >= num_entries {
213+
return Err(ReedlineError(ReedlineErrorVariants::OtherHistoryError(
214+
"Given id is out of range.",
215+
)));
216+
}
217+
218+
// Remove the item with the specified id
219+
self.entries.remove(id);
220+
221+
Ok(())
215222
}
216223

217224
/// Writes unwritten history contents to disk.

0 commit comments

Comments
 (0)