Skip to content

Commit 015d8c2

Browse files
committed
feat(pretty-fmt): update README for pretty flag
- update README for `--pretty` flag - update CHANGELOG
1 parent 6384de1 commit 015d8c2

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,17 @@ 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+
205+
This is available for wallet, key, repl and compile features. When ommitted, outputs default to `JSON`.
206+
207+
## Minimum Supported Rust Version (MSRV)
208+
209+
This library should always compile with any valid combination of features on Rust **1.75.0**.

src/handlers.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use crate::utils::BlockchainClient::KyotoClient;
1818
use crate::utils::*;
1919
use bdk_wallet::bip39::{Language, Mnemonic};
2020
use bdk_wallet::bitcoin::{
21+
Address, Amount, FeeRate, Network, Psbt, Sequence, Txid,
2122
bip32::{DerivationPath, KeySource},
2223
consensus::encode::serialize_hex,
2324
script::PushBytesBuf,
2425
secp256k1::Secp256k1,
25-
Amount, FeeRate, Network, Psbt, Sequence, Txid,
2626
};
2727
use bdk_wallet::chain::ChainPosition;
2828
use bdk_wallet::descriptor::Segwitv0;
@@ -34,11 +34,11 @@ use bdk_wallet::{
3434
descriptor::{Descriptor, Legacy, Miniscript},
3535
miniscript::policy::Concrete,
3636
};
37-
use cli_table::{format::Justify, Cell, CellStruct, Style, Table};
37+
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
3838

3939
use bdk_wallet::keys::{
40-
bip39::WordCount, DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey,
41-
GeneratableKey, GeneratedKey,
40+
DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey, GeneratableKey, GeneratedKey,
41+
bip39::WordCount,
4242
};
4343
use bdk_wallet::miniscript::miniscript;
4444
use serde_json::json;
@@ -154,27 +154,29 @@ pub fn handle_offline_wallet_subcommand(
154154
};
155155

156156
rows.push(vec![
157-
utxo.outpoint.cell(),
157+
shorten(utxo.outpoint, 8, 10).cell(),
158158
utxo.txout
159159
.value
160160
.to_sat()
161161
.to_string()
162162
.cell()
163163
.justify(Justify::Right),
164-
utxo.txout.script_pubkey.to_hex_string().cell(),
164+
Address::from_script(&utxo.txout.script_pubkey, cli_opts.network)
165+
.unwrap()
166+
.cell(),
165167
utxo.keychain.cell(),
166168
utxo.is_spent.cell(),
167169
utxo.derivation_index.cell(),
168170
height.to_string().cell().justify(Justify::Right),
169-
block_hash.cell().justify(Justify::Right),
171+
shorten(block_hash, 8, 8).cell().justify(Justify::Right),
170172
]);
171173
}
172174
let table = rows
173175
.table()
174176
.title(vec![
175177
"Outpoint".cell().bold(true),
176178
"Output (sat)".cell().bold(true),
177-
"Output ScriptPubkey".cell().bold(true),
179+
"Output Address".cell().bold(true),
178180
"Keychain".cell().bold(true),
179181
"Is Spent".cell().bold(true),
180182
"Index".cell().bold(true),
@@ -1122,7 +1124,12 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11221124
#[cfg(not(any(feature = "sqlite")))]
11231125
let result = {
11241126
let mut wallet = new_wallet(network, &wallet_opts)?;
1125-
handle_offline_wallet_subcommand(&mut wallet, &wallet_opts, offline_subcommand)?
1127+
handle_offline_wallet_subcommand(
1128+
&mut wallet,
1129+
&wallet_opts,
1130+
&cli_opts,
1131+
offline_subcommand.clone(),
1132+
)?
11261133
};
11271134
Ok(result)
11281135
}
@@ -1140,7 +1147,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11401147
let result = handle_compile_subcommand(network, policy, script_type, pretty)?;
11411148
Ok(result)
11421149
}
1143-
// #[cfg(feature = "repl")]
1150+
#[cfg(feature = "repl")]
11441151
CliSubCommand::Repl { ref wallet_opts } => {
11451152
let network = cli_opts.network;
11461153
#[cfg(feature = "sqlite")]

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)