Skip to content

Commit 94012c6

Browse files
committed
feat(pretty-fmt): update README for pretty flag
- update README for `--pretty` flag - update CHANGELOG
1 parent 1cadf63 commit 94012c6

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
66
## [Unreleased]
77

88
- Removed MSRV and bumped Rust Edition to 2024
9+
- Add `--pretty` top level flag for formatting commands output in a tabular format
910

1011
## [1.0.0]
1112

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,12 @@ Note: You can modify the `Justfile` to reflect your nodes' configuration values.
193193
cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password sync
194194
cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password balance
195195
```
196+
197+
## Formatting Responses using `--pretty` flag
198+
199+
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:
200+
201+
```shell
202+
cargo run --pretty -n signet wallet -w {wallet_name} -d sqlite balance
203+
```
204+
This is available for wallet, key, repl and compile features. When ommitted, outputs default to `JSON`.

src/handlers.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use crate::utils::*;
2222
use bdk_redb::Store as RedbStore;
2323
use bdk_wallet::bip39::{Language, Mnemonic};
2424
use bdk_wallet::bitcoin::{
25+
Address, Amount, FeeRate, Network, Psbt, Sequence, Txid,
2526
bip32::{DerivationPath, KeySource},
2627
consensus::encode::serialize_hex,
2728
script::PushBytesBuf,
2829
secp256k1::Secp256k1,
29-
Amount, FeeRate, Network, Psbt, Sequence, Txid,
3030
};
3131
use bdk_wallet::chain::ChainPosition;
3232
use bdk_wallet::descriptor::Segwitv0;
@@ -38,11 +38,11 @@ use bdk_wallet::{
3838
descriptor::{Descriptor, Legacy, Miniscript},
3939
miniscript::policy::Concrete,
4040
};
41-
use cli_table::{format::Justify, Cell, CellStruct, Style, Table};
41+
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
4242

4343
use bdk_wallet::keys::{
44-
bip39::WordCount, DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey,
45-
GeneratableKey, GeneratedKey,
44+
DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey, GeneratableKey, GeneratedKey,
45+
bip39::WordCount,
4646
};
4747
use bdk_wallet::miniscript::miniscript;
4848
use serde_json::json;
@@ -160,27 +160,29 @@ pub fn handle_offline_wallet_subcommand(
160160
};
161161

162162
rows.push(vec![
163-
utxo.outpoint.cell(),
163+
shorten(utxo.outpoint, 8, 10).cell(),
164164
utxo.txout
165165
.value
166166
.to_sat()
167167
.to_string()
168168
.cell()
169169
.justify(Justify::Right),
170-
utxo.txout.script_pubkey.to_hex_string().cell(),
170+
Address::from_script(&utxo.txout.script_pubkey, cli_opts.network)
171+
.unwrap()
172+
.cell(),
171173
utxo.keychain.cell(),
172174
utxo.is_spent.cell(),
173175
utxo.derivation_index.cell(),
174176
height.to_string().cell().justify(Justify::Right),
175-
block_hash.cell().justify(Justify::Right),
177+
shorten(block_hash, 8, 8).cell().justify(Justify::Right),
176178
]);
177179
}
178180
let table = rows
179181
.table()
180182
.title(vec![
181183
"Outpoint".cell().bold(true),
182184
"Output (sat)".cell().bold(true),
183-
"Output ScriptPubkey".cell().bold(true),
185+
"Output Address".cell().bold(true),
184186
"Keychain".cell().bold(true),
185187
"Is Spent".cell().bold(true),
186188
"Index".cell().bold(true),
@@ -1154,7 +1156,12 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11541156
#[cfg(not(any(feature = "sqlite", feature = "redb")))]
11551157
let result = {
11561158
let mut wallet = new_wallet(network, &wallet_opts)?;
1157-
handle_offline_wallet_subcommand(&mut wallet, &wallet_opts, &cli_opts, offline_subcommand)?
1159+
handle_offline_wallet_subcommand(
1160+
&mut wallet,
1161+
&wallet_opts,
1162+
&cli_opts,
1163+
offline_subcommand.clone(),
1164+
)?
11581165
};
11591166
Ok(result)
11601167
}
@@ -1172,7 +1179,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11721179
let result = handle_compile_subcommand(network, policy, script_type, pretty)?;
11731180
Ok(result)
11741181
}
1175-
// #[cfg(feature = "repl")]
1182+
#[cfg(feature = "repl")]
11761183
CliSubCommand::Repl { ref wallet_opts } => {
11771184
let network = cli_opts.network;
11781185
#[cfg(any(feature = "sqlite", feature = "redb"))]

src/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//!
1111
//! This module includes all the utility tools used by the App.
1212
use crate::error::BDKCliError as Error;
13+
use std::fmt::Display;
1314
use std::str::FromStr;
1415

1516
use std::path::{Path, PathBuf};
@@ -371,3 +372,10 @@ pub async fn sync_kyoto_client(wallet: &mut Wallet, client: Box<LightClient>) ->
371372

372373
Ok(())
373374
}
375+
376+
pub(crate) fn shorten(displayable: impl Display, start: u8, end: u8) -> String {
377+
let displayable = displayable.to_string();
378+
let start_str: &str = &displayable[0..start as usize];
379+
let end_str: &str = &displayable[displayable.len() - end as usize..];
380+
format!("{start_str}...{end_str}")
381+
}

0 commit comments

Comments
 (0)