Skip to content

Commit

Permalink
fix: select with a specific option set as selected is not shown as se…
Browse files Browse the repository at this point in the history
…lected in the output
  • Loading branch information
roele authored and jdx committed Jan 7, 2025
1 parent 6aadac2 commit 6c36aae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
8 changes: 2 additions & 6 deletions examples/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ fn main() {
let ms = Select::new("Country")
.description("Pick a country")
.filterable(true)
.option(
DemandOption::new("US")
.label("United States")
.selected(true),
)
.option(DemandOption::new("US").label("United States"))
.option(DemandOption::new("DE").label("Germany"))
.option(DemandOption::new("BR").label("Brazil"))
.option(DemandOption::new("BR").label("Brazil").selected(true))
.option(DemandOption::new("CA").label("Canada"))
.option(DemandOption::new("MX").label("Mexico"))
.option(DemandOption::new("FR").label("France"))
Expand Down
23 changes: 17 additions & 6 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use fuzzy_matcher::FuzzyMatcher;
use itertools::Itertools;
use termcolor::{Buffer, WriteColor};

/// Select multiple options from a list
/// Select a single option from a list
///
/// If multiple options are marked as selected, only the last one will be shown as selected.
///
/// # Example
/// ```rust
Expand All @@ -19,7 +21,7 @@ use termcolor::{Buffer, WriteColor};
/// .description("Select your topping")
/// .filterable(true)
/// .option(DemandOption::new("Lettuce"))
/// .option(DemandOption::new("Tomatoes"))
/// .option(DemandOption::new("Tomatoes").selected(true))
/// .option(DemandOption::new("Charm Sauce"))
/// .option(DemandOption::new("Jalapenos").label("Jalapeños"))
/// .option(DemandOption::new("Cheese"))
Expand Down Expand Up @@ -96,6 +98,7 @@ impl<'a, T> Select<'a, T> {
pub fn option(mut self, option: DemandOption<T>) -> Self {
self.options.push(option);
self.pages = self.get_pages();
self.cursor_y = self.get_selected_option_idx();
self
}

Expand All @@ -105,6 +108,7 @@ impl<'a, T> Select<'a, T> {
self.options.push(option);
}
self.pages = self.get_pages();
self.cursor_y = self.get_selected_option_idx();
self
}

Expand Down Expand Up @@ -313,6 +317,13 @@ impl<'a, T> Select<'a, T> {
((self.options.len() as f64) / self.capacity as f64).ceil() as usize
}

fn get_selected_option_idx(&mut self) -> usize {
self.visible_options()
.iter()
.rposition(|o| o.selected)
.unwrap_or(0)
}

fn render(&self) -> io::Result<String> {
let mut out = Buffer::ansi();

Expand Down Expand Up @@ -489,19 +500,19 @@ mod tests {
fn test_render() {
let select = Select::new("Country")
.description("Select your Country")
.option(DemandOption::new("United States").selected(true))
.option(DemandOption::new("United States"))
.option(DemandOption::new("Germany"))
.option(DemandOption::new("Brazil"))
.option(DemandOption::new("Brazil").selected(true))
.option(DemandOption::new("Canada"))
.option(DemandOption::new("Mexico"));

assert_eq!(
indoc! {
"Country
Select your Country
United States
United States
Germany
Brazil
Brazil
Canada
Mexico
↑/↓/k/j up/down • enter confirm
Expand Down

0 comments on commit 6c36aae

Please sign in to comment.