Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e697a43fec | ||
|
|
358e3763c3 | ||
|
|
419737af61 | ||
|
|
9786633eba | ||
|
|
502ce33af4 | ||
|
|
8190a1e513 | ||
|
|
72644e5134 | ||
|
|
222a8ae4f9 | ||
|
|
2694badc93 | ||
|
|
ba604b7dd6 | ||
|
|
2853aba409 | ||
|
|
2719604ba9 | ||
|
|
a80e4af117 | ||
|
|
d862ccde37 | ||
|
|
63ea50103a | ||
|
|
d9aa064380 | ||
|
|
5449ad0629 | ||
|
|
c5969eefe4 | ||
|
|
34813c8642 | ||
|
|
c2832a5412 | ||
|
|
67663b9d7d | ||
|
|
45ad6da847 | ||
|
|
5eb8cc5f91 | ||
|
|
99a14b5563 | ||
|
|
3bc140f90f | ||
|
|
3f21b1d901 | ||
|
|
7eb31cf24b | ||
|
|
b726255bfd | ||
|
|
082b7a69e9 | ||
|
|
39922dbf80 | ||
|
|
4f0cd06969 | ||
|
|
70f843ce01 | ||
|
|
4e5e5cfa19 | ||
|
|
36e77a64e9 | ||
|
|
b542bedf5b | ||
|
|
8e41f2712e | ||
|
|
03000c8493 | ||
|
|
92725d6cb2 | ||
|
|
01966b8239 | ||
|
|
788f6c9e1e | ||
|
|
936043b7d4 | ||
|
|
c90f375952 | ||
|
|
2241c00bb6 | ||
|
|
bc128814fc | ||
|
|
5a556bf5e2 | ||
|
|
b5c2e78a72 | ||
|
|
4b2635020f | ||
|
|
5751a58477 | ||
|
|
a528d0d683 |
65
.travis.yml
Normal file
@@ -0,0 +1,65 @@
|
||||
language: generic
|
||||
|
||||
dist: bionic
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- env: CXX=g++-9 CC=gcc-9
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-9
|
||||
sources:
|
||||
- sourceline: 'ppa:ubuntu-toolchain-r/test'
|
||||
- env: CXX=g++-8 CC=gcc-8
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-8
|
||||
- env: CXX=g++-7 CC=gcc-7
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-7
|
||||
- env: CXX=g++-6 CC=gcc-6
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-6
|
||||
- env: CXX=g++-5 CC=gcc-5
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-5
|
||||
|
||||
- env: CXX=clang++-9 CC=clang-9
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- clang-9
|
||||
- libc++-9-dev
|
||||
- libc++abi-9-dev
|
||||
sources:
|
||||
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main'
|
||||
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
|
||||
- env: CXX=clang++-8 CC=clang-8
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- clang-8
|
||||
- libc++-8-dev
|
||||
- libc++abi-8-dev
|
||||
- env: CXX=clang++-7 CC=clang-7
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- clang-7
|
||||
- libc++-7-dev
|
||||
- libc++abi-7-dev
|
||||
|
||||
|
||||
script:
|
||||
- if [[ "$CXX" == clang* ]]; then export CXXFLAGS="-stdlib=libc++"; fi
|
||||
- mkdir build && cd build
|
||||
- cmake -DDEMO=ON -DSAMPLES=ON ..
|
||||
- make
|
||||
274
README.md
@@ -3,10 +3,13 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://travis-ci.com/p-ranav/indicators">
|
||||
<img src="https://travis-ci.com/p-ranav/indicators.svg?branch=master" alt="ci status"/>
|
||||
</a>
|
||||
<a href="https://github.com/p-ranav/indicators/blob/master/LICENSE">
|
||||
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
|
||||
</a>
|
||||
<img src="https://img.shields.io/badge/version-1.1-blue.svg?cacheSeconds=2592000" alt="version"/>
|
||||
<img src="https://img.shields.io/badge/version-1.5-blue.svg?cacheSeconds=2592000" alt="version"/>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@@ -20,36 +23,21 @@
|
||||
* Source for the above GIF can be found [here](demo/demo.cpp)
|
||||
* MIT License
|
||||
|
||||
# Table of Contents
|
||||
|
||||
* [Progress Bar](#progress-bar)
|
||||
* [Block Progress Bar](#block-progress-bar)
|
||||
* [Multi Progress](#multiprogress)
|
||||
* [Progress Spinner](#progress-spinner)
|
||||
* [Contributing](#contributing)
|
||||
* [License](#license)
|
||||
|
||||
# Progress bar
|
||||
|
||||
To introduce a progress bar in your application, include `indicators/progress_bar.hpp` and create a `ProgressBar` object.
|
||||
|
||||
```cpp
|
||||
#include <indicators/progress_bar.hpp>
|
||||
|
||||
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_postfix_text("Getting started");
|
||||
bar.set_foreground_color(indicators::Color::GREEN);
|
||||
|
||||
// Update bar state
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Here's the general structure of a progress bar:
|
||||
To introduce a progress bar in your application, include `indicators/progress_bar.hpp` and create a `ProgressBar` object. Here's the general structure of a progress bar:
|
||||
|
||||
```
|
||||
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}?] {postfix}
|
||||
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}] {postfix}
|
||||
^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^
|
||||
```
|
||||
|
||||
@@ -61,6 +49,10 @@ From application-level code, there are two ways in which you can update this pro
|
||||
|
||||
You can update the progress bar using `bar.tick()` which increments progress by exactly `1%`.
|
||||
|
||||
<p align="center">
|
||||
<img src="img/progress_bar_tick.gif"/>
|
||||
</p>
|
||||
|
||||
```cpp
|
||||
#include <indicators/progress_bar.hpp>
|
||||
#include <thread>
|
||||
@@ -76,7 +68,7 @@ int main() {
|
||||
bar.lead_bar_progress_with(">");
|
||||
bar.fill_bar_remainder_with(" ");
|
||||
bar.end_bar_with("]");
|
||||
bar.set_postfix_text("Getting started");
|
||||
bar.set_postfix_text("Extracting Archive");
|
||||
bar.set_foreground_color(indicators::Color::GREEN);
|
||||
|
||||
// Update bar state
|
||||
@@ -97,110 +89,61 @@ The above code will print a progress bar that goes from 0 to 100% at the rate of
|
||||
|
||||
If you'd rather control progress of the bar in discrete steps, consider using `bar.set_progress(value)`. Example:
|
||||
|
||||
|
||||
<p align="center">
|
||||
<img src="img/progress_bar_set_progress.gif"/>
|
||||
</p>
|
||||
|
||||
```cpp
|
||||
#include <chrono>
|
||||
#include <indicators/progress_bar.hpp>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
int main() {
|
||||
|
||||
// Hide cursor
|
||||
std::cout << "\e[?25l";
|
||||
|
||||
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_postfix_text("Getting started");
|
||||
bar.set_foreground_color(indicators::Color::GREEN);
|
||||
|
||||
// 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
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
## Multi-threaded Example
|
||||
|
||||
```cpp
|
||||
#include <indicators/progress_bar.hpp>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
|
||||
indicators::ProgressBar 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_foreground_color(indicators::Color::YELLOW);
|
||||
bar.set_postfix_text("Loading dependency 1/4");
|
||||
bar.set_foreground_color(indicators::Color::CYAN);
|
||||
|
||||
// As configured, the bar will look like this:
|
||||
//
|
||||
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
|
||||
//
|
||||
//
|
||||
// Update bar state
|
||||
bar.set_progress(10); // 10% done
|
||||
|
||||
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"
|
||||
};
|
||||
// do some work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
||||
|
||||
// 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
|
||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
//
|
||||
//
|
||||
bar.set_postfix_text("Loading dependency 2/4");
|
||||
|
||||
auto job = [&bar, &index, &status_text]() {
|
||||
while (true) {
|
||||
if (bar.is_completed()) {
|
||||
break;
|
||||
}
|
||||
bar.set_postfix_text(status_text[index % status_text.size()]);
|
||||
bar.tick();
|
||||
index += 1;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
};
|
||||
bar.set_progress(30); // 30% done
|
||||
|
||||
std::thread first_job(job);
|
||||
std::thread second_job(job);
|
||||
std::thread third_job(job);
|
||||
std::thread last_job(job);
|
||||
// do some more work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(700));
|
||||
|
||||
first_job.join();
|
||||
second_job.join();
|
||||
third_job.join();
|
||||
last_job.join();
|
||||
bar.set_postfix_text("Loading dependency 3/4");
|
||||
|
||||
bar.set_progress(65); // 65% done
|
||||
|
||||
// do final bit of work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(900));
|
||||
|
||||
bar.set_postfix_text("Loaded dependencies!");
|
||||
|
||||
bar.set_progress(100); // all done
|
||||
|
||||
bar.mark_as_completed();
|
||||
|
||||
// Show cursor
|
||||
std::cout << "\e[?25h";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -294,29 +237,110 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
# Progress Spinner
|
||||
# MultiProgress
|
||||
|
||||
To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object.
|
||||
`indicators` supports management of multiple progress bars with the `MultiProgress` class template.
|
||||
|
||||
`template <typename Indicator, size_t count> class MultiProgress` is a class template that holds references to multiple progress bars and provides a safe interface to update the state of each bar. `MultiProgress` works with both `ProgressBar` and `BlockProgressBar` classes.
|
||||
|
||||
Below is an example `MultiProgress` object that manages three `ProgressBar` objects.
|
||||
|
||||
<p align="center">
|
||||
<img src="img/multi_progress.gif"/>
|
||||
</p>
|
||||
|
||||
```cpp
|
||||
#include <indicators/progress_spinner.hpp>
|
||||
#include <indicators/multi_progress.hpp>
|
||||
#include <indicators/progress_bar.hpp>
|
||||
|
||||
int main() {
|
||||
indicators::ProgressSpinner spinner;
|
||||
|
||||
// Configure the spinner
|
||||
spinner.set_prefix_text(" ");
|
||||
spinner.set_postfix_text("Checking credentials");
|
||||
spinner.set_foreground_color(indicators::Color::YELLOW);
|
||||
spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
||||
// Configure first progress bar
|
||||
indicators::ProgressBar bar1;
|
||||
bar1.set_bar_width(50);
|
||||
bar1.start_bar_with("[");
|
||||
bar1.fill_bar_progress_with("■");
|
||||
bar1.lead_bar_progress_with("■");
|
||||
bar1.fill_bar_remainder_with(" ");
|
||||
bar1.end_bar_with(" ]");
|
||||
bar1.set_foreground_color(indicators::Color::YELLOW);
|
||||
bar1.show_elapsed_time();
|
||||
bar1.show_remaining_time();
|
||||
bar1.set_prefix_text("Progress Bar #1 ");
|
||||
|
||||
// Update spinner state
|
||||
// Configure second progress bar
|
||||
indicators::ProgressBar bar2;
|
||||
bar2.set_bar_width(50);
|
||||
bar2.start_bar_with("[");
|
||||
bar2.fill_bar_progress_with("=");
|
||||
bar2.lead_bar_progress_with(">");
|
||||
bar2.fill_bar_remainder_with(" ");
|
||||
bar2.end_bar_with(" ]");
|
||||
bar2.set_foreground_color(indicators::Color::CYAN);
|
||||
bar2.show_elapsed_time();
|
||||
bar2.show_remaining_time();
|
||||
bar2.set_prefix_text("Progress Bar #2 ");
|
||||
|
||||
// Configure third progress bar
|
||||
indicators::ProgressBar bar3;
|
||||
bar3.set_bar_width(50);
|
||||
bar3.start_bar_with("[");
|
||||
bar3.fill_bar_progress_with("#");
|
||||
bar3.lead_bar_progress_with("#");
|
||||
bar3.fill_bar_remainder_with(" ");
|
||||
bar3.end_bar_with(" ]");
|
||||
bar3.set_foreground_color(indicators::Color::RED);
|
||||
bar3.show_elapsed_time();
|
||||
bar3.show_remaining_time();
|
||||
bar3.set_prefix_text("Progress Bar #3 ");
|
||||
|
||||
// Construct MultiProgress object
|
||||
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);
|
||||
|
||||
std::cout << "Multiple Progress Bars:\n";
|
||||
|
||||
auto job1 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<0>();
|
||||
if (bars.is_completed<0>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
};
|
||||
|
||||
auto job2 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<1>();
|
||||
if (bars.is_completed<1>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
};
|
||||
|
||||
auto job3 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<2>();
|
||||
if (bars.is_completed<2>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||
}
|
||||
};
|
||||
|
||||
std::thread first_job(job1);
|
||||
std::thread second_job(job2);
|
||||
std::thread third_job(job3);
|
||||
|
||||
first_job.join();
|
||||
second_job.join();
|
||||
third_job.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Here's the general structure of a progress spinner:
|
||||
# Progress Spinner
|
||||
|
||||
To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object. Here's the general structure of a progress spinner:
|
||||
|
||||
```
|
||||
{prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
|
||||
@@ -324,6 +348,10 @@ Here's the general structure of a progress spinner:
|
||||
|
||||
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)`.
|
||||
|
||||
<p align="center">
|
||||
<img src="img/progress_spinner.gif"/>
|
||||
</p>
|
||||
|
||||
```cpp
|
||||
#include <indicators/progress_spinner.hpp>
|
||||
|
||||
|
||||
BIN
img/multi_progress.gif
Normal file
|
After Width: | Height: | Size: 185 KiB |
BIN
img/progress_bar_set_progress.gif
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
img/progress_bar_tick.gif
Normal file
|
After Width: | Height: | Size: 41 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 146 KiB |
@@ -25,6 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
#define NOMINMAX
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -103,7 +104,10 @@ public:
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
||||
size_t current() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return _completed; }
|
||||
|
||||
@@ -130,7 +134,10 @@ private:
|
||||
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;
|
||||
Color _foreground_color{indicators::Color::WHITE};
|
||||
|
||||
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||
std::atomic<bool> _multi_progress_mode{false};
|
||||
|
||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
||||
using namespace std;
|
||||
@@ -161,7 +168,13 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void _print_progress() {
|
||||
void _print_progress(bool from_multi_progress = false) {
|
||||
if (_multi_progress_mode && !from_multi_progress) {
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
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);
|
||||
@@ -201,7 +214,7 @@ private:
|
||||
auto whole_width = std::floor(progress * _bar_width);
|
||||
auto remainder_width = fmod((progress * _bar_width), 1.0f);
|
||||
auto part_width = std::floor(remainder_width * lead_characters.size());
|
||||
_lead = lead_characters[part_width];
|
||||
_lead = lead_characters[size_t(part_width)];
|
||||
if ((_bar_width - whole_width - 1) < 0)
|
||||
_lead = "";
|
||||
for (size_t i = 0; i < whole_width; ++i)
|
||||
@@ -230,6 +243,9 @@ private:
|
||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||
_print_duration(std::cout, remaining);
|
||||
std::cout << "]";
|
||||
} else {
|
||||
if (_show_elapsed_time)
|
||||
std::cout << "]";
|
||||
}
|
||||
|
||||
if (_max_postfix_text_length == 0)
|
||||
@@ -239,7 +255,7 @@ private:
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
}
|
||||
if (_completed)
|
||||
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||
std::cout << termcolor::reset << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
94
include/indicators/multi_progress.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Activity Indicators for Modern C++
|
||||
https://github.com/p-ranav/indicators
|
||||
|
||||
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <indicators/color.hpp>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace indicators {
|
||||
|
||||
template <typename Indicator, size_t count> class MultiProgress {
|
||||
public:
|
||||
template <typename... Indicators,
|
||||
typename = typename std::enable_if<(sizeof...(Indicators) == count)>::type>
|
||||
explicit MultiProgress(Indicators &... bars) {
|
||||
_bars = {bars...};
|
||||
for (auto &bar : _bars) {
|
||||
bar.get()._multi_progress_mode = true;
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) {
|
||||
if (!_bars[index].get().is_completed())
|
||||
_bars[index].get().set_progress(value);
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
typename std::enable_if<(index >= 0 && index < count), void>::type tick() {
|
||||
if (!_bars[index].get().is_completed())
|
||||
_bars[index].get().tick();
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
template <size_t index>
|
||||
typename std::enable_if<(index >= 0 && index < count), bool>::type is_completed() const {
|
||||
return _bars[index].get().is_completed();
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<bool> _started{false};
|
||||
std::mutex _mutex;
|
||||
std::vector<std::reference_wrapper<Indicator>> _bars;
|
||||
|
||||
bool _all_completed() {
|
||||
bool result{true};
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
result &= _bars[i].get().is_completed();
|
||||
return result;
|
||||
}
|
||||
|
||||
void _print_progress() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
if (_started)
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
std::cout << "\x1b[A";
|
||||
for (auto &bar : _bars) {
|
||||
bar.get()._print_progress(true);
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << termcolor::reset;
|
||||
if (!_started)
|
||||
_started = true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace indicators
|
||||
@@ -25,6 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
#define NOMINMAX
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -117,7 +118,10 @@ public:
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
||||
size_t current() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return _completed; }
|
||||
|
||||
@@ -144,7 +148,10 @@ private:
|
||||
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;
|
||||
Color _foreground_color{indicators::Color::WHITE};
|
||||
|
||||
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||
std::atomic<bool> _multi_progress_mode{false};
|
||||
|
||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
||||
using namespace std;
|
||||
@@ -175,7 +182,13 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void _print_progress() {
|
||||
void _print_progress(bool from_multi_progress = false) {
|
||||
if (_multi_progress_mode && !from_multi_progress) {
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
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);
|
||||
@@ -238,6 +251,9 @@ private:
|
||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||
_print_duration(std::cout, remaining);
|
||||
std::cout << "]";
|
||||
} else {
|
||||
if (_show_elapsed_time)
|
||||
std::cout << "]";
|
||||
}
|
||||
|
||||
if (_max_postfix_text_length == 0)
|
||||
@@ -247,7 +263,7 @@ private:
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
}
|
||||
if (_completed)
|
||||
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||
std::cout << termcolor::reset << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,6 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
#define NOMINMAX
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -92,7 +93,10 @@ public:
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
||||
size_t current() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return _completed; }
|
||||
|
||||
@@ -206,6 +210,9 @@ private:
|
||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||
_print_duration(std::cout, remaining);
|
||||
std::cout << "]";
|
||||
} else {
|
||||
if (_show_elapsed_time)
|
||||
std::cout << "]";
|
||||
}
|
||||
|
||||
if (_max_postfix_text_length == 0)
|
||||
|
||||
@@ -16,3 +16,9 @@ target_link_libraries(progress_spinner PRIVATE indica::indica)
|
||||
|
||||
add_executable(time_meter time_meter.cpp)
|
||||
target_link_libraries(time_meter PRIVATE indica::indica)
|
||||
|
||||
add_executable(multi_progress_bar multi_progress_bar.cpp)
|
||||
target_link_libraries(multi_progress_bar PRIVATE indica::indica)
|
||||
|
||||
add_executable(multi_block_progress_bar multi_block_progress_bar.cpp)
|
||||
target_link_libraries(multi_block_progress_bar PRIVATE indica::indica)
|
||||
|
||||
67
samples/multi_block_progress_bar.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <indicators/block_progress_bar.hpp>
|
||||
#include <indicators/multi_progress.hpp>
|
||||
|
||||
int main() {
|
||||
|
||||
indicators::BlockProgressBar bar1;
|
||||
bar1.set_bar_width(50);
|
||||
bar1.set_foreground_color(indicators::Color::YELLOW);
|
||||
bar1.show_elapsed_time();
|
||||
bar1.show_remaining_time();
|
||||
bar1.set_prefix_text("Progress Bar #1 ");
|
||||
|
||||
indicators::BlockProgressBar bar2;
|
||||
bar2.set_bar_width(50);
|
||||
bar2.set_foreground_color(indicators::Color::CYAN);
|
||||
bar2.show_elapsed_time();
|
||||
bar2.show_remaining_time();
|
||||
bar2.set_prefix_text("Progress Bar #2 ");
|
||||
|
||||
indicators::BlockProgressBar bar3;
|
||||
bar3.set_bar_width(50);
|
||||
bar3.set_foreground_color(indicators::Color::RED);
|
||||
bar3.show_elapsed_time();
|
||||
bar3.show_remaining_time();
|
||||
bar3.set_prefix_text("Progress Bar #3 ");
|
||||
|
||||
indicators::MultiProgress<indicators::BlockProgressBar, 3> bars(bar1, bar2, bar3);
|
||||
|
||||
std::cout << "Multiple Progress Bars:\n";
|
||||
|
||||
auto job1 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<0>();
|
||||
if (bars.is_completed<0>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
};
|
||||
|
||||
auto job2 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<1>();
|
||||
if (bars.is_completed<1>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
};
|
||||
|
||||
auto job3 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<2>();
|
||||
if (bars.is_completed<2>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||
}
|
||||
};
|
||||
|
||||
std::thread first_job(job1);
|
||||
std::thread second_job(job2);
|
||||
std::thread third_job(job3);
|
||||
|
||||
first_job.join();
|
||||
second_job.join();
|
||||
third_job.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
82
samples/multi_progress_bar.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <indicators/multi_progress.hpp>
|
||||
#include <indicators/progress_bar.hpp>
|
||||
|
||||
int main() {
|
||||
|
||||
indicators::ProgressBar bar1;
|
||||
bar1.set_bar_width(50);
|
||||
bar1.start_bar_with("[");
|
||||
bar1.fill_bar_progress_with("■");
|
||||
bar1.lead_bar_progress_with("■");
|
||||
bar1.fill_bar_remainder_with(" ");
|
||||
bar1.end_bar_with(" ]");
|
||||
bar1.set_foreground_color(indicators::Color::YELLOW);
|
||||
bar1.show_elapsed_time();
|
||||
bar1.show_remaining_time();
|
||||
bar1.set_prefix_text("Progress Bar #1 ");
|
||||
|
||||
indicators::ProgressBar bar2;
|
||||
bar2.set_bar_width(50);
|
||||
bar2.start_bar_with("[");
|
||||
bar2.fill_bar_progress_with("=");
|
||||
bar2.lead_bar_progress_with(">");
|
||||
bar2.fill_bar_remainder_with(" ");
|
||||
bar2.end_bar_with(" ]");
|
||||
bar2.set_foreground_color(indicators::Color::CYAN);
|
||||
bar2.show_elapsed_time();
|
||||
bar2.show_remaining_time();
|
||||
bar2.set_prefix_text("Progress Bar #2 ");
|
||||
|
||||
indicators::ProgressBar bar3;
|
||||
bar3.set_bar_width(50);
|
||||
bar3.start_bar_with("[");
|
||||
bar3.fill_bar_progress_with("#");
|
||||
bar3.lead_bar_progress_with("#");
|
||||
bar3.fill_bar_remainder_with(" ");
|
||||
bar3.end_bar_with(" ]");
|
||||
bar3.set_foreground_color(indicators::Color::RED);
|
||||
bar3.show_elapsed_time();
|
||||
bar3.show_remaining_time();
|
||||
bar3.set_prefix_text("Progress Bar #3 ");
|
||||
|
||||
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);
|
||||
|
||||
std::cout << "Multiple Progress Bars:\n";
|
||||
|
||||
auto job1 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<0>();
|
||||
if (bars.is_completed<0>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
};
|
||||
|
||||
auto job2 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<1>();
|
||||
if (bars.is_completed<1>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
};
|
||||
|
||||
auto job3 = [&bars]() {
|
||||
while (true) {
|
||||
bars.tick<2>();
|
||||
if (bars.is_completed<2>())
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||
}
|
||||
};
|
||||
|
||||
std::thread first_job(job1);
|
||||
std::thread second_job(job2);
|
||||
std::thread third_job(job3);
|
||||
|
||||
first_job.join();
|
||||
second_job.join();
|
||||
third_job.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -3,36 +3,50 @@
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
|
||||
// Hide cursor
|
||||
std::cout << "\e[?25l";
|
||||
|
||||
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.fill_bar_progress_with("■");
|
||||
bar.lead_bar_progress_with("■");
|
||||
bar.fill_bar_remainder_with("-");
|
||||
bar.end_bar_with(" ]");
|
||||
bar.set_postfix_text("Getting started");
|
||||
bar.set_foreground_color(indicators::Color::GREEN);
|
||||
bar.set_postfix_text("Loading dependency 1/4");
|
||||
bar.set_foreground_color(indicators::Color::CYAN);
|
||||
|
||||
// Update bar state
|
||||
|
||||
bar.set_progress(10); // 10% done
|
||||
|
||||
// do some work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
||||
|
||||
bar.set_postfix_text("Loading dependency 2/4");
|
||||
|
||||
bar.set_progress(30); // 30% done
|
||||
|
||||
// do some more work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(700));
|
||||
|
||||
bar.set_postfix_text("Loading dependency 3/4");
|
||||
|
||||
bar.set_progress(65); // 65% done
|
||||
|
||||
// do final bit of work
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(900));
|
||||
|
||||
bar.set_postfix_text("Loaded dependencies!");
|
||||
|
||||
bar.set_progress(100); // all done
|
||||
|
||||
bar.mark_as_completed();
|
||||
|
||||
// Show cursor
|
||||
std::cout << "\e[?25h";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||