File tree 3 files changed +30
-10
lines changed 3 files changed +30
-10
lines changed Original file line number Diff line number Diff line change 16
16
17
17
#[ cfg( not( any( feature = "sqlite" , feature = "sqlite-dynlib" ) ) ) ]
18
18
use reedline:: FileBackedHistory ;
19
- use reedline:: { CursorConfig , MenuBuilder } ;
19
+ use reedline:: { CursorConfig , HistoryItemId , MenuBuilder } ;
20
20
21
21
fn main ( ) -> reedline:: Result < ( ) > {
22
22
println ! ( "Ctrl-D to quit" ) ;
@@ -175,6 +175,18 @@ fn main() -> reedline::Result<()> {
175
175
line_editor. print_history_session ( ) ?;
176
176
continue ;
177
177
}
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
+ }
178
190
// Get this history session identifier
179
191
if buffer. trim ( ) == "history sessionid" {
180
192
line_editor. print_history_session_id ( ) ?;
Original file line number Diff line number Diff line change @@ -536,8 +536,9 @@ impl Reedline {
536
536
. search ( SearchQuery :: everything ( SearchDirection :: Forward , None ) )
537
537
. expect ( "todo: error handling" ) ;
538
538
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) ) ?;
541
542
}
542
543
Ok ( ( ) )
543
544
}
Original file line number Diff line number Diff line change @@ -205,13 +205,20 @@ impl History for FileBackedHistory {
205
205
Ok ( ( ) )
206
206
}
207
207
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 ( ( ) )
215
222
}
216
223
217
224
/// Writes unwritten history contents to disk.
You can’t perform that action at this time.
0 commit comments