p-ranav / indicators

Activity Indicators for Modern C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can I set an interval time?

HaoKunT opened this issue · comments

I use indicators in a Computationally intensive task, I find using the indicators will make the task slower. if I do not use the indicators, it will take 1min. If I use the indicators, it will take 22min.

I want the tick() not print the bar. Can I set an interval time? Such as print the bar every 5 seconds. I don't kown how to config it.

Although the library does not provide a set_interval() function, this can be achieved using IndeterminateProgressBar and threading on your side:

  • Create a thread that ticks every N seconds, e.g., N = 2 in this example
  • Once the thread is started, the bar will ticked every 2 seconds in the thread function
  • Meanwhile, in the main thread, you can do your work
  • When the work is done, mark the bar as completed - At this point, the thread function will exit
#include <chrono>
#include <indicators/indeterminate_progress_bar.hpp>
#include <thread>

int main() {

  indicators::IndeterminateProgressBar bar{
      indicators::option::BarWidth{40},
      indicators::option::Start{"["},
      indicators::option::Fill{"·"},
      indicators::option::Lead{"<==>"},
      indicators::option::End{"]"},
      indicators::option::PostfixText{"Working"},
      indicators::option::ForegroundColor{indicators::Color::yellow},
      indicators::option::FontStyles{
          std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
  };

  // This thread will tick the bar every N seconds (N = 2 in this example)
  auto tick_thread = std::thread([&bar](std::chrono::seconds period) {
    while (true) {
      bar.tick();
      if (bar.is_completed())
        break;
      std::this_thread::sleep_for(period);
    }
  }, 
  std::chrono::seconds(2));

  // do work here
  std::this_thread::sleep_for(std::chrono::seconds(10));
  // when done, stop ticking
  bar.set_option(indicators::option::PostfixText{"Done"});
  bar.mark_as_completed();

  tick_thread.join();

  return 0;
}

But if I use IndeterminateProgressBar, I don't known current progress of task. Maybe the only way is using set_progress function in a thread.