Updated samples/demos to show usage of hide/show time elapsed/remaining

This commit is contained in:
Pranav Srinivas Kumar
2019-12-17 09:31:43 -06:00
parent 8198d8a802
commit 6224a46371
10 changed files with 72 additions and 43 deletions

View File

@@ -1,2 +1,2 @@
#!/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

View File

@@ -18,6 +18,8 @@ int main() {
p.lead_bar_progress_with("");
p.fill_bar_remainder_with(" ");
p.end_bar_with(" ]");
p.hide_elapsed_time();
p.hide_remaining_time();
p.set_foreground_color(indicators::Color::YELLOW);
std::atomic<size_t> index{0};
@@ -57,6 +59,8 @@ int main() {
p.lead_bar_progress_with("");
p.fill_bar_remainder_with("");
p.end_bar_with("");
p.hide_elapsed_time();
p.hide_remaining_time();
p.set_foreground_color(indicators::Color::WHITE);
p.hide_percentage();
auto job = [&p]() {
@@ -87,6 +91,8 @@ int main() {
p.fill_bar_remainder_with(" ");
p.end_bar_with("]");
p.set_postfix_text("Getting started");
p.hide_elapsed_time();
p.hide_remaining_time();
p.set_foreground_color(indicators::Color::GREEN);
auto job = [&p]() {
while (true) {
@@ -124,6 +130,8 @@ int main() {
p4.set_foreground_color(indicators::Color::CYAN);
p4.set_postfix_text("Restoring system state");
p4.hide_percentage();
p4.hide_elapsed_time();
p4.hide_remaining_time();
std::atomic<size_t> index4{0};
auto job4 = [&p4, &index4, &lead_spinner]() {
while (true) {
@@ -167,6 +175,8 @@ int main() {
p.set_progress(100);
p.set_foreground_color(indicators::Color::WHITE);
p.set_postfix_text("Reverting system restore");
p.hide_elapsed_time();
p.hide_remaining_time();
std::atomic<size_t> progress{100};
auto job = [&p, &progress]() {
while (true) {
@@ -194,6 +204,8 @@ int main() {
p.set_postfix_text("Checking credentials");
p.set_foreground_color(indicators::Color::YELLOW);
p.set_spinner_states({"", "", "", "", "", "", "", ""});
p.hide_elapsed_time();
p.hide_remaining_time();
auto job = [&p]() {
while (true) {
if (p.is_completed()) {
@@ -224,6 +236,8 @@ int main() {
p.set_foreground_color(indicators::Color::WHITE);
p.set_spinner_states({"", "", "", ""});
p.hide_percentage();
p.hide_elapsed_time();
p.hide_remaining_time();
auto job = [&p]() {
while (true) {
auto current = p.current();
@@ -269,6 +283,8 @@ int main() {
p2.end_bar_with("🌑");
p2.set_postfix_text("Achieved low-Earth orbit");
p2.set_foreground_color(indicators::Color::WHITE);
p2.hide_elapsed_time();
p2.hide_remaining_time();
std::vector<std::string> ship_trail{"", "", "", "", "", "", "", ""};
std::atomic<int> ship_trail_index{0};
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {

View File

@@ -155,7 +155,7 @@ private:
};
void _save_start_time() {
if (_show_elapsed_time && !_saved_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;
}
@@ -223,6 +223,8 @@ private:
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);

View File

@@ -169,7 +169,7 @@ private:
};
void _save_start_time() {
if (_show_elapsed_time && !_saved_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;
}
@@ -231,6 +231,8 @@ private:
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);

View File

@@ -146,7 +146,7 @@ private:
};
void _save_start_time() {
if (_show_elapsed_time && !_saved_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;
}
@@ -199,6 +199,8 @@ private:
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);

View File

@@ -1,20 +1,22 @@
#include <chrono>
#include <indicators/block_progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
// Hide cursor
std::cout << "\e[?25l";
indicators::BlockProgressBar bar;
// Configure the bar
bar.set_bar_width(80);
bar.start_bar_with("[");
bar.end_bar_with("]");
bar.set_foreground_color(indicators::Color::WHITE);
bar.set_foreground_color(indicators::Color::WHITE);
bar.hide_elapsed_time();
bar.hide_remaining_time();
// Update bar state
auto progress = 0.0f;
while (true) {
@@ -26,7 +28,7 @@ int main() {
}
// Show cursor
std::cout << "\e[?25h";
std::cout << "\e[?25h";
return 0;
}

View File

@@ -11,31 +11,30 @@ int main() {
bar.fill_bar_remainder_with("-");
bar.end_bar_with("]");
bar.set_foreground_color(indicators::Color::YELLOW);
bar.hide_elapsed_time();
bar.hide_remaining_time();
// As configured, the bar will look like this:
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
//
//
std::atomic<size_t> index{0};
std::vector<std::string> status_text =
{
"Rocket.exe is not responding",
"Finding a replacement engineer",
"Buying more snacks",
"Assimilating the modding community",
"Crossing fingers",
"Porting KSP to a Nokia 3310"
};
std::vector<std::string> status_text = {"Rocket.exe is not responding",
"Finding a replacement engineer",
"Buying more snacks",
"Assimilating the modding community",
"Crossing fingers",
"Porting KSP to a Nokia 3310"};
// 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
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70% Finding a replacement engineer
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
//
//
auto job = [&bar, &index, &status_text]() {
while (true) {
@@ -58,6 +57,6 @@ int main() {
second_job.join();
third_job.join();
last_job.join();
return 0;
}

View File

@@ -1,10 +1,10 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with("[");
@@ -13,26 +13,28 @@ int main() {
bar.fill_bar_remainder_with(" ");
bar.end_bar_with("]");
bar.set_postfix_text("Getting started");
bar.set_foreground_color(indicators::Color::GREEN);
bar.set_foreground_color(indicators::Color::GREEN);
bar.hide_elapsed_time();
bar.hide_remaining_time();
// Update bar state
bar.set_progress(10); // 10% done
// do some work
std::this_thread::sleep_for(std::chrono::milliseconds(100));
bar.set_progress(30); // 30% done
// do some more work
// do some more work
std::this_thread::sleep_for(std::chrono::milliseconds(600));
bar.set_progress(65); // 65% done
// do final bit of work
std::this_thread::sleep_for(std::chrono::milliseconds(300));
bar.set_progress(100); // all done
return 0;
}

View File

@@ -1,10 +1,10 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with("[");
@@ -13,8 +13,10 @@ int main() {
bar.fill_bar_remainder_with(" ");
bar.end_bar_with("]");
bar.set_postfix_text("Getting started");
bar.set_foreground_color(indicators::Color::GREEN);
bar.set_foreground_color(indicators::Color::GREEN);
bar.hide_elapsed_time();
bar.hide_remaining_time();
// Update bar state
while (true) {
bar.tick();

View File

@@ -6,12 +6,14 @@ int main() {
std::cout << "\e[?25l";
indicators::ProgressSpinner spinner;
// Configure the spinner
spinner.set_postfix_text("Checking credentials");
spinner.set_foreground_color(indicators::Color::YELLOW);
spinner.set_spinner_states({"", "", "", "", "", "", "", ""});
spinner.hide_elapsed_time();
spinner.hide_remaining_time();
// Update spinner state
auto job = [&spinner]() {
while (true) {
@@ -32,7 +34,7 @@ int main() {
thread.join();
// Show cursor
std::cout << "\e[?25h";
std::cout << "\e[?25h";
return 0;
}