Get example working

This commit is contained in:
tahahawa
2025-04-11 15:19:48 -04:00
committed by taha
parent 20a8f12160
commit fbea2a40c3
6 changed files with 147 additions and 27 deletions

View File

@@ -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
View 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(());
}

View File

@@ -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.
}