File tree Expand file tree Collapse file tree 3 files changed +30
-10
lines changed Expand file tree Collapse file tree 3 files changed +30
-10
lines changed Original file line number Diff line number Diff line change 1616
1717#[ cfg( not( any( feature = "sqlite" , feature = "sqlite-dynlib" ) ) ) ]
1818use reedline:: FileBackedHistory ;
19- use reedline:: { CursorConfig , MenuBuilder } ;
19+ use reedline:: { CursorConfig , HistoryItemId , MenuBuilder } ;
2020
2121fn 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 ( ) ?;
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments