Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: select with a specific option set as selected is not shown as selected in the output #81

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading