Get example working
This commit is contained in:
@@ -8,3 +8,8 @@ license.workspace = true
|
||||
[dependencies]
|
||||
clap = { version = "4.5.35", features = ["derive"] }
|
||||
harmony = { path = "../harmony" }
|
||||
harmony_tui = { path = "../harmony_tui", optional = true }
|
||||
|
||||
|
||||
[features]
|
||||
tui = ["dep:harmony_tui"]
|
||||
|
||||
88
harmony_cli/src/lib.rs
Normal file
88
harmony_cli/src/lib.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
use clap::Parser;
|
||||
use clap::builder::ArgPredicate;
|
||||
use harmony;
|
||||
use harmony::{score::Score, topology::Topology};
|
||||
|
||||
#[cfg(feature = "tui")]
|
||||
use harmony_tui;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Name of score to run
|
||||
#[cfg(feature = "tui")]
|
||||
#[arg(
|
||||
short,
|
||||
long,
|
||||
default_value_if("interactive", ArgPredicate::IsPresent, Some("unused"))
|
||||
)]
|
||||
run: String,
|
||||
|
||||
/// Name of score to run
|
||||
#[cfg(not(feature = "tui"))]
|
||||
#[arg(short, long)]
|
||||
run: String,
|
||||
|
||||
/// Run interactive or not
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
interactive: bool,
|
||||
|
||||
/// Run all or nth
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
all: bool,
|
||||
|
||||
/// Run nth matching, zero indexed
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
number: u8,
|
||||
}
|
||||
|
||||
pub async fn init<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
|
||||
maestro: harmony::maestro::Maestro<T>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Args::parse();
|
||||
|
||||
#[cfg(feature = "tui")]
|
||||
if args.interactive {
|
||||
return harmony_tui::init(maestro).await;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tui"))]
|
||||
if args.interactive {
|
||||
return Err("Not compiled with interactive support".into());
|
||||
}
|
||||
|
||||
let scores = maestro.scores();
|
||||
let scores_read = scores.read().expect("Should be able to read scores");
|
||||
let scores_read_vec: Vec<Box<dyn Score<T>>> =
|
||||
scores_read.iter().map(|s| s.clone_box()).collect();
|
||||
|
||||
if args.all {
|
||||
for s in scores_read_vec {
|
||||
if s.name().contains(&args.run) {
|
||||
println!("Running: {}", s.clone_box().name());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut count = 0;
|
||||
let mut found = false;
|
||||
for s in scores_read_vec {
|
||||
if s.name().contains(&args.run) {
|
||||
found = true;
|
||||
if count < args.number {
|
||||
// Skip scores that aren't the nth specified
|
||||
count = count + 1;
|
||||
continue;
|
||||
} else {
|
||||
// Run nth score found
|
||||
println!("Running: {}", s.clone_box().name());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
println!("Couldn't find score by that name")
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
use clap::Parser;
|
||||
use harmony;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Name of score to run
|
||||
#[arg(short, long)]
|
||||
run: String,
|
||||
|
||||
/// Run interactive or not
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
interactive: bool,
|
||||
|
||||
/// Run all or first
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
all: bool,
|
||||
|
||||
/// Run nth matching
|
||||
#[arg(short, long, default_value_t = 1)]
|
||||
number: u8,
|
||||
}
|
||||
|
||||
#[harmony::main]
|
||||
pub async fn main(maestro: harmony::maestro::Maestro) {
|
||||
maestro.
|
||||
}
|
||||
Reference in New Issue
Block a user