mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Compare commits
75 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 | ||
|
|
74674154f5 | ||
|
|
c42ae6862e | ||
|
|
1e45ef3530 | ||
|
|
d5a1c9a440 | ||
|
|
8c37f2f1dc | ||
|
|
aa5dffa4e2 | ||
|
|
78b7abcfbd | ||
|
|
fdad265e99 | ||
|
|
f28ab68c60 | ||
|
|
6224a46371 | ||
|
|
8198d8a802 | ||
|
|
5360fec641 | ||
|
|
c770704697 | ||
|
|
97c89284a9 | ||
|
|
24d92beaca | ||
|
|
fe38859715 | ||
|
|
ab63e2c45b | ||
|
|
9d951f5e82 | ||
|
|
36ec8bb4d2 | ||
|
|
094f25e012 | ||
|
|
e9feed8aa1 | ||
|
|
2b67c02ba6 | ||
|
|
4c9ed35080 | ||
|
|
5f35000071 | ||
|
|
fb2d9bddbe | ||
|
|
e7d0135316 |
65
.travis.yml
Normal file
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
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.8)
|
||||||
project(indica VERSION 1.0.0 LANGUAGES CXX)
|
project(indica VERSION 1.0.0 LANGUAGES CXX)
|
||||||
option(INDICA_BUILD_TESTS OFF)
|
option(INDICA_BUILD_TESTS OFF)
|
||||||
|
option(SAMPLES "Build Samples" OFF)
|
||||||
|
option(DEMO "Build Demo" OFF)
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
@@ -16,6 +18,15 @@ target_include_directories(indica INTERFACE
|
|||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
||||||
target_link_libraries(indica INTERFACE Threads::Threads)
|
target_link_libraries(indica INTERFACE Threads::Threads)
|
||||||
|
|
||||||
|
|
||||||
|
if( DEMO )
|
||||||
|
add_subdirectory(demo)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if( SAMPLES )
|
||||||
|
add_subdirectory(samples)
|
||||||
|
endif()
|
||||||
|
|
||||||
configure_package_config_file(indicaConfig.cmake.in
|
configure_package_config_file(indicaConfig.cmake.in
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/indicaConfig.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/indicaConfig.cmake
|
||||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
|
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
|
||||||
|
|||||||
375
README.md
375
README.md
@@ -3,10 +3,13 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<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">
|
<a href="https://github.com/p-ranav/indicators/blob/master/LICENSE">
|
||||||
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
|
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
|
||||||
</a>
|
</a>
|
||||||
<img src="https://img.shields.io/badge/version-1.0-blue.svg?cacheSeconds=2592000" alt="version"/>
|
<img src="https://img.shields.io/badge/version-1.5-blue.svg?cacheSeconds=2592000" alt="version"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
@@ -15,56 +18,30 @@
|
|||||||
|
|
||||||
# Highlights
|
# Highlights
|
||||||
|
|
||||||
* Thread-safe `ProgressBar` and `ProgressSpinner` classes
|
* Thread-safe progress bars and spinners
|
||||||
* Header-only library. Grab a copy of `include/indicators`
|
* Header-only library. Grab a copy of `include/indicators`
|
||||||
* Source for the above GIF can be found [here](demo/demo.cpp)
|
* Source for the above GIF can be found [here](demo/demo.cpp)
|
||||||
* MIT License
|
* 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
|
# Progress bar
|
||||||
|
|
||||||
To introduce a progress bar in your application, include `indicators/progress_bar.hpp` and create a `ProgressBar` object.
|
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:
|
||||||
|
|
||||||
```cpp
|
|
||||||
#include <indicators/progress_bar.hpp>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
indicators::ProgressBar bar;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
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 ^^^^^^^^^^^^^^^
|
||||||
```
|
```
|
||||||
|
|
||||||
Each of these elements (and more) can be configured using the ProgressBar API. Here's an example configuration:
|
The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
|
||||||
|
|
||||||
```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;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Now that the bar is configured, let's update the state of the bar. The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
|
|
||||||
|
|
||||||
From application-level code, there are two ways in which you can update this progress:
|
From application-level code, there are two ways in which you can update this progress:
|
||||||
|
|
||||||
@@ -72,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%`.
|
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
|
```cpp
|
||||||
#include <indicators/progress_bar.hpp>
|
#include <indicators/progress_bar.hpp>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -87,7 +68,7 @@ int main() {
|
|||||||
bar.lead_bar_progress_with(">");
|
bar.lead_bar_progress_with(">");
|
||||||
bar.fill_bar_remainder_with(" ");
|
bar.fill_bar_remainder_with(" ");
|
||||||
bar.end_bar_with("]");
|
bar.end_bar_with("]");
|
||||||
bar.set_postfix_text("Getting started");
|
bar.set_postfix_text("Extracting Archive");
|
||||||
bar.set_foreground_color(indicators::Color::GREEN);
|
bar.set_foreground_color(indicators::Color::GREEN);
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
@@ -108,121 +89,117 @@ 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:
|
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
|
```cpp
|
||||||
|
#include <chrono>
|
||||||
#include <indicators/progress_bar.hpp>
|
#include <indicators/progress_bar.hpp>
|
||||||
#include <thread>
|
#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.end_bar_with(" ]");
|
||||||
|
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(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(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(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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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 `[{elapsed}<{remaining}]`:
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="img/time_meter.gif"/>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
```cpp
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
indicators::ProgressBar bar;
|
||||||
|
|
||||||
// Configure the bar
|
// Configure the bar
|
||||||
bar.set_bar_width(50);
|
bar.set_bar_width(50);
|
||||||
bar.start_bar_with("[");
|
bar.start_bar_with(" [");
|
||||||
bar.fill_bar_progress_with("=");
|
bar.fill_bar_progress_with("█");
|
||||||
bar.lead_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.fill_bar_remainder_with("-");
|
||||||
bar.end_bar_with("]");
|
bar.end_bar_with("]");
|
||||||
|
bar.set_prefix_text("Training Gaze Network 👀");
|
||||||
bar.set_foreground_color(indicators::Color::YELLOW);
|
bar.set_foreground_color(indicators::Color::YELLOW);
|
||||||
|
|
||||||
// As configured, the bar will look like this:
|
// Show time elapsed and remaining
|
||||||
//
|
bar.show_elapsed_time();
|
||||||
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
|
bar.show_remaining_time();
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
std::atomic<size_t> index{0};
|
// Update bar state
|
||||||
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) {
|
while (true) {
|
||||||
if (bar.is_completed()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bar.set_postfix_text(status_text[index % status_text.size()]);
|
|
||||||
bar.tick();
|
bar.tick();
|
||||||
index += 1;
|
if (bar.is_completed())
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
break;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
std::thread first_job(job);
|
// Show cursor
|
||||||
std::thread second_job(job);
|
std::cout << "\e[?25h";
|
||||||
std::thread third_job(job);
|
|
||||||
std::thread last_job(job);
|
|
||||||
|
|
||||||
first_job.join();
|
|
||||||
second_job.join();
|
|
||||||
third_job.join();
|
|
||||||
last_job.join();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
# Smooth 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.
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img height="70" src="img/block_progress_bar.gif"/>
|
<img src="img/block_progress_bar.gif"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -260,47 +237,121 @@ int main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# MultiProgress
|
||||||
|
|
||||||
|
`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/multi_progress.hpp>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// 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 ");
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# Progress Spinner
|
# Progress Spinner
|
||||||
|
|
||||||
To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object.
|
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:
|
||||||
|
|
||||||
```cpp
|
|
||||||
#include <indicators/progress_spinner.hpp>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
indicators::ProgressSpinner spinner;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Here's the general structure of a progress spinner:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
<prefix_text> <spinner> <progress_percentage>? <postfix_text>
|
{prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
|
||||||
```
|
|
||||||
|
|
||||||
Each of these elements (and more) can be configured using the ProgressSpinner API. Here's an example configuration:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
#include <indicators/progress_spinner.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({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
|
||||||
|
|
||||||
// Update spinner state
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
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)`.
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="img/progress_spinner.gif"/>
|
||||||
|
</p>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <indicators/progress_spinner.hpp>
|
#include <indicators/progress_spinner.hpp>
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
2
demo/CMakeLists.txt
Normal file
2
demo/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
add_executable(demo demo.cpp)
|
||||||
|
target_link_libraries(demo PRIVATE indica::indica)
|
||||||
BIN
img/multi_progress.gif
Normal file
BIN
img/multi_progress.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 185 KiB |
BIN
img/progress_bar_set_progress.gif
Normal file
BIN
img/progress_bar_set_progress.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
BIN
img/progress_bar_tick.gif
Normal file
BIN
img/progress_bar_tick.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 41 KiB |
BIN
img/progress_spinner.gif
Normal file
BIN
img/progress_spinner.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
BIN
img/time_meter.gif
Normal file
BIN
img/time_meter.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
@@ -25,15 +25,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#define NOMINMAX
|
||||||
#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 +78,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,10 +100,12 @@ 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() {
|
||||||
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,11 +129,56 @@ 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{indicators::Color::WHITE};
|
||||||
|
|
||||||
void _print_progress() {
|
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;
|
||||||
|
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(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};
|
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:
|
||||||
@@ -155,7 +214,7 @@ private:
|
|||||||
auto whole_width = std::floor(progress * _bar_width);
|
auto whole_width = std::floor(progress * _bar_width);
|
||||||
auto remainder_width = fmod((progress * _bar_width), 1.0f);
|
auto remainder_width = fmod((progress * _bar_width), 1.0f);
|
||||||
auto part_width = std::floor(remainder_width * lead_characters.size());
|
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)
|
if ((_bar_width - whole_width - 1) < 0)
|
||||||
_lead = "";
|
_lead = "";
|
||||||
for (size_t i = 0; i < whole_width; ++i)
|
for (size_t i = 0; i < whole_width; ++i)
|
||||||
@@ -168,6 +227,27 @@ 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 << "]";
|
||||||
|
} else {
|
||||||
|
if (_show_elapsed_time)
|
||||||
|
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";
|
||||||
@@ -175,7 +255,7 @@ private:
|
|||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
_completed = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
94
include/indicators/multi_progress.hpp
Normal file
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,9 +25,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#define NOMINMAX
|
||||||
#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 +92,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,10 +114,12 @@ 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() {
|
||||||
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,11 +143,56 @@ 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{indicators::Color::WHITE};
|
||||||
|
|
||||||
void _print_progress() {
|
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;
|
||||||
|
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(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};
|
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 +235,27 @@ 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 << "]";
|
||||||
|
} else {
|
||||||
|
if (_show_elapsed_time)
|
||||||
|
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";
|
||||||
@@ -182,7 +263,7 @@ private:
|
|||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
_completed = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -25,9 +25,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#define NOMINMAX
|
||||||
#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 +63,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 +80,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,10 +89,12 @@ 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() {
|
||||||
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,12 +119,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 +194,27 @@ 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 << "]";
|
||||||
|
} else {
|
||||||
|
if (_show_elapsed_time)
|
||||||
|
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";
|
||||||
|
|||||||
24
samples/CMakeLists.txt
Normal file
24
samples/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
add_executable(block_progress_bar block_progress_bar.cpp)
|
||||||
|
target_link_libraries(block_progress_bar PRIVATE indica::indica)
|
||||||
|
|
||||||
|
add_executable(multi_threaded_bar multi_threaded_bar.cpp)
|
||||||
|
target_link_libraries(multi_threaded_bar PRIVATE indica::indica)
|
||||||
|
|
||||||
|
add_executable(progress_bar_set_progress progress_bar_set_progress.cpp)
|
||||||
|
target_link_libraries(progress_bar_set_progress PRIVATE indica::indica)
|
||||||
|
|
||||||
|
add_executable(progress_bar_tick progress_bar_tick.cpp)
|
||||||
|
target_link_libraries(progress_bar_tick PRIVATE indica::indica)
|
||||||
|
|
||||||
|
add_executable(progress_spinner progress_spinner.cpp)
|
||||||
|
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)
|
||||||
@@ -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() {
|
||||||
|
|
||||||
|
|||||||
67
samples/multi_block_progress_bar.cpp
Normal file
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
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;
|
||||||
|
}
|
||||||
60
samples/multi_threaded_bar.cpp
Normal file
60
samples/multi_threaded_bar.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#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);
|
||||||
|
|
||||||
|
// 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"};
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::thread first_job(job);
|
||||||
|
std::thread second_job(job);
|
||||||
|
std::thread third_job(job);
|
||||||
|
std::thread last_job(job);
|
||||||
|
|
||||||
|
first_job.join();
|
||||||
|
second_job.join();
|
||||||
|
third_job.join();
|
||||||
|
last_job.join();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
52
samples/progress_bar_set_progress.cpp
Normal file
52
samples/progress_bar_set_progress.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#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.end_bar_with(" ]");
|
||||||
|
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(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(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(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;
|
||||||
|
}
|
||||||
27
samples/progress_bar_tick.cpp
Normal file
27
samples/progress_bar_tick.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#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_postfix_text("Getting started");
|
||||||
|
bar.set_foreground_color(indicators::Color::GREEN);
|
||||||
|
|
||||||
|
// Update bar state
|
||||||
|
while (true) {
|
||||||
|
bar.tick();
|
||||||
|
if (bar.is_completed())
|
||||||
|
break;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
38
samples/progress_spinner.cpp
Normal file
38
samples/progress_spinner.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <indicators/progress_spinner.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// Hide cursor
|
||||||
|
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({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
||||||
|
|
||||||
|
// Update spinner state
|
||||||
|
auto job = [&spinner]() {
|
||||||
|
while (true) {
|
||||||
|
if (spinner.is_completed()) {
|
||||||
|
spinner.set_foreground_color(indicators::Color::GREEN);
|
||||||
|
spinner.set_prefix_text("✔");
|
||||||
|
spinner.hide_spinner();
|
||||||
|
spinner.hide_percentage();
|
||||||
|
spinner.set_postfix_text("Authenticated!");
|
||||||
|
spinner.mark_as_completed();
|
||||||
|
break;
|
||||||
|
} else
|
||||||
|
spinner.tick();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::thread thread(job);
|
||||||
|
thread.join();
|
||||||
|
|
||||||
|
// Show cursor
|
||||||
|
std::cout << "\e[?25h";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
34
samples/time_meter.cpp
Normal file
34
samples/time_meter.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user