Skip to content

feat(pretty-format): add pretty flag to format outputs in a tabular form#212

Merged
tvpeter merged 2 commits intobitcoindevkit:masterfrom
tvpeter:feat/pretty-format
Aug 26, 2025
Merged

feat(pretty-format): add pretty flag to format outputs in a tabular form#212
tvpeter merged 2 commits intobitcoindevkit:masterfrom
tvpeter:feat/pretty-format

Conversation

@tvpeter
Copy link
Collaborator

@tvpeter tvpeter commented Aug 18, 2025

Fixes #193

Description

This PR adds a top level flag --pretty to format output in a tabular form to enhance readability for users.

Notes to the reviewers

Changelog notice

  • Add --pretty top level flag for formatting commands output in a tabular format

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature
  • I've updated CHANGELOG.md

@coveralls
Copy link

coveralls commented Aug 18, 2025

Pull Request Test Coverage Report for Build 17246288729

Details

  • 0 of 362 (0.0%) changed or added relevant lines in 2 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.7%) to 2.159%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/utils.rs 0 6 0.0%
src/handlers.rs 0 356 0.0%
Files with Coverage Reduction New Missed Lines %
src/handlers.rs 5 2.72%
Totals Coverage Status
Change from base Build 17231813091: -0.7%
Covered Lines: 25
Relevant Lines: 1158

💛 - Coveralls

@tvpeter tvpeter force-pushed the feat/pretty-format branch 2 times, most recently from 2ba0b94 to 3a5204f Compare August 20, 2025 17:32
@tvpeter tvpeter marked this pull request as ready for review August 21, 2025 09:24
@tvpeter tvpeter requested a review from notmandatory August 21, 2025 09:25
@tvpeter tvpeter added this to the CLI 2.0.0 milestone Aug 25, 2025
@tvpeter tvpeter force-pushed the feat/pretty-format branch from efeb5ae to b1a05a6 Compare August 25, 2025 18:00
@notmandatory
Copy link
Member

notmandatory commented Aug 25, 2025

I manually tested and everything works fine, my only small nit is that the wallet unspent command is too long to fit on one line for even my wide monitor. But I don't know a better way to pretty display everything without somehow abbreviating some of the column data. I think it would make sense to apply a function on the outpoint and block hash cells, and it would also be a little smaller and more useful to display the Output ScriptPubkey as an address rather than as the raw script bytes.

Something like this in utils.rs:

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}")
}

Then the "outputs" match arm would look like:

Unspent => {
            let utxos = wallet.list_unspent().collect::<Vec<_>>();
            if cli_opts.pretty {
                let mut rows: Vec<Vec<CellStruct>> = vec![];
                for utxo in &utxos {
                    let height = utxo
                        .chain_position
                        .confirmation_height_upper_bound()
                        .map(|h| h.to_string())
                        .unwrap_or("Pending".to_string());

                    let block_hash = match &utxo.chain_position {
                        ChainPosition::Confirmed { anchor, .. } => anchor.block_id.hash.to_string(),
                        ChainPosition::Unconfirmed { .. } => "Unconfirmed".to_string(),
                    };

                    rows.push(vec![
                        shorten(utxo.outpoint, 8, 10).cell(),
                        utxo.txout
                            .value
                            .to_sat()
                            .to_string()
                            .cell()
                            .justify(Justify::Right),
                        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),
                        shorten(block_hash,8,8).cell().justify(Justify::Right),
                    ]);
                }
                let table = rows
                    .table()
                    .title(vec![
                        "Outpoint".cell().bold(true),
                        "Output (sat)".cell().bold(true),
                        "Output Address".cell().bold(true),
                        "Keychain".cell().bold(true),
                        "Is Spent".cell().bold(true),
                        "Index".cell().bold(true),
                        "Block Height".cell().bold(true),
                        "Block Hash".cell().bold(true),
                    ])
                    .display()
                    .map_err(|e| Error::Generic(e.to_string()))?;
                Ok(format!("{table}"))
            } else {
                Ok(serde_json::to_string_pretty(&utxos)?)
            }
        }

@notmandatory
Copy link
Member

Also looks like you'll need to add some feature gating to fix the --no-default-features ci build.

@tvpeter tvpeter force-pushed the feat/pretty-format branch from b1a05a6 to 015d8c2 Compare August 26, 2025 02:43
@tvpeter
Copy link
Collaborator Author

tvpeter commented Aug 26, 2025

I manually tested and everything works fine, my only small nit is that the wallet unspent command is too long to fit on one line for even my wide monitor. But I don't know a better way to pretty display everything without somehow abbreviating some of the column data. I think it would make sense to apply a function on the outpoint and block hash cells, and it would also be a little smaller and more useful to display the Output ScriptPubkey as an address rather than as the raw script bytes.

Updated. Thank you for the suggestion, it fits just great.

- add cli-table and `--pretty` flag to format
output of commands in a tabular form for:
    - offline wallet commands
    - repl
    - compile commands
    - keys commands
@tvpeter tvpeter force-pushed the feat/pretty-format branch from 015d8c2 to 98826f9 Compare August 26, 2025 09:00
Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 98826f9

- update README for `--pretty` flag
- update CHANGELOG
@tvpeter tvpeter force-pushed the feat/pretty-format branch from 98826f9 to 94012c6 Compare August 26, 2025 17:53
@tvpeter tvpeter merged commit a78cc3b into bitcoindevkit:master Aug 26, 2025
5 checks passed
@github-project-automation github-project-automation bot moved this to Done in BDK-CLI Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Add option to return command results in "pretty" format

3 participants