From bf2772e53a31cd43dad98c4840b213a28175dc0d Mon Sep 17 00:00:00 2001 From: FroVolod Date: Wed, 3 Apr 2024 21:01:17 +0300 Subject: [PATCH] refactored --- Cargo.lock | 55 +++++++++++++++++++ Cargo.toml | 3 + src/main.rs | 32 +++++++++++ src/transaction_signature_options/send/mod.rs | 35 ++---------- 4 files changed, 94 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 892e01fe0..f3495496e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,12 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "assert_matches" version = "1.5.0" @@ -1877,6 +1883,7 @@ dependencies = [ "number_prefix", "portable-atomic", "unicode-width", + "vt100", ] [[package]] @@ -2343,6 +2350,9 @@ dependencies = [ "thiserror", "tokio", "toml", + "tracing", + "tracing-indicatif", + "tracing-subscriber", "url", "wasmparser", "zstd", @@ -4541,6 +4551,18 @@ dependencies = [ "tracing", ] +[[package]] +name = "tracing-indicatif" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "069580424efe11d97c3fef4197fa98c004fa26672cc71ad8770d224e23b1951d" +dependencies = [ + "indicatif", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.1.4" @@ -4717,6 +4739,39 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "vt100" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84cd863bf0db7e392ba3bd04994be3473491b31e66340672af5d11943c6274de" +dependencies = [ + "itoa", + "log", + "unicode-width", + "vte", +] + +[[package]] +name = "vte" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5022b5fbf9407086c180e9557be968742d839e68346af7792b8592489732197" +dependencies = [ + "arrayvec", + "utf8parse", + "vte_generate_state_changes", +] + +[[package]] +name = "vte_generate_state_changes" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d257817081c7dffcdbab24b9e62d2def62e2ff7d00b1c20062551e6cccc145ff" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "waker-fn" version = "1.1.1" diff --git a/Cargo.toml b/Cargo.toml index 74b9ec972..eacd0ae0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,9 @@ open = "4" shell-words = "1" cargo-util = "0.1.1" indicatif = "0.17.8" +tracing = "0.1.40" +tracing-indicatif = "0.3.6" +tracing-subscriber = "0.3.18" wasmparser = "0.200.0" diff --git a/src/main.rs b/src/main.rs index c30f1e663..2903c07ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,12 @@ use clap::Parser; use color_eyre::eyre::WrapErr; use interactive_clap::ToCliArgs; +use indicatif::ProgressStyle; +use tracing_indicatif::IndicatifLayer; +use tracing_subscriber::fmt::writer::MakeWriterExt; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; + pub use near_cli_rs::commands; pub use near_cli_rs::common::{self, CliResult}; pub use near_cli_rs::config; @@ -60,6 +66,32 @@ fn main() -> crate::common::CliResult { color_eyre::install()?; + let indicatif_layer = IndicatifLayer::new().with_progress_style( + ProgressStyle::with_template( + "{spinner:.blue} {span_child_prefix} {span_name} {{{span_fields}}}", + ) + .unwrap() + .tick_strings(&[ + "▹▹▹▹▹", + "▸▹▹▹▹", + "▹▸▹▹▹", + "▹▹▸▹▹", + "▹▹▹▸▹", + "▹▹▹▹▸", + "▪▪▪▪▪", + ]), + ); + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer().with_writer( + indicatif_layer + .get_stderr_writer() + .with_max_level(tracing::Level::INFO), + ), + ) + .with(indicatif_layer) + .init(); + #[cfg(feature = "self-update")] let handle = std::thread::spawn(|| -> color_eyre::eyre::Result { crate::commands::extensions::self_update::get_latest_version() diff --git a/src/transaction_signature_options/send/mod.rs b/src/transaction_signature_options/send/mod.rs index e75e7579a..6472d9a5b 100644 --- a/src/transaction_signature_options/send/mod.rs +++ b/src/transaction_signature_options/send/mod.rs @@ -9,6 +9,7 @@ pub struct Send; pub struct SendContext; impl SendContext { + #[tracing::instrument(skip_all, name = "Sending transaction ...")] pub fn from_previous_context( previous_context: super::SubmitContext, _scope: &::InteractiveClapContextScope, @@ -26,35 +27,9 @@ impl SendContext { ) .map_err(color_eyre::Report::msg)?; - // eprintln!("Transaction sent ..."); // long-spinner download iterator let retries_number = 5; - let mut retries_left = (0..retries_number).rev(); + let mut retries = (0..retries_number).rev(); let transaction_info = loop { - let pb = indicatif::ProgressBar::new_spinner(); - pb.enable_steady_tick(std::time::Duration::from_millis(120)); - pb.set_style( - indicatif::ProgressStyle::with_template("{spinner:.blue} {msg}") - .unwrap() - .tick_strings(&[ - "▹▹▹▹▹", - "▸▹▹▹▹", - "▹▸▹▹▹", - "▹▹▸▹▹", - "▹▹▹▸▹", - "▹▹▹▹▸", - "▪▪▪▪▪", - ]), - ); - if retries_left.len() < retries_number { - pb.set_message(format!( - "Sending a transaction ({} retries left) ...", - retries_left.len() + 1 - )); - } else { - pb.set_message("Sending a transaction ..."); - } - std::thread::sleep(std::time::Duration::from_secs(5)); - let transaction_info_result = previous_context.network_config.json_rpc_client() .blocking_call( near_jsonrpc_client::methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest{ @@ -63,16 +38,14 @@ impl SendContext { ); match transaction_info_result { Ok(response) => { - pb.finish_with_message("Your transaction has been sent successfully."); break response; } Err(ref err) => match crate::common::rpc_transaction_error(err) { Ok(_) => { - if retries_left.next().is_some() { - std::thread::sleep(std::time::Duration::from_millis(100)); - } else { + if retries.next().is_none() { return Err(color_eyre::eyre::eyre!(err.to_string())); } + std::thread::sleep(std::time::Duration::from_secs(5)); } Err(report) => return Err(color_eyre::Report::msg(report)), },