add index to display

This commit is contained in:
Taha Hawa 2025-04-17 18:22:39 -04:00
parent f75d03c570
commit 92e60b00d2

View File

@ -28,7 +28,8 @@ pub struct Args {
long,
default_value_t = true,
default_value_if("number", ArgPredicate::IsPresent, "false"),
conflicts_with = "number"
conflicts_with = "number",
conflicts_with = "list"
)]
all: bool,
@ -69,6 +70,17 @@ fn maestro_scores_filter<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
return scores_vec;
}
fn list_scores_with_index<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
scores_vec: &Vec<Box<dyn Score<T>>>,
) -> String {
let mut display_str = String::new();
for (i, s) in scores_vec.iter().enumerate() {
let name = s.name();
display_str.push_str(&format!("\n{i}: {name}"));
}
return display_str;
}
pub async fn init<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
maestro: harmony::maestro::Maestro<T>,
args_struct: Option<Args>,
@ -94,25 +106,31 @@ pub async fn init<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
return Err("No score found".into());
}
// if list option is specified, print filtered list and exit
if args.list {
println!("Available scores:");
println!("{}", list_scores_with_index(&scores_vec));
return Ok(());
}
// if no score to run provided, and list isn't specified, print help
if !args.yes {
let confirmation =
Confirm::new(format!("This will run the following scores: {:#?}", scores_vec).as_str())
.with_default(true)
.prompt()
.expect("Unexpected prompt error");
let confirmation = Confirm::new(
format!(
"This will run the following scores: {}\n",
list_scores_with_index(&scores_vec)
)
.as_str(),
)
.with_default(true)
.prompt()
.expect("Unexpected prompt error");
if !confirmation {
return Ok(());
}
}
// if list option is specified, print filtered list and exit
if args.list {
println!("Available scores: {:#?}", scores_vec);
return Ok(());
}
// if all is specified, run all. Otherwise, run the first match or specified index
for s in scores_vec {
println!("Running: {}", s.name());