merge bitcoin#22547: Add progress bar for -getinfo

This commit is contained in:
Kittywhiskers Van Gogh 2021-07-26 00:08:19 +08:00
parent 1f89bfd176
commit b6ca36edda
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD

View File

@ -894,6 +894,29 @@ static void GetWalletBalances(UniValue& result)
result.pushKV("balances", balances);
}
/**
* GetProgressBar contructs a progress bar with 5% intervals.
*
* @param[in] progress The proportion of the progress bar to be filled between 0 and 1.
* @param[out] progress_bar String representation of the progress bar.
*/
static void GetProgressBar(double progress, std::string& progress_bar)
{
if (progress < 0 || progress > 1) return;
static constexpr double INCREMENT{0.05};
static const std::string COMPLETE_BAR{"\u2592"};
static const std::string INCOMPLETE_BAR{"\u2591"};
for (int i = 0; i < progress / INCREMENT; ++i) {
progress_bar += COMPLETE_BAR;
}
for (int i = 0; i < (1 - progress) / INCREMENT; ++i) {
progress_bar += INCOMPLETE_BAR;
}
}
/**
* ParseGetInfoResult takes in -getinfo result in UniValue object and parses it
* into a user friendly UniValue string to be printed on the console.
@ -936,7 +959,17 @@ static void ParseGetInfoResult(UniValue& result)
std::string result_string = strprintf("%sChain: %s%s\n", BLUE, result["chain"].getValStr(), RESET);
result_string += strprintf("Blocks: %s\n", result["blocks"].getValStr());
result_string += strprintf("Headers: %s\n", result["headers"].getValStr());
result_string += strprintf("Verification progress: %.4f%%\n", result["verificationprogress"].get_real() * 100);
const double ibd_progress{result["verificationprogress"].get_real()};
std::string ibd_progress_bar;
// Display the progress bar only if IBD progress is less than 99%
if (ibd_progress < 0.99) {
GetProgressBar(ibd_progress, ibd_progress_bar);
// Add padding between progress bar and IBD progress
ibd_progress_bar += " ";
}
result_string += strprintf("Verification progress: %s%.4f%%\n", ibd_progress_bar, ibd_progress * 100);
result_string += strprintf("Difficulty: %s\n\n", result["difficulty"].getValStr());
result_string += strprintf(