Skip to content

Commit b6ca36e

Browse files
committed
merge bitcoin#22547: Add progress bar for -getinfo
1 parent 1f89bfd commit b6ca36e

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
@@ -894,6 +894,29 @@ static void GetWalletBalances(UniValue& result)
894894
result.pushKV("balances", balances);
895895
}
896896

897+
/**
898+
* GetProgressBar contructs a progress bar with 5% intervals.
899+
*
900+
* @param[in] progress The proportion of the progress bar to be filled between 0 and 1.
901+
* @param[out] progress_bar String representation of the progress bar.
902+
*/
903+
static void GetProgressBar(double progress, std::string& progress_bar)
904+
{
905+
if (progress < 0 || progress > 1) return;
906+
907+
static constexpr double INCREMENT{0.05};
908+
static const std::string COMPLETE_BAR{"\u2592"};
909+
static const std::string INCOMPLETE_BAR{"\u2591"};
910+
911+
for (int i = 0; i < progress / INCREMENT; ++i) {
912+
progress_bar += COMPLETE_BAR;
913+
}
914+
915+
for (int i = 0; i < (1 - progress) / INCREMENT; ++i) {
916+
progress_bar += INCOMPLETE_BAR;
917+
}
918+
}
919+
897920
/**
898921
* ParseGetInfoResult takes in -getinfo result in UniValue object and parses it
899922
* into a user friendly UniValue string to be printed on the console.
@@ -936,7 +959,17 @@ static void ParseGetInfoResult(UniValue& result)
936959
std::string result_string = strprintf("%sChain: %s%s\n", BLUE, result["chain"].getValStr(), RESET);
937960
result_string += strprintf("Blocks: %s\n", result["blocks"].getValStr());
938961
result_string += strprintf("Headers: %s\n", result["headers"].getValStr());
939-
result_string += strprintf("Verification progress: %.4f%%\n", result["verificationprogress"].get_real() * 100);
962+
963+
const double ibd_progress{result["verificationprogress"].get_real()};
964+
std::string ibd_progress_bar;
965+
// Display the progress bar only if IBD progress is less than 99%
966+
if (ibd_progress < 0.99) {
967+
GetProgressBar(ibd_progress, ibd_progress_bar);
968+
// Add padding between progress bar and IBD progress
969+
ibd_progress_bar += " ";
970+
}
971+
972+
result_string += strprintf("Verification progress: %s%.4f%%\n", ibd_progress_bar, ibd_progress * 100);
940973
result_string += strprintf("Difficulty: %s\n\n", result["difficulty"].getValStr());
941974

942975
result_string += strprintf(

0 commit comments

Comments
 (0)