progressBar: use chrono instead of clock

This commit is contained in:
Gwenhael Goavec-Merou 2021-02-24 13:31:55 +01:00
parent 16932786db
commit 566d33c2f1
2 changed files with 12 additions and 6 deletions

View File

@ -17,6 +17,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <chrono>
#include <string>
#include "progressBar.hpp" #include "progressBar.hpp"
#include "display.hpp" #include "display.hpp"
@ -24,7 +26,7 @@ ProgressBar::ProgressBar(std::string mess, int maxValue, int progressLen,
bool quiet): _mess(mess), _maxValue(maxValue), bool quiet): _mess(mess), _maxValue(maxValue),
_progressLen(progressLen), _quiet(quiet), _first(true) _progressLen(progressLen), _quiet(quiet), _first(true)
{ {
last_time = clock(); last_time = std::chrono::system_clock::now();
} }
void ProgressBar::display(int value, char force) void ProgressBar::display(int value, char force)
{ {
@ -36,8 +38,11 @@ void ProgressBar::display(int value, char force)
return; return;
} }
clock_t this_time = clock(); std::chrono::time_point<std::chrono::system_clock> this_time;
if (!force && ((((float)(this_time - last_time))/CLOCKS_PER_SEC) < 0.2)) this_time = std::chrono::system_clock::now();
std::chrono::duration<double> diff = this_time - last_time;
if (!force && diff.count() < 1)
{ {
return; return;
} }
@ -64,7 +69,7 @@ void ProgressBar::done()
void ProgressBar::fail() void ProgressBar::fail()
{ {
if (_quiet) { if (_quiet) {
printError("\nFail"); printError("Fail");
} else { } else {
display(_maxValue, true); display(_maxValue, true);
printError("\nFail"); printError("\nFail");

View File

@ -17,8 +17,8 @@
#ifndef PROGRESSBARE_HPP #ifndef PROGRESSBARE_HPP
#define PROGRESSBARE_HPP #define PROGRESSBARE_HPP
#include <time.h>
#include <iostream> #include <iostream>
#include <chrono>
class ProgressBar { class ProgressBar {
public: public:
@ -31,7 +31,8 @@ class ProgressBar {
std::string _mess; std::string _mess;
int _maxValue; int _maxValue;
int _progressLen; int _progressLen;
clock_t last_time; //records the time of last progress bar update //records the time of last progress bar update
std::chrono::time_point<std::chrono::system_clock> last_time;
bool _quiet; bool _quiet;
bool _first; bool _first;
}; };