Skip to content

Commit ce09131

Browse files
committed
Merge bitcoin/bitcoin#22547: cli: Add progress bar for -getinfo
b851a92 cli: Add progress bar for -getinfo (klementtan) Pull request description: Add a progress bar for the `Verification progress` attribute in `-getinfo` when verification progress `< 99%`. ![image](https://user-images.githubusercontent.com/49265907/127897458-27d8aaa9-7893-4665-9c40-36389a8d9cbb.png) **Motivation**: * Improve user-friendliness of `-getinfo` * Can be useful with `watch -n 1 bitcoin-cli -getinfo`(suggested by theStack [below](bitcoin/bitcoin#22547 (comment))) * The progress bar is only display when are still syncing to tip(verification progress `< 99%`) **Reviewing** If your verification progress is `> 99%` you can restart the verification progress with ```shell $ ./src/bitcoind -reindex $./src/bitcoin-cli -getinfo ``` ACKs for top commit: prayank23: reACK bitcoin/bitcoin@b851a92 theStack: re-ACK b851a92 🍹 Zero-1729: re-tACK b851a92 (re-tested, works as expected 🍾) jonatack: ACK b851a92 lsilva01: Tested ACK bitcoin/bitcoin@b851a92 on mainnet and signet on Ubuntu 20.04. Tree-SHA512: 2046d812e3c4623c6cc3ed4c24f2daaa92ba12cd181fa21626b782743890c2373be3175cff1441a7ba37295b6d5818368deea90d483959875c22f7ad9b601a20
2 parents 8193294 + b851a92 commit ce09131

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/bitcoin-cli.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,29 @@ static void GetWalletBalances(UniValue& result)
884884
result.pushKV("balances", balances);
885885
}
886886

887+
/**
888+
* GetProgressBar contructs a progress bar with 5% intervals.
889+
*
890+
* @param[in] progress The proportion of the progress bar to be filled between 0 and 1.
891+
* @param[out] progress_bar String representation of the progress bar.
892+
*/
893+
static void GetProgressBar(double progress, std::string& progress_bar)
894+
{
895+
if (progress < 0 || progress > 1) return;
896+
897+
static constexpr double INCREMENT{0.05};
898+
static const std::string COMPLETE_BAR{"\u2592"};
899+
static const std::string INCOMPLETE_BAR{"\u2591"};
900+
901+
for (int i = 0; i < progress / INCREMENT; ++i) {
902+
progress_bar += COMPLETE_BAR;
903+
}
904+
905+
for (int i = 0; i < (1 - progress) / INCREMENT; ++i) {
906+
progress_bar += INCOMPLETE_BAR;
907+
}
908+
}
909+
887910
/**
888911
* ParseGetInfoResult takes in -getinfo result in UniValue object and parses it
889912
* into a user friendly UniValue string to be printed on the console.
@@ -926,7 +949,17 @@ static void ParseGetInfoResult(UniValue& result)
926949
std::string result_string = strprintf("%sChain: %s%s\n", BLUE, result["chain"].getValStr(), RESET);
927950
result_string += strprintf("Blocks: %s\n", result["blocks"].getValStr());
928951
result_string += strprintf("Headers: %s\n", result["headers"].getValStr());
929-
result_string += strprintf("Verification progress: %.4f%%\n", result["verificationprogress"].get_real() * 100);
952+
953+
const double ibd_progress{result["verificationprogress"].get_real()};
954+
std::string ibd_progress_bar;
955+
// Display the progress bar only if IBD progress is less than 99%
956+
if (ibd_progress < 0.99) {
957+
GetProgressBar(ibd_progress, ibd_progress_bar);
958+
// Add padding between progress bar and IBD progress
959+
ibd_progress_bar += " ";
960+
}
961+
962+
result_string += strprintf("Verification progress: %s%.4f%%\n", ibd_progress_bar, ibd_progress * 100);
930963
result_string += strprintf("Difficulty: %s\n\n", result["difficulty"].getValStr());
931964

932965
result_string += strprintf(

0 commit comments

Comments
 (0)