Skip to content

Commit

Permalink
Refactor spinner (#2846)
Browse files Browse the repository at this point in the history
**Stack**:
- #2847
- #2846⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
ksew1 authored Jan 13, 2025
1 parent 7ebb054 commit ec1fee9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
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

0 comments on commit ec1fee9

Please sign in to comment.