diff --git a/Cargo.lock b/Cargo.lock index 614d291e..969a60d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -454,6 +454,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "comfy-table" version = "7.0.1" @@ -1751,6 +1762,12 @@ dependencies = [ "regex", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "pem" version = "1.1.1" @@ -2589,6 +2606,17 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "spinoff" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20aa2ed67fbb202e7b716ff8bfc6571dd9301617767380197d701c31124e88f6" +dependencies = [ + "colored", + "once_cell", + "paste", +] + [[package]] name = "stackable-cockpit" version = "0.0.0-dev" @@ -2708,6 +2736,7 @@ dependencies = [ "serde_json", "serde_yaml", "snafu", + "spinoff", "stackable-cockpit", "tera", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 2e9f8069..02cc4633 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ serde_json = "1.0" serde_yaml = "0.9" sha2 = "0.10" snafu = "0.7" +spinoff = "0.8.0" stackable-operator = { git = "https://github.com/stackabletech/operator-rs.git", tag = "0.47.0" } tera = "1.18" tokio = { version = "1.29.0", features = ["rt-multi-thread", "macros", "fs", "process"] } diff --git a/rust/stackablectl/Cargo.toml b/rust/stackablectl/Cargo.toml index 90afa366..0612223c 100644 --- a/rust/stackablectl/Cargo.toml +++ b/rust/stackablectl/Cargo.toml @@ -27,6 +27,7 @@ serde_json.workspace = true serde_yaml.workspace = true serde.workspace = true snafu.workspace = true +spinoff.workspace = true tera.workspace = true tokio.workspace = true tracing-subscriber.workspace = true diff --git a/rust/stackablectl/src/output/mod.rs b/rust/stackablectl/src/output/mod.rs index a0325601..a5b77f76 100644 --- a/rust/stackablectl/src/output/mod.rs +++ b/rust/stackablectl/src/output/mod.rs @@ -4,6 +4,7 @@ use std::{ }; use snafu::{ResultExt, Snafu}; +use spinoff::{spinners, Color, Spinner}; use tera::Tera; mod error; @@ -88,11 +89,11 @@ where } } -#[derive(Debug)] pub struct Output where C: ContextExt, { + progress: Option, renderer: Tera, context: C, } @@ -106,20 +107,22 @@ where let no_color = use_colored_output(!no_color); context.set_no_color(no_color); - Ok(Self { renderer, context }) + Ok(Self { + progress: None, + renderer, + context, + }) } - fn create_renderer() -> Result { - let mut renderer = Tera::default(); - - renderer - .add_raw_templates(vec![ - ("result", include_str!("templates/result.tpl")), - ("error", include_str!("templates/error.tpl")), - ]) - .context(CreationSnafu)?; + pub fn enable_progress(&mut self, initial_message: String) { + self.progress + .get_or_insert(Spinner::new(spinners::Dots, initial_message, Color::Green)); + } - Ok(renderer) + pub fn set_progress_message(&mut self, message: String) { + if let Some(progress) = self.progress.as_mut() { + progress.update_text(message) + } } pub fn render(self) -> Result { @@ -134,6 +137,19 @@ where .context(RenderSnafu), } } + + fn create_renderer() -> Result { + let mut renderer = Tera::default(); + + renderer + .add_raw_templates(vec![ + ("result", include_str!("templates/result.tpl")), + ("error", include_str!("templates/error.tpl")), + ]) + .context(CreationSnafu)?; + + Ok(renderer) + } } impl Deref for Output