Skip to content

Commit

Permalink
feat: add get_spinner macro for spinner initialization
Browse files Browse the repository at this point in the history
- Introduced `get_spinner` macro in `src/macros.rs` to streamline spinner creation.
- Updated `run` function in `src/core.rs` to use the new `get_spinner` macro.
- Modified `main` function in `src/main.rs` to utilize the `get_spinner` macro.
- Renamed test function `basic` to `test_basic_flow` in `tests/basic.rs` for better clarity.
  • Loading branch information
thelezend committed Sep 25, 2024
1 parent 998a4c8 commit 93b7d32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
8 changes: 2 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use solana_client::{nonblocking::rpc_client::RpcClient, rpc_config::RpcTransacti
use solana_sdk::{commitment_config::CommitmentConfig, signature::Signature};
use solana_transaction_status::UiTransactionEncoding;

use crate::{PrevBuy, RepeatingWallet};
use crate::{get_spinner, PrevBuy, RepeatingWallet};

/// Runs the main logic of the solana-copy-trade-detect application.
///
Expand All @@ -37,15 +37,11 @@ use crate::{PrevBuy, RepeatingWallet};
pub async fn run(args: &crate::Args) -> Result<Vec<RepeatingWallet>, crate::Error> {
let mut prev_wallets = HashMap::new();

let spinner = ProgressBar::new_spinner();
spinner.set_style(ProgressStyle::with_template("{spinner:.green} {msg}").unwrap());
spinner.enable_steady_tick(Duration::from_millis(120));
spinner.set_message(format!(
let spinner = get_spinner!(format!(
"{} {}Fetching fresh swaps...",
console::style("[1/3]").bold().dim(),
crate::LIGHTNING,
));

let fresh_swaps = fetch_fresh_swaps(args).await?;
spinner.finish();

Expand Down
25 changes: 25 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ macro_rules! print_if_terminal {
}
};
}

/// Creates and configures a new spinner with a custom message.
///
/// This macro initializes a new spinner using the `indicatif` crate, sets its style,
/// enables a steady tick, and assigns a custom message to it.
///
/// # Arguments
///
/// * `$msg` - The message to display with the spinner.
///
/// # Example
///
/// ```
/// let spinner = get_spinner!("Loading...");
/// ```
#[macro_export]
macro_rules! get_spinner {
($msg:expr) => {{
let spinner = ProgressBar::new_spinner();
spinner.set_style(ProgressStyle::with_template("{spinner:.green} {msg}").unwrap());
spinner.enable_steady_tick(Duration::from_millis(120));
spinner.set_message($msg);
spinner
}};
}
11 changes: 3 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ use std::{

use clap::Parser;
use indicatif::{ProgressBar, ProgressStyle};
use solana_copy_trade_detect::{Args, RepeatingWallet};
use solana_copy_trade_detect::{get_spinner, Args, RepeatingWallet};

#[tokio::main]
async fn main() {
dotenvy::dotenv().ok();

// dotenvy::from_filename(".env.test").ok();

let args = Args::parse();
let result = solana_copy_trade_detect::run(&args).await;

Expand All @@ -25,10 +23,7 @@ async fn main() {
.output_file
.unwrap_or(PathBuf::from(format!("{}.txt", args.wallet)));

let spinner = ProgressBar::new_spinner();
spinner.set_style(ProgressStyle::with_template("{spinner:.green} {msg}").unwrap());
spinner.enable_steady_tick(Duration::from_millis(120));
spinner.set_message(format!(
let spinner = get_spinner!(format!(
"{} {}Writing output to {}",
console::style("[3/3]").bold().dim(),
solana_copy_trade_detect::FILE,
Expand All @@ -38,7 +33,7 @@ async fn main() {
write_to_file(repeating_wallets, &file_path).expect("Failed to write to file");
spinner.finish();

println!("{}Done!", solana_copy_trade_detect::CHECK);
println!("\t\t{}Done!", solana_copy_trade_detect::CHECK);
} else {
println!("{}", serde_json::to_string(&repeating_wallets).unwrap());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use assert_cmd::Command;
mod common;

#[test]
fn basic() -> Result<(), Box<dyn std::error::Error>> {
fn test_basic_flow() -> Result<(), Box<dyn std::error::Error>> {
common::setup();

let mut cmd = Command::cargo_bin("solana-copy-trade-detect")?;
Expand All @@ -17,10 +17,10 @@ fn basic() -> Result<(), Box<dyn std::error::Error>> {
]);

let output = cmd.assert().success().get_output().clone();
println!("{:#?}", output);

// Check if the output is valid JSON
let output = String::from_utf8(output.stdout)?;
serde_json::from_str::<serde_json::Value>(&output).expect("Failed to parse json");
println!("{}", output);
Ok(())
}

0 comments on commit 93b7d32

Please sign in to comment.