Skip to content

Commit b851a92

Browse files
klementtanjonatack
andcommitted
cli: Add progress bar for -getinfo
Co-authored-by: jonatack <[email protected]>
1 parent 5d83e7d commit b851a92

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)