Merge bitcoin/bitcoin#22547: cli: Add progress bar for -getinfo

b851a92c06 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](https://github.com/bitcoin/bitcoin/pull/22547#issuecomment-887488172))
  * 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 b851a92c06
  theStack:
    re-ACK b851a92c06 🍹
  Zero-1729:
    re-tACK b851a92c06 (re-tested, works as expected 🍾)
  jonatack:
    ACK b851a92c06
  lsilva01:
    Tested ACK b851a92c06 on mainnet and signet on Ubuntu 20.04.

Tree-SHA512: 2046d812e3c4623c6cc3ed4c24f2daaa92ba12cd181fa21626b782743890c2373be3175cff1441a7ba37295b6d5818368deea90d483959875c22f7ad9b601a20
pull/22582/head
Samuel Dobson 3 years ago
commit ce0913148b
No known key found for this signature in database
GPG Key ID: D300116E1C875A3D

@ -884,6 +884,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.
@ -926,7 +949,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(

Loading…
Cancel
Save