]> Untitled Git - bdk-cli/commitdiff
feat(pretty-fmt): update README for `pretty` flag
authorVihiga Tyonum <withtvpeter@gmail.com>
Thu, 21 Aug 2025 09:19:18 +0000 (10:19 +0100)
committerVihiga Tyonum <withtvpeter@gmail.com>
Tue, 26 Aug 2025 17:53:02 +0000 (18:53 +0100)
- update README for `--pretty` flag
- update CHANGELOG

CHANGELOG.md
README.md
src/handlers.rs
src/utils.rs

index 6c28d4e8185d4b33113483bba0253e4d00edf4f8..cbc6be0fe676cd7617fd951726f529d32c2e2813 100644 (file)
@@ -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]
 
index c95d61e00bdb413674d76373fd02c0656d6e546e..bca4e005aa757265d13da1db20b953019eb75d72 100644 (file)
--- 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`.
index 0cab14b865183e24ff65d19336773bfa8e694b5a..a023d3f7a15a03183d62efd0edac9632b7e5ac74 100644 (file)
@@ -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<String, Error> {
             #[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<String, Error> {
             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"))]
index de05c665ddd2505435f98387710c526de03e19b9..c82b7d8b071a7700b7e05a89f0af3f81576f7a67 100644 (file)
@@ -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<LightClient>) ->
 
     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}")
+}