From: Vihiga Tyonum Date: Thu, 21 Aug 2025 09:19:18 +0000 (+0100) Subject: feat(pretty-fmt): update README for `pretty` flag X-Git-Tag: v2.0.0~4^2 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.CommandStringError.html?a=commitdiff_plain;h=94012c65bd130fc6c5330ff368219b3bef81fddd;p=bdk-cli feat(pretty-fmt): update README for `pretty` flag - update README for `--pretty` flag - update CHANGELOG --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c28d4e..cbc6be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details. ## [Unreleased] - Removed MSRV and bumped Rust Edition to 2024 +- Add `--pretty` top level flag for formatting commands output in a tabular format ## [1.0.0] diff --git a/README.md b/README.md index c95d61e..bca4e00 100644 --- a/README.md +++ b/README.md @@ -193,3 +193,12 @@ Note: You can modify the `Justfile` to reflect your nodes' configuration values. cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password sync cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password balance ``` + +## Formatting Responses using `--pretty` flag + +You can optionally return outputs of commands in human-readable, tabular format instead of `JSON`. To enable this option, simply add the `--pretty` flag as a top level flag. For instance, you wallet's balance in a pretty format, you can run: + +```shell +cargo run --pretty -n signet wallet -w {wallet_name} -d sqlite balance +``` +This is available for wallet, key, repl and compile features. When ommitted, outputs default to `JSON`. diff --git a/src/handlers.rs b/src/handlers.rs index 0cab14b..a023d3f 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -22,11 +22,11 @@ use crate::utils::*; use bdk_redb::Store as RedbStore; use bdk_wallet::bip39::{Language, Mnemonic}; use bdk_wallet::bitcoin::{ + Address, Amount, FeeRate, Network, Psbt, Sequence, Txid, bip32::{DerivationPath, KeySource}, consensus::encode::serialize_hex, script::PushBytesBuf, secp256k1::Secp256k1, - Amount, FeeRate, Network, Psbt, Sequence, Txid, }; use bdk_wallet::chain::ChainPosition; use bdk_wallet::descriptor::Segwitv0; @@ -38,11 +38,11 @@ use bdk_wallet::{ descriptor::{Descriptor, Legacy, Miniscript}, miniscript::policy::Concrete, }; -use cli_table::{format::Justify, Cell, CellStruct, Style, Table}; +use cli_table::{Cell, CellStruct, Style, Table, format::Justify}; use bdk_wallet::keys::{ - bip39::WordCount, DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey, - GeneratableKey, GeneratedKey, + DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey, GeneratableKey, GeneratedKey, + bip39::WordCount, }; use bdk_wallet::miniscript::miniscript; use serde_json::json; @@ -160,19 +160,21 @@ pub fn handle_offline_wallet_subcommand( }; rows.push(vec![ - utxo.outpoint.cell(), + shorten(utxo.outpoint, 8, 10).cell(), utxo.txout .value .to_sat() .to_string() .cell() .justify(Justify::Right), - utxo.txout.script_pubkey.to_hex_string().cell(), + Address::from_script(&utxo.txout.script_pubkey, cli_opts.network) + .unwrap() + .cell(), utxo.keychain.cell(), utxo.is_spent.cell(), utxo.derivation_index.cell(), height.to_string().cell().justify(Justify::Right), - block_hash.cell().justify(Justify::Right), + shorten(block_hash, 8, 8).cell().justify(Justify::Right), ]); } let table = rows @@ -180,7 +182,7 @@ pub fn handle_offline_wallet_subcommand( .title(vec![ "Outpoint".cell().bold(true), "Output (sat)".cell().bold(true), - "Output ScriptPubkey".cell().bold(true), + "Output Address".cell().bold(true), "Keychain".cell().bold(true), "Is Spent".cell().bold(true), "Index".cell().bold(true), @@ -1154,7 +1156,12 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { #[cfg(not(any(feature = "sqlite", feature = "redb")))] let result = { let mut wallet = new_wallet(network, &wallet_opts)?; - handle_offline_wallet_subcommand(&mut wallet, &wallet_opts, &cli_opts, offline_subcommand)? + handle_offline_wallet_subcommand( + &mut wallet, + &wallet_opts, + &cli_opts, + offline_subcommand.clone(), + )? }; Ok(result) } @@ -1172,7 +1179,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { let result = handle_compile_subcommand(network, policy, script_type, pretty)?; Ok(result) } - // #[cfg(feature = "repl")] + #[cfg(feature = "repl")] CliSubCommand::Repl { ref wallet_opts } => { let network = cli_opts.network; #[cfg(any(feature = "sqlite", feature = "redb"))] diff --git a/src/utils.rs b/src/utils.rs index de05c66..c82b7d8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -10,6 +10,7 @@ //! //! This module includes all the utility tools used by the App. use crate::error::BDKCliError as Error; +use std::fmt::Display; use std::str::FromStr; use std::path::{Path, PathBuf}; @@ -371,3 +372,10 @@ pub async fn sync_kyoto_client(wallet: &mut Wallet, client: Box) -> Ok(()) } + +pub(crate) fn shorten(displayable: impl Display, start: u8, end: u8) -> String { + let displayable = displayable.to_string(); + let start_str: &str = &displayable[0..start as usize]; + let end_str: &str = &displayable[displayable.len() - end as usize..]; + format!("{start_str}...{end_str}") +}