Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor spinner #2846

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 crates/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ starknet.workspace = true
url.workspace = true
regex.workspace = true
snapbox.workspace = true
indicatif.workspace = true
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod command;
pub mod consts;
pub mod print;
pub mod rpc;
pub mod spinner;
pub mod test_utils;
pub mod utils;

Expand Down
25 changes: 25 additions & 0 deletions crates/shared/src/spinner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use indicatif::{ProgressBar, ProgressStyle};
use std::borrow::Cow;
use std::time::Duration;

/// Styled spinner that uses [`ProgressBar`].
/// Automatically finishes and clears itself when dropped.
pub struct Spinner(ProgressBar);
impl Spinner {
/// Create [`Spinner`] with a message.
pub fn create_with_message(message: impl Into<Cow<'static, str>>) -> Self {
let spinner = ProgressBar::new_spinner();
let style = ProgressStyle::with_template("\n{spinner} {msg}\n")
.expect("template is static str and should be valid");
spinner.set_style(style);
spinner.enable_steady_tick(Duration::from_millis(100));
spinner.set_message(message);
Self(spinner)
}
}

impl Drop for Spinner {
fn drop(&mut self) {
self.0.finish_and_clear();
}
}
36 changes: 18 additions & 18 deletions crates/universal-sierra-compiler-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::Write;
use std::str::from_utf8;
use tempfile::Builder;

use crate::spinner::spawn_spinner_message;
use crate::spinner::spawn_usc_spinner;
pub use command::*;
use shared::command::CommandExt;

Expand Down Expand Up @@ -72,25 +72,25 @@ pub fn compile_sierra_at_path<T: UniversalSierraCompilerOutput>(
sierra_file_path: &Utf8Path,
sierra_type: &SierraType,
) -> Result<T> {
let spinner = spawn_spinner_message(sierra_file_path)?;

let mut usc_command = UniversalSierraCompilerCommand::new();
let usc_output = usc_command
.inherit_stderr()
.args(vec![
&("compile-".to_string() + &sierra_type.to_string()),
"--sierra-path",
sierra_file_path.as_str(),
])
.command()
.output_checked()
.context(
"Error while compiling Sierra. \
let usc_output = {
let _spinner = spawn_usc_spinner(sierra_file_path)?;

let mut usc_command = UniversalSierraCompilerCommand::new();
usc_command
.inherit_stderr()
.args(vec![
&("compile-".to_string() + &sierra_type.to_string()),
"--sierra-path",
sierra_file_path.as_str(),
])
.command()
.output_checked()
.context(
"Error while compiling Sierra. \
Make sure you have the latest universal-sierra-compiler binary installed. \
Contact us if it doesn't help",
)?;

spinner.finish_and_clear();
)?
};

Ok(T::convert(from_utf8(&usc_output.stdout)?.to_string()))
}
Expand Down
11 changes: 3 additions & 8 deletions crates/universal-sierra-compiler-api/src/spinner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use anyhow::Result;
use camino::Utf8Path;
use indicatif::{ProgressBar, ProgressStyle};
use shared::spinner::Spinner;
use std::env;
use std::time::Duration;

pub fn spawn_spinner_message(sierra_file_path: &Utf8Path) -> Result<ProgressBar> {
let spinner = ProgressBar::new_spinner();
spinner.set_style(ProgressStyle::with_template("\n{spinner} {msg}\n")?);
spinner.enable_steady_tick(Duration::from_millis(100));

pub fn spawn_usc_spinner(sierra_file_path: &Utf8Path) -> Result<Spinner> {
// Skip printing path when compiling unsaved sierra
// which occurs during test execution for some cheatcodes e.g. `replace_bytecode`
let message = if is_temp_file(sierra_file_path)? {
Expand All @@ -19,7 +14,7 @@ pub fn spawn_spinner_message(sierra_file_path: &Utf8Path) -> Result<ProgressBar>
sierra_file_path.canonicalize_utf8()?
)
};
spinner.set_message(message);
let spinner = Spinner::create_with_message(message);

Ok(spinner)
}
Expand Down
Loading