Merge pull request #10 from p-ranav/feature/time

Feature/time
This commit is contained in:
Pranav
2019-12-17 10:00:23 -06:00
committed by GitHub
13 changed files with 335 additions and 56 deletions

View File

@@ -49,8 +49,8 @@ int main() {
Here's the general structure of a progress bar: Here's the general structure of a progress bar:
``` ```
<prefix_text> <bar_start> <fill> <lead> <remaining> <bar_end> <progress_percentage>? <postfix_text> {prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}?] {postfix}
^^^^^^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^^^^ ^^^^^ Show/Hide ^^^^^ ^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^
``` ```
The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete. The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
@@ -206,6 +206,57 @@ int main() {
} }
``` ```
## Showing Time Elapsed/Remaining
All progress bars and spinners in `indicators` support showing time elapsed and time remaining. Inspired by python's [tqdm](https://github.com/tqdm/tqdm) module, the format of this meter is as follows:
```bash
[{elapsed}<{remaining}]
```
Below is an example for configuring this meter:
<p align="center">
<img src="img/time_meter.gif"/>
</p>
```cpp
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with(" [");
bar.fill_bar_progress_with("");
bar.lead_bar_progress_with("");
bar.fill_bar_remainder_with("-");
bar.end_bar_with("]");
bar.set_prefix_text("Training Gaze Network 👀");
bar.set_foreground_color(indicators::Color::YELLOW);
// Show time elapsed and remaining
bar.show_elapsed_time();
bar.show_remaining_time();
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}
```
# Block Progress Bar # Block Progress Bar
Are you in need of a smooth block progress bar using [unicode block elements](https://en.wikipedia.org/wiki/Block_Elements)? Use `BlockProgressBar` instead of `ProgressBar`. Thanks to [this blog post](https://mike42.me/blog/2018-06-make-better-cli-progress-bars-with-unicode-block-characters) for making `BlockProgressBar` an easy addition to the library. Are you in need of a smooth block progress bar using [unicode block elements](https://en.wikipedia.org/wiki/Block_Elements)? Use `BlockProgressBar` instead of `ProgressBar`. Thanks to [this blog post](https://mike42.me/blog/2018-06-make-better-cli-progress-bars-with-unicode-block-characters) for making `BlockProgressBar` an easy addition to the library.
@@ -274,7 +325,7 @@ int main() {
Here's the general structure of a progress spinner: Here's the general structure of a progress spinner:
``` ```
<prefix_text> <spinner> <progress_percentage>? <postfix_text> {prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
``` ```
ProgressSpinner has a vector of strings: `spinner_states`. At each update, the spinner will pick the next string from this sequence to print to the console. The spinner state can be updated similarly to ProgressBars: Using either `tick()` or `set_progress(value)`. ProgressSpinner has a vector of strings: `spinner_states`. At each update, the spinner will pick the next string from this sequence to print to the console. The spinner state can be updated similarly to ProgressBars: Using either `tick()` or `set_progress(value)`.

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env bash #!/usr/bin/env bash
find ./include ./demo/ -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i find ./include ./demo/ ./samples/ -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i

BIN
img/time_meter.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@@ -27,13 +27,15 @@ SOFTWARE.
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <chrono>
#include <cmath>
#include <indicators/color.hpp> #include <indicators/color.hpp>
#include <iomanip>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <cmath>
namespace indicators { namespace indicators {
@@ -75,11 +77,20 @@ public:
void hide_percentage() { _show_percentage = false; } 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) { void set_progress(float value) {
{ {
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress = value; _progress = value;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
@@ -88,12 +99,11 @@ public:
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress += 1; _progress += 1;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
size_t current() { size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; } bool is_completed() const { return _completed; }
@@ -115,11 +125,47 @@ private:
std::atomic<size_t> _max_postfix_text_length{0}; std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false}; std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true}; std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::mutex _mutex; std::mutex _mutex;
Color _foreground_color; 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 || _show_remaining_time) && !_saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
}
}
void _print_progress() { void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex}; 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; std::cout << termcolor::bold;
switch (_foreground_color) { switch (_foreground_color) {
case Color::GREY: case Color::GREY:
@@ -168,6 +214,24 @@ private:
if (_show_percentage) { if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%"; 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) {
if (_show_elapsed_time)
std::cout << "<";
else
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) if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10; _max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";

View File

@@ -27,7 +27,10 @@ SOFTWARE.
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <chrono>
#include <cmath>
#include <indicators/color.hpp> #include <indicators/color.hpp>
#include <iomanip>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <string> #include <string>
@@ -88,11 +91,20 @@ public:
void hide_percentage() { _show_percentage = false; } 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) { void set_progress(float value) {
{ {
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress = value; _progress = value;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
@@ -101,12 +113,11 @@ public:
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress += 1; _progress += 1;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
size_t current() { size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; } bool is_completed() const { return _completed; }
@@ -128,11 +139,47 @@ private:
std::atomic<size_t> _max_postfix_text_length{0}; std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false}; std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true}; std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::mutex _mutex; std::mutex _mutex;
Color _foreground_color; 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 || _show_remaining_time) && !_saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
}
}
void _print_progress() { void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex}; 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; std::cout << termcolor::bold;
switch (_foreground_color) { switch (_foreground_color) {
case Color::GREY: case Color::GREY:
@@ -175,6 +222,24 @@ private:
if (_show_percentage) { if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%"; 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) {
if (_show_elapsed_time)
std::cout << "<";
else
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) if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10; _max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";

View File

@@ -27,7 +27,10 @@ SOFTWARE.
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <chrono>
#include <cmath>
#include <indicators/color.hpp> #include <indicators/color.hpp>
#include <iomanip>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <string> #include <string>
@@ -59,6 +62,14 @@ public:
void hide_percentage() { _show_percentage = false; } 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 show_spinner() { _show_spinner = true; } void show_spinner() { _show_spinner = true; }
void hide_spinner() { _show_spinner = false; } void hide_spinner() { _show_spinner = false; }
@@ -68,6 +79,7 @@ public:
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress = value; _progress = value;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
@@ -76,12 +88,11 @@ public:
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_progress += 1; _progress += 1;
} }
_save_start_time();
_print_progress(); _print_progress();
} }
size_t current() { size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; } bool is_completed() const { return _completed; }
@@ -104,12 +115,48 @@ private:
std::atomic<size_t> _max_postfix_text_length{0}; std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false}; std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true}; std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::atomic<bool> _show_spinner{true}; std::atomic<bool> _show_spinner{true};
std::mutex _mutex; std::mutex _mutex;
Color _foreground_color; 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 || _show_remaining_time) && !_saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
}
}
void _print_progress() { void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex}; 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; std::cout << termcolor::bold;
switch (_foreground_color) { switch (_foreground_color) {
case Color::GREY: case Color::GREY:
@@ -143,6 +190,24 @@ private:
if (_show_percentage) { if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%"; 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) {
if (_show_elapsed_time)
std::cout << "<";
else
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) if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10; _max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";

View File

@@ -13,3 +13,6 @@ target_link_libraries(progress_bar_tick PRIVATE indica::indica)
add_executable(progress_spinner progress_spinner.cpp) add_executable(progress_spinner progress_spinner.cpp)
target_link_libraries(progress_spinner PRIVATE indica::indica) target_link_libraries(progress_spinner PRIVATE indica::indica)
add_executable(time_meter time_meter.cpp)
target_link_libraries(time_meter PRIVATE indica::indica)

View File

@@ -1,6 +1,6 @@
#include <chrono>
#include <indicators/block_progress_bar.hpp> #include <indicators/block_progress_bar.hpp>
#include <thread> #include <thread>
#include <chrono>
int main() { int main() {

View File

@@ -19,15 +19,12 @@ int main() {
// //
std::atomic<size_t> index{0}; std::atomic<size_t> index{0};
std::vector<std::string> status_text = std::vector<std::string> status_text = {"Rocket.exe is not responding",
{
"Rocket.exe is not responding",
"Finding a replacement engineer", "Finding a replacement engineer",
"Buying more snacks", "Buying more snacks",
"Assimilating the modding community", "Assimilating the modding community",
"Crossing fingers", "Crossing fingers",
"Porting KSP to a Nokia 3310" "Porting KSP to a Nokia 3310"};
};
// Let's say you want to append some status text to the right of the progress bar // Let's say you want to append some status text to the right of the progress bar
// You can use bar.set_postfix_text(...) to append text to the right // You can use bar.set_postfix_text(...) to append text to the right

View File

@@ -1,6 +1,6 @@
#include <chrono>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
#include <chrono>
int main() { int main() {
indicators::ProgressBar bar; indicators::ProgressBar bar;

View File

@@ -1,6 +1,6 @@
#include <chrono>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
#include <chrono>
int main() { int main() {
indicators::ProgressBar bar; indicators::ProgressBar bar;

34
samples/time_meter.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with(" [");
bar.fill_bar_progress_with("");
bar.lead_bar_progress_with("");
bar.fill_bar_remainder_with("-");
bar.end_bar_with("]");
bar.set_prefix_text("Training Gaze Network 👀");
bar.set_foreground_color(indicators::Color::YELLOW);
// Show time elapsed and remaining
bar.show_elapsed_time();
bar.show_remaining_time();
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}