From 697c669d05735ed9786f410b473f6e35467c9dc3 Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Tue, 4 Feb 2025 15:43:05 -0500 Subject: [PATCH] feat(examples/tui): add TUI example with harmony integration - Create `Cargo.toml` for the new TUI example under `examples/tui` - Implement basic `main.rs` that initializes Maestro and launches the TUI - Update `harmony_tui/src/lib.rs` to handle async score execution gracefully --- harmony-rs/examples/tui/Cargo.toml | 18 ++++++++++++++++++ harmony-rs/examples/tui/src/main.rs | 19 +++++++++++++++++++ harmony-rs/harmony_tui/src/lib.rs | 15 +++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 harmony-rs/examples/tui/Cargo.toml create mode 100644 harmony-rs/examples/tui/src/main.rs diff --git a/harmony-rs/examples/tui/Cargo.toml b/harmony-rs/examples/tui/Cargo.toml new file mode 100644 index 0000000..d9390ca --- /dev/null +++ b/harmony-rs/examples/tui/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "example-tui" +edition = "2024" +version.workspace = true +readme.workspace = true +license.workspace = true +publish = false + +[dependencies] +harmony = { path = "../../harmony" } +harmony_tui = { path = "../../harmony_tui" } +harmony_types = { path = "../../harmony_types" } +cidr = { workspace = true } +tokio = { workspace = true } +harmony_macros = { path = "../../harmony_macros" } +log = { workspace = true } +env_logger = { workspace = true } +url = { workspace = true } diff --git a/harmony-rs/examples/tui/src/main.rs b/harmony-rs/examples/tui/src/main.rs new file mode 100644 index 0000000..660d593 --- /dev/null +++ b/harmony-rs/examples/tui/src/main.rs @@ -0,0 +1,19 @@ +use harmony::{ + inventory::Inventory, + maestro::Maestro, + modules::dummy::{ErrorScore, PanicScore, SuccessScore}, + topology::HAClusterTopology, +}; + +#[tokio::main] +async fn main() { + let inventory = Inventory::empty_inventory(); + let topology = HAClusterTopology::dummy(); + let mut maestro = Maestro::new(inventory, topology); + maestro.register_all(vec![ + Box::new(SuccessScore {}), + Box::new(ErrorScore {}), + Box::new(PanicScore {}), + ]); + harmony_tui::init(maestro).await.unwrap(); +} diff --git a/harmony-rs/harmony_tui/src/lib.rs b/harmony-rs/harmony_tui/src/lib.rs index 96b6e99..64dda5d 100644 --- a/harmony-rs/harmony_tui/src/lib.rs +++ b/harmony-rs/harmony_tui/src/lib.rs @@ -61,7 +61,7 @@ enum HarmonyTuiEvent { impl HarmonyTUI { pub fn new(maestro: Maestro) -> Self { let maestro = Arc::new(maestro); - let (handle, sender) = Self::start_channel(maestro.clone()); + let (_handle, sender) = Self::start_channel(maestro.clone()); let score = ScoreListWidget::new(Self::scores_list(&maestro), sender); HarmonyTUI { @@ -83,14 +83,17 @@ impl HarmonyTUI { HarmonyTuiEvent::LaunchScore(score_item) => { let maestro = maestro.clone(); - let interpretation_result = + let joinhandle_result = tokio::spawn(async move { maestro.interpret(score_item.0).await }) .await; - match interpretation_result { - Ok(success) => info!("Score execution successful {success:?}"), - Err(e) => { - error!("Score execution failed {:#?}", e); + match joinhandle_result { + Ok(interpretation_result) => match interpretation_result { + Ok(success) => info!("Score execution successful {success:?}"), + Err(error) => error!("Score failed {error:?}"), + }, + Err(joinhandle_failure) => { + error!("Score execution TASK FAILED! {:#?}", joinhandle_failure); } } }