p-ranav / indicators

Activity Indicators for Modern C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can i re-use a prior defined bar rather than define two separated bars?

YinLiLin opened this issue · comments

Is it possible to re-use a prior defined bar? as follows:

using namespace indicators;
indicators::ProgressBar bar {
        option::BarWidth{45},
        option::Start{"["},
        option::Fill{"■"},
        option::Lead{"■"},
        option::Remainder{" "},
        option::End{"]"},
        option::ShowPercentage{true},
        option::ShowElapsedTime{true},
        option::ShowRemainingTime{true},
        option::PrefixText{""}
};

bar.set_progress(0);
for(int i = 0; i < 100; i++){
    bar.set_progress(i+1);
}

#re-use the bar again
bar.set_progress(0);
for(int j = 0; j < 100; j++){
    bar.set_progress(j+1);
}

The first works fine, but the second bar will be printed in a single new line for each update. So i am just wondering if there is a solution for this problem?

Thanks.

Yes.

When the bar progress reaches 100%, it is marked as completed.

You can set this value as false and reuse the bar.

// main.cpp
#include <indicators.hpp>

int main() {

  using namespace indicators;
  indicators::ProgressBar bar {
                               option::BarWidth{45},
                               option::Start{"["},
                               option::Fill{""},
                               option::Lead{""},
                               option::Remainder{" "},
                               option::End{"]"},
                               option::ShowPercentage{true},
                               option::ShowElapsedTime{true},
                               option::ShowRemainingTime{true},
                               option::PrefixText{""}
  };

  bar.set_progress(0);
  for(int i = 0; i < 100; i++){
    bar.set_progress(i+1);
  }

  // Mark the bar as "not completed"
  bar.set_option(option::Completed{false});

  // re-yse the bar again
  bar.set_progress(0);
  for(int j = 0; j < 100; j++){
    bar.set_progress(j+1);
  }

}
$ ./main
[■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■] 100% [00m:00s<00m:00s]
[■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■] 100% [00m:00s<00m:00s]

It would be good to do this in set_progress if the input is 0, e.g., set_progress(0). Feel free to create a PR for this.

Great! Thank you for the professional answer.

You're welcome!