Skip to content

Commit

Permalink
Add basic spinner support
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Oct 17, 2023
1 parent e45cb0c commit 1aff8ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions rust/stackablectl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 28 additions & 12 deletions rust/stackablectl/src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use snafu::{ResultExt, Snafu};
use spinoff::{spinners, Color, Spinner};
use tera::Tera;

mod error;
Expand Down Expand Up @@ -88,11 +89,11 @@ where
}
}

#[derive(Debug)]
pub struct Output<C>
where
C: ContextExt,
{
progress: Option<Spinner>,
renderer: Tera,
context: C,
}
Expand All @@ -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<Tera> {
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<String> {
Expand All @@ -134,6 +137,19 @@ where
.context(RenderSnafu),
}
}

fn create_renderer() -> Result<Tera> {
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<C> Deref for Output<C>
Expand Down

0 comments on commit 1aff8ba

Please sign in to comment.