Skip to content

Commit

Permalink
refactor: unify tty color check
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Dec 2, 2024
1 parent df10e5b commit b819661
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 62 deletions.
17 changes: 7 additions & 10 deletions src/subcommands/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::BlockId, providers::Provider};

use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};
use crate::{
block_id::BlockIdParser, utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs,
};

#[derive(Debug, Parser)]
pub struct Block {
Expand All @@ -29,18 +30,14 @@ impl Block {

let provider = self.provider.into_provider()?;

let block_json = if self.receipts {
serde_json::to_value(provider.get_block_with_receipts(self.block_id).await?)?
if self.receipts {
print_colored_json(&provider.get_block_with_receipts(self.block_id).await?)?;
} else if self.full {
serde_json::to_value(provider.get_block_with_txs(self.block_id).await?)?
print_colored_json(&provider.get_block_with_txs(self.block_id).await?)?;
} else {
serde_json::to_value(provider.get_block_with_tx_hashes(self.block_id).await?)?
print_colored_json(&provider.get_block_with_tx_hashes(self.block_id).await?)?;
};

let block_json =
colored_json::to_colored_json(&block_json, ColorMode::Auto(Output::StdOut))?;
println!("{block_json}");

Ok(())
}
}
12 changes: 4 additions & 8 deletions src/subcommands/block_traces.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::BlockId, providers::Provider};

use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};
use crate::{
block_id::BlockIdParser, utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs,
};

#[derive(Debug, Parser)]
pub struct BlockTraces {
Expand All @@ -25,12 +26,7 @@ impl BlockTraces {

let provider = self.provider.into_provider()?;

let traces_json =
serde_json::to_value(provider.trace_block_transactions(self.block_id).await?)?;

let traces_json =
colored_json::to_colored_json(&traces_json, ColorMode::Auto(Output::StdOut))?;
println!("{traces_json}");
print_colored_json(&provider.trace_block_transactions(self.block_id).await?)?;

Ok(())
}
Expand Down
11 changes: 4 additions & 7 deletions src/subcommands/state_update.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::BlockId, providers::Provider};

use crate::{block_id::BlockIdParser, verbosity::VerbosityArgs, ProviderArgs};
use crate::{
block_id::BlockIdParser, utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs,
};

#[derive(Debug, Parser)]
pub struct StateUpdate {
Expand All @@ -25,11 +26,7 @@ impl StateUpdate {

let provider = self.provider.into_provider()?;

let update_json = serde_json::to_value(provider.get_state_update(self.block_id).await?)?;

let update_json =
colored_json::to_colored_json(&update_json, ColorMode::Auto(Output::StdOut))?;
println!("{update_json}");
print_colored_json(&provider.get_state_update(self.block_id).await?)?;

Ok(())
}
Expand Down
8 changes: 2 additions & 6 deletions src/subcommands/syncing.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::SyncStatusType, providers::Provider};

use crate::{verbosity::VerbosityArgs, ProviderArgs};
use crate::{utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct Syncing {
Expand All @@ -23,10 +22,7 @@ impl Syncing {

match sync_status {
SyncStatusType::Syncing(status) => {
let status_json = serde_json::to_value(status)?;
let status_json =
colored_json::to_colored_json(&status_json, ColorMode::Auto(Output::StdOut))?;
println!("{status_json}");
print_colored_json(&status)?;
}
SyncStatusType::NotSyncing => {
println!("Not syncing");
Expand Down
9 changes: 2 additions & 7 deletions src/subcommands/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::Felt, providers::Provider};

use crate::{verbosity::VerbosityArgs, ProviderArgs};
use crate::{utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct Transaction {
Expand All @@ -23,11 +22,7 @@ impl Transaction {
let transaction_hash = Felt::from_hex(&self.hash)?;

let transaction = provider.get_transaction_by_hash(transaction_hash).await?;

let transaction_json = serde_json::to_value(transaction)?;
let transaction_json =
colored_json::to_colored_json(&transaction_json, ColorMode::Auto(Output::StdOut))?;
println!("{transaction_json}");
print_colored_json(&transaction)?;

Ok(())
}
Expand Down
9 changes: 2 additions & 7 deletions src/subcommands/transaction_receipt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::Felt, providers::Provider};

use crate::{verbosity::VerbosityArgs, ProviderArgs};
use crate::{utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct TransactionReceipt {
Expand All @@ -23,11 +22,7 @@ impl TransactionReceipt {
let transaction_hash = Felt::from_hex(&self.hash)?;

let receipt = provider.get_transaction_receipt(transaction_hash).await?;

let receipt_json = serde_json::to_value(receipt)?;
let receipt_json =
colored_json::to_colored_json(&receipt_json, ColorMode::Auto(Output::StdOut))?;
println!("{receipt_json}");
print_colored_json(&receipt)?;

Ok(())
}
Expand Down
10 changes: 2 additions & 8 deletions src/subcommands/transaction_status.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::Felt, providers::Provider};

use crate::{verbosity::VerbosityArgs, ProviderArgs};
use crate::{utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct TransactionStatus {
Expand All @@ -22,12 +21,7 @@ impl TransactionStatus {
let provider = self.provider.into_provider()?;
let transaction_hash = Felt::from_hex(&self.hash)?;

let status = provider.get_transaction_status(transaction_hash).await?;

let status_json = serde_json::to_value(status)?;
let status_json =
colored_json::to_colored_json(&status_json, ColorMode::Auto(Output::StdOut))?;
println!("{status_json}");
print_colored_json(&provider.get_transaction_status(transaction_hash).await?)?;

Ok(())
}
Expand Down
9 changes: 2 additions & 7 deletions src/subcommands/transaction_trace.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::Result;
use clap::Parser;
use colored_json::{ColorMode, Output};
use starknet::{core::types::Felt, providers::Provider};

use crate::{verbosity::VerbosityArgs, ProviderArgs};
use crate::{utils::print_colored_json, verbosity::VerbosityArgs, ProviderArgs};

#[derive(Debug, Parser)]
pub struct TransactionTrace {
Expand All @@ -23,11 +22,7 @@ impl TransactionTrace {
let transaction_hash: Felt = self.hash.parse()?;

let trace = provider.trace_transaction(transaction_hash).await?;

let trace_json = serde_json::to_value(trace)?;
let trace_json =
colored_json::to_colored_json(&trace_json, ColorMode::Auto(Output::StdOut))?;
println!("{trace_json}");
print_colored_json(&trace)?;

Ok(())
}
Expand Down
10 changes: 8 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{io::Read, time::Duration};
use anyhow::Result;
use bigdecimal::{BigDecimal, Zero};
use colored::Colorize;
use colored_json::{ColorMode, ColoredFormatter, Output};
use colored_json::ColoredFormatter;
use flate2::read::GzDecoder;
use num_bigint::{BigInt, Sign};
use num_integer::Integer;
Expand Down Expand Up @@ -116,7 +116,13 @@ where
{
let mut writer = Vec::with_capacity(128);

if ColorMode::Auto(Output::StdOut).use_color() {
#[cfg(not(all(target_arch = "wasm32", target_os = "wasi")))]
let use_color = colored_json::ColorMode::Auto(colored_json::Output::StdOut).use_color();

#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
let use_color = true;

if use_color {
let formatter = ColoredFormatter::new(PrettyFormatter::new());
let mut serializer = serde_json::Serializer::with_formatter(&mut writer, formatter);
value.serialize(&mut serializer)?;
Expand Down

0 comments on commit b819661

Please sign in to comment.