mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Draft implementation of time elapsed/remaining meter
This commit is contained in:
@@ -27,7 +27,10 @@ SOFTWARE.
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <indicators/color.hpp>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@@ -88,11 +91,20 @@ public:
|
||||
|
||||
void hide_percentage() { _show_percentage = false; }
|
||||
|
||||
void show_elapsed_time() { _show_elapsed_time = true; }
|
||||
|
||||
void hide_elapsed_time() { _show_elapsed_time = false; }
|
||||
|
||||
void show_remaining_time() { _show_remaining_time = true; }
|
||||
|
||||
void hide_remaining_time() { _show_remaining_time = false; }
|
||||
|
||||
void set_progress(float value) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_progress = value;
|
||||
}
|
||||
_save_start_time();
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
@@ -101,12 +113,11 @@ public:
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_progress += 1;
|
||||
}
|
||||
_save_start_time();
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
size_t current() {
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
}
|
||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
||||
|
||||
bool is_completed() const { return _completed; }
|
||||
|
||||
@@ -128,11 +139,47 @@ private:
|
||||
std::atomic<size_t> _max_postfix_text_length{0};
|
||||
std::atomic<bool> _completed{false};
|
||||
std::atomic<bool> _show_percentage{true};
|
||||
std::atomic<bool> _show_elapsed_time{true};
|
||||
std::atomic<bool> _show_remaining_time{true};
|
||||
std::atomic<bool> _saved_start_time{false};
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||
std::mutex _mutex;
|
||||
Color _foreground_color;
|
||||
|
||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
typedef duration<int, ratio<86400>> days;
|
||||
char fill = os.fill();
|
||||
os.fill('0');
|
||||
auto d = duration_cast<days>(ns);
|
||||
ns -= d;
|
||||
auto h = duration_cast<hours>(ns);
|
||||
ns -= h;
|
||||
auto m = duration_cast<minutes>(ns);
|
||||
ns -= m;
|
||||
auto s = duration_cast<seconds>(ns);
|
||||
if (d.count() > 0)
|
||||
os << setw(2) << d.count() << "d:";
|
||||
if (h.count() > 0)
|
||||
os << setw(2) << h.count() << "h:";
|
||||
os << setw(2) << m.count() << "m:" << setw(2) << s.count() << 's';
|
||||
os.fill(fill);
|
||||
return os;
|
||||
};
|
||||
|
||||
void _save_start_time() {
|
||||
if (_show_elapsed_time && !_saved_start_time) {
|
||||
_start_time_point = std::chrono::high_resolution_clock::now();
|
||||
_saved_start_time = true;
|
||||
}
|
||||
}
|
||||
|
||||
void _print_progress() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||
|
||||
std::cout << termcolor::bold;
|
||||
switch (_foreground_color) {
|
||||
case Color::GREY:
|
||||
@@ -175,6 +222,21 @@ private:
|
||||
if (_show_percentage) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
||||
}
|
||||
|
||||
if (_show_elapsed_time) {
|
||||
std::cout << " [";
|
||||
_print_duration(std::cout, elapsed);
|
||||
}
|
||||
|
||||
if (_show_remaining_time) {
|
||||
std::cout << "<";
|
||||
auto eta = std::chrono::nanoseconds(
|
||||
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||
_print_duration(std::cout, remaining);
|
||||
std::cout << "]";
|
||||
}
|
||||
|
||||
if (_max_postfix_text_length == 0)
|
||||
_max_postfix_text_length = 10;
|
||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
||||
|
||||
Reference in New Issue
Block a user