mikaelmello / inquire

A Rust library for building interactive prompts
https://docs.rs/inquire
MIT License
1.97k stars 76 forks source link

with_starting_cursor does not apply #195

Closed Cyrix126 closed 9 months ago

Cyrix126 commented 10 months ago

Describe the bug When using the method with_starting_cursor on a Select prompt, it doesn't start with the cursor on the custom index but goes on the default one (the first).

To Reproduce Steps to reproduce the behavior:

  1. modify the example select.rs to add with_starting_cursor like this:
    
    use inquire::Select;

fn main() { let options = vec![ "Banana", "Apple", "Strawberry", "Grapes", "Lemon", "Tangerine", "Watermelon", "Orange", "Pear", "Avocado", "Pineapple", ];

let ans = Select::new("What's your favorite fruit?", options)
    .with_starting_cursor(2)
    .prompt();

match ans {
    Ok(choice) => println!("{choice}! That's mine too!"),
    Err(_) => println!("There was an error, please try again"),
}

}

2. execute

cargo run --example select


3. See that the cursor is on "Banana", instead of "Strawberry".

**Expected behavior**
With index set on 2 in the example, the cursor should start on "Strawberry".

**Additional context**
on main branch last commit as of now 1c7ece7364978817e8069250316437ab3b12484d
bogdiw commented 9 months ago

The DEFAULT_RESET_CURSOR was set by default to true to prevent any "behaviour" and when the function run_scorer is called it verifies the reset_cursor flag and that's why it puts it on default 0 every time.

fn run_scorer(&mut self) {
        let mut options = self.score_options();
        options.sort_unstable_by_key(|(_idx, score)| Reverse(*score));
        self.scored_options = options.into_iter().map(|(idx, _)| idx).collect();
        if self.config.reset_cursor {
            let _ = self.update_cursor_position(0);
        } else if self.scored_options.len() <= self.cursor_index {
            let _ = self.update_cursor_position(self.scored_options.len().saturating_sub(1));
        }
    }
mikaelmello commented 9 months ago

Thanks for the report! I'm pushing a fix and also adding a test so this regression doesn't happen again