@@ -195,7 +195,7 @@ impl FuzzySelect<'_> {
195
195
196
196
fn _interact_on ( self , term : & Term , allow_quit : bool ) -> Result < Option < usize > > {
197
197
// Place cursor at the end of the search term
198
- let mut position = self . initial_text . len ( ) ;
198
+ let mut cursor = self . initial_text . chars ( ) . count ( ) ;
199
199
let mut search_term = self . initial_text . to_owned ( ) ;
200
200
201
201
let mut render = TermThemeRenderer :: new ( term, self . theme ) ;
@@ -224,8 +224,15 @@ impl FuzzySelect<'_> {
224
224
let mut vim_mode = false ;
225
225
226
226
loop {
227
+ let mut byte_indices = search_term
228
+ . char_indices ( )
229
+ . map ( |( index, _) | index)
230
+ . collect :: < Vec < _ > > ( ) ;
231
+
232
+ byte_indices. push ( search_term. len ( ) ) ;
233
+
227
234
render. clear ( ) ?;
228
- render. fuzzy_select_prompt ( self . prompt . as_str ( ) , & search_term, position ) ?;
235
+ render. fuzzy_select_prompt ( self . prompt . as_str ( ) , & search_term, byte_indices [ cursor ] ) ?;
229
236
230
237
// Maps all items to a tuple of item and its match score.
231
238
let mut filtered_list = self
@@ -304,14 +311,14 @@ impl FuzzySelect<'_> {
304
311
}
305
312
term. flush ( ) ?;
306
313
}
307
- ( Key :: ArrowLeft , _, _) | ( Key :: Char ( 'h' ) , _, true ) if position > 0 => {
308
- position -= 1 ;
314
+ ( Key :: ArrowLeft , _, _) | ( Key :: Char ( 'h' ) , _, true ) if cursor > 0 => {
315
+ cursor -= 1 ;
309
316
term. flush ( ) ?;
310
317
}
311
318
( Key :: ArrowRight , _, _) | ( Key :: Char ( 'l' ) , _, true )
312
- if position < search_term . len ( ) =>
319
+ if cursor < byte_indices . len ( ) - 1 =>
313
320
{
314
- position += 1 ;
321
+ cursor += 1 ;
315
322
term. flush ( ) ?;
316
323
}
317
324
( Key :: Enter , Some ( sel) , _) if !filtered_list. is_empty ( ) => {
@@ -331,14 +338,14 @@ impl FuzzySelect<'_> {
331
338
term. show_cursor ( ) ?;
332
339
return Ok ( sel_string_pos_in_items) ;
333
340
}
334
- ( Key :: Backspace , _, _) if position > 0 => {
335
- position -= 1 ;
336
- search_term. remove ( position ) ;
341
+ ( Key :: Backspace , _, _) if cursor > 0 => {
342
+ cursor -= 1 ;
343
+ search_term. remove ( byte_indices [ cursor ] ) ;
337
344
term. flush ( ) ?;
338
345
}
339
346
( Key :: Char ( chr) , _, _) if !chr. is_ascii_control ( ) => {
340
- search_term. insert ( position , chr) ;
341
- position += 1 ;
347
+ search_term. insert ( byte_indices [ cursor ] , chr) ;
348
+ cursor += 1 ;
342
349
term. flush ( ) ?;
343
350
sel = Some ( 0 ) ;
344
351
starting_row = 0 ;
0 commit comments