Compare commits
69 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2698afb34b | ||
|
|
764d796f87 | ||
|
|
5c54b69e55 | ||
|
|
186f1aafc7 | ||
|
|
cfb1593806 | ||
|
|
4a207b8223 | ||
|
|
07734c412b | ||
|
|
d543453c0c | ||
|
|
e279bbb7e7 | ||
|
|
840743f312 | ||
|
|
46c9983fab | ||
|
|
b1c16d8881 | ||
|
|
06da3a97f4 | ||
|
|
a2272eba8a | ||
|
|
46bccdcc1c | ||
|
|
3c8975aa34 | ||
|
|
9a4729f577 | ||
|
|
866c008492 | ||
|
|
dcb7c656cc | ||
|
|
cfcc9f3b4c | ||
|
|
91ac917bbc | ||
|
|
0939406db4 | ||
|
|
d4dea6db2a | ||
|
|
0dec563049 | ||
|
|
4f9796e904 | ||
|
|
a91d3b6ff7 | ||
|
|
1cba7fd321 | ||
|
|
b1f01d5e52 | ||
|
|
2291c8c39c | ||
|
|
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 |
@@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.8)
|
||||||
project(indica VERSION 1.0.0 LANGUAGES CXX)
|
project(indicators VERSION 1.6.0 LANGUAGES CXX DESCRIPTION "Activity Indicators for Modern C++")
|
||||||
option(INDICA_BUILD_TESTS OFF)
|
option(INDICATORS_BUILD_TESTS OFF)
|
||||||
option(SAMPLES "Build Samples" OFF)
|
option(SAMPLES "Build Samples" OFF)
|
||||||
option(DEMO "Build Demo" OFF)
|
option(DEMO "Build Demo" OFF)
|
||||||
|
|
||||||
@@ -9,14 +9,14 @@ include(GNUInstallDirs)
|
|||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
add_library(indica INTERFACE)
|
add_library(indicators INTERFACE)
|
||||||
add_library(indica::indica ALIAS indica)
|
add_library(indicators::indicators ALIAS indicators)
|
||||||
|
|
||||||
target_compile_features(indica INTERFACE cxx_std_11)
|
target_compile_features(indicators INTERFACE cxx_std_11)
|
||||||
target_include_directories(indica INTERFACE
|
target_include_directories(indicators INTERFACE
|
||||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
||||||
target_link_libraries(indica INTERFACE Threads::Threads)
|
target_link_libraries(indicators INTERFACE Threads::Threads)
|
||||||
|
|
||||||
|
|
||||||
if( DEMO )
|
if( DEMO )
|
||||||
@@ -27,17 +27,23 @@ if( SAMPLES )
|
|||||||
add_subdirectory(samples)
|
add_subdirectory(samples)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
configure_package_config_file(indicaConfig.cmake.in
|
configure_package_config_file(indicatorsConfig.cmake.in
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/indicaConfig.cmake
|
${CMAKE_CURRENT_BINARY_DIR}/indicatorsConfig.cmake
|
||||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
|
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
|
||||||
|
|
||||||
install(TARGETS indica EXPORT indicaTargets)
|
configure_file(indicators.pc.in
|
||||||
install(EXPORT indicaTargets
|
"${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/indicators.pc"
|
||||||
FILE indicaTargets.cmake
|
@ONLY)
|
||||||
NAMESPACE indica::
|
|
||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
|
install(TARGETS indicators EXPORT indicatorsTargets)
|
||||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/indicaConfig.cmake
|
install(EXPORT indicatorsTargets
|
||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
|
FILE indicatorsTargets.cmake
|
||||||
|
NAMESPACE indicators::
|
||||||
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
|
||||||
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/indicatorsConfig.cmake
|
||||||
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
|
||||||
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/indicators.pc
|
||||||
|
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/)
|
||||||
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/indicators
|
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/indicators
|
||||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||||
USE_SOURCE_PERMISSIONS
|
USE_SOURCE_PERMISSIONS
|
||||||
|
|||||||
353
README.md
@@ -6,10 +6,13 @@
|
|||||||
<a href="https://travis-ci.com/p-ranav/indicators">
|
<a href="https://travis-ci.com/p-ranav/indicators">
|
||||||
<img src="https://travis-ci.com/p-ranav/indicators.svg?branch=master" alt="ci status"/>
|
<img src="https://travis-ci.com/p-ranav/indicators.svg?branch=master" alt="ci status"/>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://www.codacy.com/manual/p-ranav/indicators?utm_source=github.com&utm_medium=referral&utm_content=p-ranav/indicators&utm_campaign=Badge_Grade">
|
||||||
|
<img src="https://api.codacy.com/project/badge/Grade/93401e73f250407cb32445afec4e3e99" alt="codacy"/>
|
||||||
|
</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.2-blue.svg?cacheSeconds=2592000" alt="version"/>
|
<img src="https://img.shields.io/badge/version-1.6-blue.svg?cacheSeconds=2592000" alt="version"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
@@ -23,33 +26,18 @@
|
|||||||
* 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;
|
|
||||||
|
|
||||||
// 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:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}] {postfix}
|
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}] {postfix}
|
||||||
@@ -64,23 +52,27 @@ 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>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
using namespace indicators;
|
||||||
|
ProgressBar bar{
|
||||||
// Configure the bar
|
option::BarWidth{50},
|
||||||
bar.set_bar_width(50);
|
option::Start{"["},
|
||||||
bar.start_bar_with("[");
|
option::Fill{"="},
|
||||||
bar.fill_bar_progress_with("=");
|
option::Lead{">"},
|
||||||
bar.lead_bar_progress_with(">");
|
option::Remainder{" "},
|
||||||
bar.fill_bar_remainder_with(" ");
|
option::End{"]"},
|
||||||
bar.end_bar_with("]");
|
option::PostfixText{"Extracting Archive"};
|
||||||
bar.set_postfix_text("Getting started");
|
option::ForegroundColor{Color::GREEN};
|
||||||
bar.set_foreground_color(indicators::Color::GREEN);
|
};
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -100,110 +92,60 @@ 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>
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
|
||||||
|
|
||||||
// Configure the bar
|
// Hide cursor
|
||||||
bar.set_bar_width(50);
|
std::cout << "\e[?25l";
|
||||||
bar.start_bar_with("[");
|
using namespace indicators;
|
||||||
bar.fill_bar_progress_with("=");
|
ProgressBar bar{
|
||||||
bar.lead_bar_progress_with(">");
|
option::BarWidth{50},
|
||||||
bar.fill_bar_remainder_with(" ");
|
option::Start{"["},
|
||||||
bar.end_bar_with("]");
|
option::Fill{"■"},
|
||||||
bar.set_postfix_text("Getting started");
|
option::Lead{"■"},
|
||||||
bar.set_foreground_color(indicators::Color::GREEN);
|
option::Remainder{"-"},
|
||||||
|
option::End{" ]"},
|
||||||
|
option::PostfixText{"Loading dependency 1/4"},
|
||||||
|
option::ForegroundColor{Color::CYAN}
|
||||||
|
};
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
|
|
||||||
bar.set_progress(10); // 10% done
|
bar.set_progress(10); // 10% done
|
||||||
|
|
||||||
// do some work
|
// do some work
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
||||||
|
|
||||||
|
bar.set_option(option::PostfixText{"Loading dependency 2/4"});
|
||||||
|
|
||||||
bar.set_progress(30); // 30% done
|
bar.set_progress(30); // 30% done
|
||||||
|
|
||||||
// do some more work
|
// do some more work
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
std::this_thread::sleep_for(std::chrono::milliseconds(700));
|
||||||
|
|
||||||
|
bar.set_option(option::PostfixText{"Loading dependency 3/4"});
|
||||||
|
|
||||||
bar.set_progress(65); // 65% done
|
bar.set_progress(65); // 65% done
|
||||||
|
|
||||||
// do final bit of work
|
// 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_option(option::PostfixText{"Loaded dependencies!"});
|
||||||
|
|
||||||
bar.set_progress(100); // all done
|
bar.set_progress(100); // all done
|
||||||
|
|
||||||
return 0;
|
bar.mark_as_completed();
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Multi-threaded Example
|
// Show cursor
|
||||||
|
std::cout << "\e[?25h";
|
||||||
```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);
|
|
||||||
|
|
||||||
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -223,21 +165,19 @@ All progress bars and spinners in `indicators` support showing time elapsed and
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
using namespace indicators;
|
||||||
|
indicators::ProgressBar bar{
|
||||||
// Configure the bar
|
option::BarWidth{50},
|
||||||
bar.set_bar_width(50);
|
option::Start{" ["},
|
||||||
bar.start_bar_with(" [");
|
option::Fill{"█"},
|
||||||
bar.fill_bar_progress_with("█");
|
option::Lead{"█"},
|
||||||
bar.lead_bar_progress_with("█");
|
option::Remainder{"-"},
|
||||||
bar.fill_bar_remainder_with("-");
|
option::End{"]"},
|
||||||
bar.end_bar_with("]");
|
option::PrefixText{"Training Gaze Network "},
|
||||||
bar.set_prefix_text("Training Gaze Network 👀");
|
option::ForegroundColor{Color::YELLOW}
|
||||||
bar.set_foreground_color(indicators::Color::YELLOW);
|
option::ShowElapsedTime{true};
|
||||||
|
option::ShowRemainingTime{true};
|
||||||
// Show time elapsed and remaining
|
};
|
||||||
bar.show_elapsed_time();
|
|
||||||
bar.show_remaining_time();
|
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -271,14 +211,14 @@ int main() {
|
|||||||
|
|
||||||
// Hide cursor
|
// Hide cursor
|
||||||
std::cout << "\e[?25l";
|
std::cout << "\e[?25l";
|
||||||
|
using namespace indicators;
|
||||||
|
|
||||||
indicators::BlockProgressBar bar;
|
BlockProgressBar bar{
|
||||||
|
option::BarWidth{80},
|
||||||
// Configure the bar
|
option::Start{"["},
|
||||||
bar.set_bar_width(80);
|
option::End{"]"},
|
||||||
bar.start_bar_with("[");
|
option::ForegroundColor{Color::WHITE}
|
||||||
bar.end_bar_with("]");
|
};
|
||||||
bar.set_foreground_color(indicators::Color::WHITE);
|
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
auto progress = 0.0f;
|
auto progress = 0.0f;
|
||||||
@@ -297,29 +237,114 @@ 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
|
```cpp
|
||||||
#include <indicators/progress_spinner.hpp>
|
#include <indicators/multi_progress.hpp>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressSpinner spinner;
|
using namespace indicators;
|
||||||
|
// Configure first progress bar
|
||||||
|
ProgressBar bar1{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::Start{"["},
|
||||||
|
option::Fill{"■"},
|
||||||
|
option::Lead{"■"},
|
||||||
|
option::Remainder{" "},
|
||||||
|
option::End{" ]"},
|
||||||
|
option::ForegroundColor{Color::YELLOW},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"Progress Bar #1 "}
|
||||||
|
};
|
||||||
|
|
||||||
// Configure the spinner
|
// Configure second progress bar
|
||||||
spinner.set_prefix_text(" ");
|
|
||||||
spinner.set_postfix_text("Checking credentials");
|
|
||||||
spinner.set_foreground_color(indicators::Color::YELLOW);
|
|
||||||
spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
|
||||||
|
|
||||||
// Update spinner state
|
ProgressBar bar1{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::Start{"["},
|
||||||
|
option::Fill{"="},
|
||||||
|
option::Lead{">"},
|
||||||
|
option::Remainder{" "},
|
||||||
|
option::End{" ]"},
|
||||||
|
option::ForegroundColor{Color::CYAN},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"Progress Bar #2 "}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configure third progress bar
|
||||||
|
indicators::ProgressBar bar3{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::Start{"["},
|
||||||
|
option::Fill{"#"},
|
||||||
|
option::Lead{"#"},
|
||||||
|
option::Remainder{" "},
|
||||||
|
option::End{" ]"},
|
||||||
|
option::ForegroundColor{Color::RED},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"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;
|
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}
|
{prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
|
||||||
@@ -327,26 +352,30 @@ 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)`.
|
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>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressSpinner spinner;
|
using namespace indicators;
|
||||||
|
indicators::ProgressSpinner spinner{
|
||||||
// Configure the spinner
|
option::PostfixText{"Checking credentials"},
|
||||||
spinner.set_postfix_text("Checking credentials");
|
option::ForegroundColor{Color::YELLOW},
|
||||||
spinner.set_foreground_color(indicators::Color::YELLOW);
|
option::SpinnerStates{std::vector<std::string>{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}}
|
||||||
spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
};
|
||||||
|
|
||||||
// Update spinner state
|
// Update spinner state
|
||||||
auto job = [&spinner]() {
|
auto job = [&spinner]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (spinner.is_completed()) {
|
if (spinner.is_completed()) {
|
||||||
spinner.set_foreground_color(indicators::Color::GREEN);
|
spinner.set_option(option::ForegroundColor{Color::GREEN});
|
||||||
spinner.set_prefix_text("✔");
|
spinner.set_option(option::PrefixText{"✔"});
|
||||||
spinner.hide_spinner();
|
spinner.set_option(option::ShowSpinner{false});
|
||||||
spinner.hide_percentage();
|
spinner.set_option(option::ShowPercentage{false});
|
||||||
spinner.set_postfix_text("Authenticated!");
|
spinner.set_option(option::PostfixText{"Authenticated!"});
|
||||||
spinner.mark_as_completed();
|
spinner.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
add_executable(demo demo.cpp)
|
add_executable(demo demo.cpp)
|
||||||
target_link_libraries(demo PRIVATE indica::indica)
|
target_link_libraries(demo PRIVATE indicators::indicators)
|
||||||
|
|||||||
227
demo/demo.cpp
@@ -6,19 +6,18 @@ int main() {
|
|||||||
|
|
||||||
// Hide cursor
|
// Hide cursor
|
||||||
std::cout << "\e[?25l";
|
std::cout << "\e[?25l";
|
||||||
|
using namespace indicators;
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// PROGRESS BAR 1
|
// PROGRESS BAR 1
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p{option::BarWidth{50},
|
||||||
p.set_bar_width(50);
|
option::Start{"["},
|
||||||
p.start_bar_with("[");
|
option::Fill{"■"},
|
||||||
p.fill_bar_progress_with("■");
|
option::Lead{"■"},
|
||||||
p.lead_bar_progress_with("■");
|
option::Remainder{" "},
|
||||||
p.fill_bar_remainder_with(" ");
|
option::End{" ]"},
|
||||||
p.end_bar_with(" ]");
|
option::ForegroundColor{indicators::Color::YELLOW}};
|
||||||
p.set_foreground_color(indicators::Color::YELLOW);
|
|
||||||
|
|
||||||
std::atomic<size_t> index{0};
|
std::atomic<size_t> index{0};
|
||||||
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
||||||
@@ -35,7 +34,7 @@ int main() {
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
p.set_postfix_text(status_text[index % status_text.size()]);
|
p.set_option(option::PostfixText{status_text[index % status_text.size()]});
|
||||||
p.set_progress(index * 10);
|
p.set_progress(index * 10);
|
||||||
index += 1;
|
index += 1;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||||
@@ -50,20 +49,20 @@ int main() {
|
|||||||
// PROGRESS BAR 2
|
// PROGRESS BAR 2
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p;
|
||||||
p.set_bar_width(40);
|
p.set_option(option::BarWidth{40});
|
||||||
p.set_prefix_text("Reading package list... ");
|
p.set_option(option::PrefixText{"Reading package list... "});
|
||||||
p.start_bar_with("");
|
p.set_option(option::Start{""});
|
||||||
p.fill_bar_progress_with("");
|
p.set_option(option::Fill{""});
|
||||||
p.lead_bar_progress_with("");
|
p.set_option(option::Lead{""});
|
||||||
p.fill_bar_remainder_with("");
|
p.set_option(option::Remainder{""});
|
||||||
p.end_bar_with("");
|
p.set_option(option::End{""});
|
||||||
p.set_foreground_color(indicators::Color::WHITE);
|
p.set_option(option::ForegroundColor{indicators::Color::WHITE});
|
||||||
p.hide_percentage();
|
p.set_option(option::ShowPercentage{false});
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p.set_prefix_text("Reading package list... " + std::to_string(p.current()) + "% ");
|
p.set_option(option::PrefixText{"Reading package list... " + std::to_string(p.current()) + "% "});
|
||||||
if (p.current() + 2 >= 100)
|
if (p.current() + 2 >= 100)
|
||||||
p.set_prefix_text("Reading package list... Done");
|
p.set_option(option::PrefixText{"Reading package list... Done"});
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed()) {
|
if (p.is_completed()) {
|
||||||
break;
|
break;
|
||||||
@@ -80,25 +79,25 @@ int main() {
|
|||||||
// PROGRESS BAR 3
|
// PROGRESS BAR 3
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p;
|
||||||
p.set_bar_width(50);
|
p.set_option(option::BarWidth{50});
|
||||||
p.start_bar_with("[");
|
p.set_option(option::Start{"["});
|
||||||
p.fill_bar_progress_with("=");
|
p.set_option(option::Fill{"="});
|
||||||
p.lead_bar_progress_with(">");
|
p.set_option(option::Lead{">"});
|
||||||
p.fill_bar_remainder_with(" ");
|
p.set_option(option::Remainder{" "});
|
||||||
p.end_bar_with("]");
|
p.set_option(option::End{"]"});
|
||||||
p.set_postfix_text("Getting started");
|
p.set_option(option::PostfixText{"Getting started"});
|
||||||
p.set_foreground_color(indicators::Color::GREEN);
|
p.set_option(option::ForegroundColor{indicators::Color::GREEN});
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p.current();
|
auto ticks = p.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p.set_postfix_text("Delaying the inevitable");
|
p.set_option(option::PostfixText{"Delaying the inevitable"});
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p.set_postfix_text("Crying quietly");
|
p.set_option(option::PostfixText{"Crying quietly"});
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p.set_postfix_text("Almost there");
|
p.set_option(option::PostfixText{"Almost there"});
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p.set_postfix_text("Done");
|
p.set_option(option::PostfixText{"Done"});
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
@@ -115,32 +114,31 @@ int main() {
|
|||||||
//
|
//
|
||||||
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||||
indicators::ProgressBar p4;
|
indicators::ProgressBar p4;
|
||||||
p4.set_bar_width(40);
|
p4.set_option(option::BarWidth{40});
|
||||||
p4.start_bar_with("");
|
p4.set_option(option::Start{""});
|
||||||
p4.fill_bar_progress_with("⠸");
|
p4.set_option(option::Fill{"⠸"});
|
||||||
p4.lead_bar_progress_with("");
|
p4.set_option(option::Lead{""});
|
||||||
p4.fill_bar_remainder_with(" ");
|
p4.set_option(option::Remainder{" "});
|
||||||
p4.end_bar_with("");
|
p4.set_option(option::End{""});
|
||||||
p4.set_foreground_color(indicators::Color::CYAN);
|
p4.set_option(option::ForegroundColor{indicators::Color::CYAN});
|
||||||
p4.set_postfix_text("Restoring system state");
|
p4.set_option(option::PostfixText{"Restoring system state"});
|
||||||
p4.hide_percentage();
|
p4.set_option(option::ShowPercentage{false});
|
||||||
std::atomic<size_t> index4{0};
|
std::atomic<size_t> index4{0};
|
||||||
auto job4 = [&p4, &index4, &lead_spinner]() {
|
auto job4 = [&p4, &index4, &lead_spinner]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
|
p4.set_option(option::PrefixText{"{ " + std::to_string(p4.current()) + "% } "});
|
||||||
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
|
p4.set_option(option::Lead{lead_spinner[index4 % lead_spinner.size()]});
|
||||||
index4 += 1;
|
index4 += 1;
|
||||||
if (p4.current() + 2 >= 100) {
|
if (p4.current() + 2 >= 100) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p4.set_foreground_color(indicators::Color::RED);
|
p4.set_option(option::ForegroundColor{indicators::Color::RED});
|
||||||
p4.set_prefix_text("{ ERROR }");
|
p4.set_option(option::PrefixText{"{ ERROR }"});
|
||||||
p4.start_bar_with("");
|
p4.set_option(option::Start{});
|
||||||
p4.fill_bar_progress_with("");
|
p4.set_option(option::Fill{});
|
||||||
p4.lead_bar_progress_with("");
|
p4.set_option(option::Lead{});
|
||||||
p4.fill_bar_remainder_with("");
|
p4.set_option(option::Remainder{});
|
||||||
p4.end_bar_with("");
|
p4.set_option(option::ShowPercentage{true});
|
||||||
p4.show_percentage();
|
p4.set_option(option::PostfixText{"Failed to restore system"});
|
||||||
p4.set_postfix_text("Failed to restore system");
|
|
||||||
p4.mark_as_completed();
|
p4.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -157,23 +155,24 @@ int main() {
|
|||||||
//
|
//
|
||||||
// GOING BACKWARDS
|
// GOING BACKWARDS
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p{
|
||||||
p.set_bar_width(50);
|
option::BarWidth {50},
|
||||||
p.start_bar_with("[");
|
option::Start{"["},
|
||||||
p.fill_bar_progress_with("■");
|
option::Fill{"■"},
|
||||||
p.lead_bar_progress_with("■");
|
option::Lead{"■"},
|
||||||
p.fill_bar_remainder_with("-");
|
option::Remainder{"-"},
|
||||||
p.end_bar_with("]");
|
option::End{"]"},
|
||||||
p.set_progress(100);
|
option::ForegroundColor{indicators::Color::WHITE},
|
||||||
p.set_foreground_color(indicators::Color::WHITE);
|
option::PostfixText{"Reverting system restore"}
|
||||||
p.set_postfix_text("Reverting system restore");
|
};
|
||||||
|
p.set_progress(100); // TODO backwards as an option?
|
||||||
std::atomic<size_t> progress{100};
|
std::atomic<size_t> progress{100};
|
||||||
auto job = [&p, &progress]() {
|
auto job = [&p, &progress]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
progress -= 1;
|
progress -= 1;
|
||||||
p.set_progress(progress);
|
p.set_progress(progress);
|
||||||
if (progress == 0) {
|
if (progress == 0) {
|
||||||
p.set_postfix_text("Revert complete!");
|
p.set_option(option::PostfixText{"Revert complete!"});
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -189,19 +188,21 @@ int main() {
|
|||||||
//
|
//
|
||||||
// PROGRESS BAR 5
|
// PROGRESS BAR 5
|
||||||
//
|
//
|
||||||
indicators::ProgressSpinner p;
|
indicators::ProgressSpinner p{
|
||||||
p.set_prefix_text("");
|
option::PrefixText{""},
|
||||||
p.set_postfix_text("Checking credentials");
|
option::PostfixText{"Checking credentials"},
|
||||||
p.set_foreground_color(indicators::Color::YELLOW);
|
option::ForegroundColor{indicators::Color::YELLOW},
|
||||||
p.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
option::SpinnerStates{std::vector<std::string>{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}}
|
||||||
|
};
|
||||||
|
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (p.is_completed()) {
|
if (p.is_completed()) {
|
||||||
p.set_foreground_color(indicators::Color::GREEN);
|
p.set_option(option::ForegroundColor{indicators::Color::GREEN});
|
||||||
p.set_prefix_text("✔");
|
p.set_option(option::PrefixText{"✔"});
|
||||||
p.hide_spinner();
|
p.set_option(option::ShowSpinner{false});
|
||||||
p.hide_percentage();
|
p.set_option(option::ShowPercentage{false});
|
||||||
p.set_postfix_text("Authenticated!");
|
p.set_option(option::PostfixText{"Authenticated!"});
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
@@ -218,70 +219,72 @@ int main() {
|
|||||||
//
|
//
|
||||||
// PROGRESS BAR 6
|
// PROGRESS BAR 6
|
||||||
//
|
//
|
||||||
indicators::ProgressSpinner p;
|
indicators::ProgressSpinner p{
|
||||||
p.set_prefix_text(" - ");
|
option::PrefixText{" - "},
|
||||||
p.set_postfix_text("Searching for the Moon");
|
option::PostfixText{"Searching for the Moon"},
|
||||||
p.set_foreground_color(indicators::Color::WHITE);
|
option::ForegroundColor{indicators::Color::WHITE},
|
||||||
p.set_spinner_states({"▖", "▘", "▝", "▗"});
|
option::ShowPercentage{false},
|
||||||
p.hide_percentage();
|
option::SpinnerStates{std::vector<std::string>{"▖", "▘", "▝", "▗"}}
|
||||||
|
};
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto current = p.current();
|
auto current = p.current();
|
||||||
if (current == 24) {
|
if (current == 24) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_option(option::PrefixText{" - ✔"});
|
||||||
p.hide_spinner();
|
p.set_option(option::ShowSpinner{false});
|
||||||
} else if (current == 25) {
|
} else if (current == 25) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.set_option(option::ShowSpinner{true});
|
||||||
p.set_prefix_text(" - ");
|
p.set_option(option::PrefixText{" - "});
|
||||||
p.set_postfix_text("Contacting Kerbal headquarters");
|
p.set_option(option::PostfixText{"Contacting Kerbal headquarters"});
|
||||||
} else if (current == 49) {
|
} else if (current == 49) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_option(option::PrefixText{" - ✔"});
|
||||||
p.hide_spinner();
|
p.set_option(option::ShowSpinner{false});
|
||||||
} else if (current == 50) {
|
} else if (current == 50) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.set_option(option::ShowSpinner{true});
|
||||||
p.set_prefix_text(" - ");
|
p.set_option(option::PrefixText{" - "});
|
||||||
p.set_postfix_text("Designing spaceship");
|
p.set_option(option::PostfixText{"Designing spaceship"});
|
||||||
} else if (current == 74) {
|
} else if (current == 74) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_option(option::PrefixText{" - ✔"});
|
||||||
p.hide_spinner();
|
p.set_option(option::ShowSpinner{false});
|
||||||
} else if (current == 75) {
|
} else if (current == 75) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.set_option(option::ShowSpinner{true});
|
||||||
p.set_prefix_text(" - ");
|
p.set_option(option::PrefixText{" - "});
|
||||||
p.set_postfix_text("Launching rocket");
|
p.set_option(option::PostfixText{"Launching rocket"});
|
||||||
} else if (current == 95) {
|
} else if (current == 95) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_option(option::PrefixText{" - ✔"});
|
||||||
p.hide_spinner();
|
p.set_option(option::ShowSpinner{false});
|
||||||
} else if (current == 99) {
|
} else if (current == 99) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
//
|
//
|
||||||
// NESTED PROGRESS BAR
|
// NESTED PROGRESS BAR
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p2;
|
indicators::ProgressBar p2{
|
||||||
p2.set_bar_width(30);
|
option::BarWidth{30},
|
||||||
p2.set_prefix_text(" - ");
|
option::PrefixText{" - "},
|
||||||
p2.start_bar_with("🌎");
|
option::Start{"🌎"},
|
||||||
p2.fill_bar_progress_with("·");
|
option::Fill{"·"},
|
||||||
p2.lead_bar_progress_with("🚀");
|
option::Lead{"🚀"},
|
||||||
p2.fill_bar_remainder_with(" ");
|
option::Remainder{" "},
|
||||||
p2.end_bar_with("🌑");
|
option::End{"🌑"},
|
||||||
p2.set_postfix_text("Achieved low-Earth orbit");
|
option::PostfixText{"Achieved low-Earth orbit"},
|
||||||
p2.set_foreground_color(indicators::Color::WHITE);
|
option::ForegroundColor{indicators::Color::WHITE}
|
||||||
|
};
|
||||||
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
||||||
std::atomic<int> ship_trail_index{0};
|
std::atomic<int> ship_trail_index{0};
|
||||||
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p2.current();
|
auto ticks = p2.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p2.set_postfix_text("Switching to trans-lunar trajectory");
|
p2.set_option(option::PostfixText{"Switching to trans-lunar trajectory"});
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p2.set_postfix_text("Transferred to Lunar lander");
|
p2.set_option(option::PostfixText{"Transferred to Lunar lander"});
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p2.set_postfix_text("Almost there");
|
p2.set_option(option::PostfixText{"Almost there"});
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p2.set_postfix_text("Landed on the Moon");
|
p2.set_option(option::PostfixText{"Landed on the Moon"});
|
||||||
p2.tick();
|
p2.tick();
|
||||||
ship_trail_index += 1;
|
ship_trail_index += 1;
|
||||||
if (p2.is_completed())
|
if (p2.is_completed())
|
||||||
@@ -291,7 +294,7 @@ int main() {
|
|||||||
};
|
};
|
||||||
std::thread thread2(job2);
|
std::thread thread2(job2);
|
||||||
thread2.join();
|
thread2.join();
|
||||||
p.set_postfix_text("Mission successful!");
|
p.set_option(indicators::option::PostfixText{"Mission successful!"});
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
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,70 +25,89 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <indicators/color.hpp>
|
||||||
|
#include <indicators/details/stream_helper.hpp>
|
||||||
|
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
|
||||||
#include <indicators/color.hpp>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include "setting.hpp"
|
||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
class BlockProgressBar {
|
class BlockProgressBar {
|
||||||
|
using Settings = std::tuple<
|
||||||
|
option::ForegroundColor,
|
||||||
|
option::BarWidth,
|
||||||
|
option::Start,
|
||||||
|
option::End,
|
||||||
|
option::PrefixText,
|
||||||
|
option::PostfixText,
|
||||||
|
option::ShowPercentage,
|
||||||
|
option::ShowElapsedTime,
|
||||||
|
option::ShowRemainingTime,
|
||||||
|
option::Completed,
|
||||||
|
option::SavedStartTime,
|
||||||
|
option::MaxPostfixTextLen>;
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
explicit BlockProgressBar(Args&&... args) : settings_(
|
||||||
_foreground_color = color;
|
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::start>(option::Start{"["}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::end>(option::End{"]"}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{""}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{""}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{true}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...)
|
||||||
|
) {}
|
||||||
|
|
||||||
|
template <typename T, details::ProgressBarOption id>
|
||||||
|
void set_option(details::Setting<T, id>&& setting){
|
||||||
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = std::move(setting).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bar_width(size_t bar_width) {
|
template <typename T, details::ProgressBarOption id>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
void set_option(const details::Setting<T, id>& setting){
|
||||||
_bar_width = bar_width;
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = setting.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_bar_with(const std::string &start) {
|
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_start = start;
|
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
|
||||||
|
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void end_bar_with(const std::string &end) {
|
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_end = end;
|
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
|
||||||
|
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
|
||||||
|
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_prefix_text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_postfix_text = text;
|
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_percentage() { _show_percentage = true; }
|
|
||||||
|
|
||||||
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::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress = value;
|
_progress = value;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -97,150 +116,107 @@ public:
|
|||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
size_t current() {
|
||||||
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
|
}
|
||||||
|
|
||||||
bool is_completed() const { return _completed; }
|
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||||
|
|
||||||
void mark_as_completed() {
|
void mark_as_completed() {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
template <details::ProgressBarOption id>
|
||||||
|
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
|
||||||
|
return details::get_value<id>(settings_).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <details::ProgressBarOption id>
|
||||||
|
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
|
||||||
|
return details::get_value<id>(settings_).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings settings_;
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
size_t _bar_width{100};
|
|
||||||
std::string _prefix_text{""};
|
|
||||||
std::string _start{"["};
|
|
||||||
std::string _fill{"█"};
|
|
||||||
std::string _lead{" "};
|
|
||||||
std::string _remainder{" "};
|
|
||||||
std::string _end{"]"};
|
|
||||||
std::string _postfix_text{""};
|
|
||||||
std::atomic<size_t> _max_postfix_text_length{0};
|
|
||||||
std::atomic<bool> _completed{false};
|
|
||||||
std::atomic<bool> _show_percentage{true};
|
|
||||||
std::atomic<bool> _show_elapsed_time{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::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
Color _foreground_color;
|
|
||||||
|
|
||||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||||
using namespace std;
|
std::atomic<bool> _multi_progress_mode{false};
|
||||||
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() {
|
void _save_start_time() {
|
||||||
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
|
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
|
||||||
|
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
||||||
|
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
|
||||||
|
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
|
||||||
_start_time_point = std::chrono::high_resolution_clock::now();
|
_start_time_point = std::chrono::high_resolution_clock::now();
|
||||||
_saved_start_time = true;
|
saved_start_time = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress(bool from_multi_progress = false) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
if (_multi_progress_mode && !from_multi_progress) {
|
||||||
|
if (_progress > 100.0) {
|
||||||
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
std::cout << termcolor::bold;
|
std::cout << termcolor::bold;
|
||||||
switch (_foreground_color) {
|
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
|
||||||
case Color::GREY:
|
std::cout << get_value<details::ProgressBarOption::prefix_text>();
|
||||||
std::cout << termcolor::grey;
|
std::cout << get_value<details::ProgressBarOption::start>();
|
||||||
break;
|
|
||||||
case Color::RED:
|
|
||||||
std::cout << termcolor::red;
|
|
||||||
break;
|
|
||||||
case Color::GREEN:
|
|
||||||
std::cout << termcolor::green;
|
|
||||||
break;
|
|
||||||
case Color::YELLOW:
|
|
||||||
std::cout << termcolor::yellow;
|
|
||||||
break;
|
|
||||||
case Color::BLUE:
|
|
||||||
std::cout << termcolor::blue;
|
|
||||||
break;
|
|
||||||
case Color::MAGENTA:
|
|
||||||
std::cout << termcolor::magenta;
|
|
||||||
break;
|
|
||||||
case Color::CYAN:
|
|
||||||
std::cout << termcolor::cyan;
|
|
||||||
break;
|
|
||||||
case Color::WHITE:
|
|
||||||
std::cout << termcolor::white;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::cout << _prefix_text;
|
|
||||||
std::cout << _start;
|
|
||||||
|
|
||||||
std::vector<std::string> lead_characters{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
details::BlockProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>()};
|
||||||
auto progress = std::min(1.0f, std::max(0.0f, _progress / 100.0f));
|
writer.write(_progress);
|
||||||
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[size_t(part_width)];
|
|
||||||
if ((_bar_width - whole_width - 1) < 0)
|
|
||||||
_lead = "";
|
|
||||||
for (size_t i = 0; i < whole_width; ++i)
|
|
||||||
std::cout << _fill;
|
|
||||||
std::cout << _lead;
|
|
||||||
for (size_t i = 0; i < (_bar_width - whole_width - 1); ++i)
|
|
||||||
std::cout << " ";
|
|
||||||
|
|
||||||
std::cout << _end;
|
std::cout << get_value<details::ProgressBarOption::end>();
|
||||||
if (_show_percentage) {
|
if (get_value<details::ProgressBarOption::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) {
|
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
_print_duration(std::cout, elapsed);
|
details::write_duration(std::cout, elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_show_remaining_time) {
|
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
||||||
if (_show_elapsed_time)
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
std::cout << "<";
|
std::cout << "<";
|
||||||
else
|
else
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
auto eta = std::chrono::nanoseconds(
|
auto eta = std::chrono::nanoseconds(
|
||||||
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
||||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||||
_print_duration(std::cout, remaining);
|
details::write_duration(std::cout, remaining);
|
||||||
std::cout << "]";
|
std::cout << "]";
|
||||||
|
} else {
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
|
std::cout << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_max_postfix_text_length == 0)
|
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
|
||||||
_max_postfix_text_length = 10;
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
|
||||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (get_value<details::ProgressBarOption::completed>() && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
139
include/indicators/details/stream_helper.hpp
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <indicators/color.hpp>
|
||||||
|
#include <indicators/termcolor.hpp>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <ostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace indicators {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
inline void set_stream_color(std::ostream &os, Color color) {
|
||||||
|
switch (color) {
|
||||||
|
case Color::GREY:
|
||||||
|
os << termcolor::grey;
|
||||||
|
break;
|
||||||
|
case Color::RED:
|
||||||
|
os << termcolor::red;
|
||||||
|
break;
|
||||||
|
case Color::GREEN:
|
||||||
|
os << termcolor::green;
|
||||||
|
break;
|
||||||
|
case Color::YELLOW:
|
||||||
|
os << termcolor::yellow;
|
||||||
|
break;
|
||||||
|
case Color::BLUE:
|
||||||
|
os << termcolor::blue;
|
||||||
|
break;
|
||||||
|
case Color::MAGENTA:
|
||||||
|
os << termcolor::magenta;
|
||||||
|
break;
|
||||||
|
case Color::CYAN:
|
||||||
|
os << termcolor::cyan;
|
||||||
|
break;
|
||||||
|
case Color::WHITE:
|
||||||
|
os << termcolor::white;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::ostream &write_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::chrono;
|
||||||
|
using days = duration<int, ratio<86400>>;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
class BlockProgressScaleWriter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BlockProgressScaleWriter(std::ostream& os, size_t bar_width)
|
||||||
|
: os(os)
|
||||||
|
, bar_width(bar_width)
|
||||||
|
{}
|
||||||
|
|
||||||
|
std::ostream &write(float progress) {
|
||||||
|
std::string fill_text{"█"};
|
||||||
|
std::vector<std::string> lead_characters{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
||||||
|
auto value = std::min(1.0f, std::max(0.0f, progress / 100.0f));
|
||||||
|
auto whole_width = std::floor(value * bar_width);
|
||||||
|
auto remainder_width = fmod((value * bar_width), 1.0f);
|
||||||
|
auto part_width = std::floor(remainder_width * lead_characters.size());
|
||||||
|
std::string lead_text = lead_characters[size_t(part_width)];
|
||||||
|
if ((bar_width - whole_width - 1) < 0)
|
||||||
|
lead_text = "";
|
||||||
|
for (size_t i = 0; i < whole_width; ++i)
|
||||||
|
os << fill_text;
|
||||||
|
os << lead_text;
|
||||||
|
for (size_t i = 0; i < (bar_width - whole_width - 1); ++i)
|
||||||
|
os << " ";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::ostream& os;
|
||||||
|
size_t bar_width = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ProgressScaleWriter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ProgressScaleWriter(std::ostream& os,
|
||||||
|
size_t bar_width,
|
||||||
|
const std::string& fill,
|
||||||
|
const std::string& lead,
|
||||||
|
const std::string& remainder)
|
||||||
|
: os(os)
|
||||||
|
, bar_width(bar_width)
|
||||||
|
, fill(fill)
|
||||||
|
, lead(lead)
|
||||||
|
, remainder(remainder)
|
||||||
|
{}
|
||||||
|
|
||||||
|
std::ostream &write(float progress) {
|
||||||
|
auto pos = static_cast<size_t>(progress * static_cast<float>(bar_width) / 100.0);
|
||||||
|
for (size_t i = 0; i < bar_width; ++i) {
|
||||||
|
if (i < pos)
|
||||||
|
os << fill;
|
||||||
|
else if (i == pos)
|
||||||
|
os << lead;
|
||||||
|
else
|
||||||
|
os << remainder;
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ostream& os;
|
||||||
|
size_t bar_width = 0;
|
||||||
|
std::string fill;
|
||||||
|
std::string lead;
|
||||||
|
std::string remainder;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
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::lock_guard<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,230 +25,218 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <indicators/details/stream_helper.hpp>
|
||||||
|
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <indicators/color.hpp>
|
#include <indicators/color.hpp>
|
||||||
|
#include <indicators/setting.hpp>
|
||||||
|
#include <tuple>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
|
using Settings = std::tuple<
|
||||||
|
option::BarWidth,
|
||||||
|
option::PrefixText,
|
||||||
|
option::PostfixText,
|
||||||
|
option::Start,
|
||||||
|
option::End,
|
||||||
|
option::Fill,
|
||||||
|
option::Lead,
|
||||||
|
option::Remainder,
|
||||||
|
option::MaxPostfixTextLen,
|
||||||
|
option::Completed,
|
||||||
|
option::ShowPercentage,
|
||||||
|
option::ShowElapsedTime,
|
||||||
|
option::ShowRemainingTime,
|
||||||
|
option::SavedStartTime,
|
||||||
|
option::ForegroundColor
|
||||||
|
>;
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
explicit ProgressBar(Args&&... args) :
|
||||||
_foreground_color = color;
|
settings_(
|
||||||
|
details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::start>(option::Start{"["}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::end>(option::End{"]"}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::fill>(option::Fill{"="}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::lead>(option::Lead{">"}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::remainder>(option::Remainder{" "}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{false} ,std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...)
|
||||||
|
)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T, details::ProgressBarOption id>
|
||||||
|
void set_option(details::Setting<T, id>&& setting){
|
||||||
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = std::move(setting).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bar_width(size_t bar_width) {
|
template <typename T, details::ProgressBarOption id>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
void set_option(const details::Setting<T, id>& setting){
|
||||||
_bar_width = bar_width;
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = setting.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_bar_with(const std::string &start) {
|
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_start = start;
|
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
|
||||||
}
|
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
|
||||||
void fill_bar_progress_with(const std::string &fill) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_fill = fill;
|
|
||||||
}
|
|
||||||
|
|
||||||
void lead_bar_progress_with(const std::string &lead) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_lead = lead;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fill_bar_remainder_with(const std::string &remainder) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_remainder = remainder;
|
|
||||||
}
|
|
||||||
|
|
||||||
void end_bar_with(const std::string &end) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_end = end;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_prefix_text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_postfix_text = text;
|
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_percentage() { _show_percentage = true; }
|
|
||||||
|
|
||||||
void hide_percentage() { _show_percentage = false; }
|
|
||||||
|
|
||||||
void show_elapsed_time() { _show_elapsed_time = true; }
|
|
||||||
|
|
||||||
void hide_elapsed_time() { _show_elapsed_time = false; }
|
|
||||||
|
|
||||||
void show_remaining_time() { _show_remaining_time = true; }
|
|
||||||
|
|
||||||
void hide_remaining_time() { _show_remaining_time = false; }
|
|
||||||
|
|
||||||
void set_progress(float value) {
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
|
||||||
_progress = value;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
|
||||||
|
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
|
||||||
|
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_progress(float newProgress){
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lck(_mutex);
|
||||||
|
_progress = newProgress;
|
||||||
|
}
|
||||||
|
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
size_t current() {
|
||||||
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
|
}
|
||||||
|
|
||||||
bool is_completed() const { return _completed; }
|
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||||
|
|
||||||
void mark_as_completed() {
|
void mark_as_completed() {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _progress{0.0};
|
|
||||||
size_t _bar_width{100};
|
template <details::ProgressBarOption id>
|
||||||
std::string _prefix_text{""};
|
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
|
||||||
std::string _start{"["};
|
return details::get_value<id>(settings_).value;
|
||||||
std::string _fill{"="};
|
}
|
||||||
std::string _lead{">"};
|
|
||||||
std::string _remainder{" "};
|
template <details::ProgressBarOption id>
|
||||||
std::string _end{"]"};
|
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
|
||||||
std::string _postfix_text{""};
|
return details::get_value<id>(settings_).value;
|
||||||
std::atomic<size_t> _max_postfix_text_length{0};
|
}
|
||||||
std::atomic<bool> _completed{false};
|
|
||||||
std::atomic<bool> _show_percentage{true};
|
float _progress{0};
|
||||||
std::atomic<bool> _show_elapsed_time{false};
|
Settings settings_;
|
||||||
std::atomic<bool> _show_remaining_time{false};
|
std::chrono::nanoseconds _elapsed;
|
||||||
std::atomic<bool> _saved_start_time{false};
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
Color _foreground_color;
|
|
||||||
|
|
||||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||||
using namespace std;
|
std::atomic<bool> _multi_progress_mode{false};
|
||||||
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() {
|
void _save_start_time() {
|
||||||
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
|
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
|
||||||
|
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
||||||
|
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
|
||||||
|
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
|
||||||
_start_time_point = std::chrono::high_resolution_clock::now();
|
_start_time_point = std::chrono::high_resolution_clock::now();
|
||||||
_saved_start_time = true;
|
saved_start_time = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress(bool from_multi_progress = false) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
if (_multi_progress_mode && !from_multi_progress) {
|
||||||
|
if (_progress > 100.0) {
|
||||||
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
if (!get_value<details::ProgressBarOption::completed>())
|
||||||
|
_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
std::cout << termcolor::bold;
|
std::cout << termcolor::bold;
|
||||||
switch (_foreground_color) {
|
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
|
||||||
case Color::GREY:
|
std::cout << get_value<details::ProgressBarOption::prefix_text>();
|
||||||
std::cout << termcolor::grey;
|
|
||||||
break;
|
std::cout << get_value<details::ProgressBarOption::start>();
|
||||||
case Color::RED:
|
|
||||||
std::cout << termcolor::red;
|
details::ProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>(),
|
||||||
break;
|
get_value<details::ProgressBarOption::fill>(),
|
||||||
case Color::GREEN:
|
get_value<details::ProgressBarOption::lead>(),
|
||||||
std::cout << termcolor::green;
|
get_value<details::ProgressBarOption::remainder>()};
|
||||||
break;
|
writer.write(_progress);
|
||||||
case Color::YELLOW:
|
|
||||||
std::cout << termcolor::yellow;
|
std::cout << get_value<details::ProgressBarOption::end>();
|
||||||
break;
|
|
||||||
case Color::BLUE:
|
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||||
std::cout << termcolor::blue;
|
|
||||||
break;
|
|
||||||
case Color::MAGENTA:
|
|
||||||
std::cout << termcolor::magenta;
|
|
||||||
break;
|
|
||||||
case Color::CYAN:
|
|
||||||
std::cout << termcolor::cyan;
|
|
||||||
break;
|
|
||||||
case Color::WHITE:
|
|
||||||
std::cout << termcolor::white;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::cout << _prefix_text;
|
|
||||||
std::cout << _start;
|
|
||||||
auto pos = static_cast<size_t>(_progress * static_cast<float>(_bar_width) / 100.0);
|
|
||||||
for (size_t i = 0; i < _bar_width; ++i) {
|
|
||||||
if (i < pos)
|
|
||||||
std::cout << _fill;
|
|
||||||
else if (i == pos)
|
|
||||||
std::cout << _lead;
|
|
||||||
else
|
|
||||||
std::cout << _remainder;
|
|
||||||
}
|
|
||||||
std::cout << _end;
|
|
||||||
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) {
|
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
_print_duration(std::cout, elapsed);
|
details::write_duration(std::cout, _elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_show_remaining_time) {
|
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
||||||
if (_show_elapsed_time)
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
std::cout << "<";
|
std::cout << "<";
|
||||||
else
|
else
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
auto eta = std::chrono::nanoseconds(
|
auto eta = std::chrono::nanoseconds(
|
||||||
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
_progress > 0 ? static_cast<long long>(_elapsed.count() * 100 / _progress) : 0);
|
||||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
auto remaining = eta > _elapsed ? (eta - _elapsed) : (_elapsed - eta);
|
||||||
_print_duration(std::cout, remaining);
|
details::write_duration(std::cout, remaining);
|
||||||
std::cout << "]";
|
std::cout << "]";
|
||||||
|
} else {
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
|
std::cout << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_max_postfix_text_length == 0)
|
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
|
||||||
_max_postfix_text_length = 10;
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
|
||||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (get_value<details::ProgressBarOption::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,59 +25,90 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <indicators/details/stream_helper.hpp>
|
||||||
|
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <indicators/color.hpp>
|
#include <indicators/color.hpp>
|
||||||
|
#include <indicators/setting.hpp>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
class ProgressSpinner {
|
class ProgressSpinner {
|
||||||
|
using Settings = std::tuple<
|
||||||
|
option::ForegroundColor,
|
||||||
|
option::PrefixText,
|
||||||
|
option::PostfixText,
|
||||||
|
option::ShowPercentage,
|
||||||
|
option::ShowElapsedTime,
|
||||||
|
option::ShowRemainingTime,
|
||||||
|
option::ShowSpinner,
|
||||||
|
option::SavedStartTime,
|
||||||
|
option::Completed,
|
||||||
|
option::MaxPostfixTextLen,
|
||||||
|
option::SpinnerStates
|
||||||
|
>;
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
explicit ProgressSpinner(Args&&... args) : settings_(
|
||||||
_foreground_color = color;
|
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{true}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::spinner_show>(option::ShowSpinner{true}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
|
||||||
|
details::get<details::ProgressBarOption::spinner_states>(option::SpinnerStates{std::vector<std::string>{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}}, std::forward<Args>(args)...)
|
||||||
|
){}
|
||||||
|
|
||||||
|
template <typename T, details::ProgressBarOption id>
|
||||||
|
void set_option(details::Setting<T, id>&& setting){
|
||||||
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = std::move(setting).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_prefix_text(const std::string &text) {
|
template <typename T, details::ProgressBarOption id>
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
void set_option(const details::Setting<T, id>& setting){
|
||||||
_prefix_text = text;
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = setting.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_postfix_text(const std::string &text) {
|
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_postfix_text = text;
|
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
|
||||||
if (_postfix_text.length() > _max_postfix_text_length)
|
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
_max_postfix_text_length = _postfix_text.length();
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_percentage() { _show_percentage = true; }
|
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
void hide_percentage() { _show_percentage = false; }
|
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
|
||||||
|
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
|
||||||
void show_elapsed_time() { _show_elapsed_time = true; }
|
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||||
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
|
||||||
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 hide_spinner() { _show_spinner = false; }
|
|
||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress = value;
|
_progress = value;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
@@ -86,138 +117,95 @@ public:
|
|||||||
|
|
||||||
void tick() {
|
void tick() {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
_progress += 1;
|
_progress += 1;
|
||||||
}
|
}
|
||||||
_save_start_time();
|
_save_start_time();
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t current() { return std::min(static_cast<size_t>(_progress), size_t(100)); }
|
size_t current() {
|
||||||
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||||
|
}
|
||||||
|
|
||||||
bool is_completed() const { return _completed; }
|
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||||
|
|
||||||
void mark_as_completed() {
|
void mark_as_completed() {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_spinner_states(const std::vector<std::string> &states) {
|
private:
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
Settings settings_;
|
||||||
_states = states;
|
float _progress{0.0};
|
||||||
|
size_t _index{0};
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||||
|
std::mutex _mutex;
|
||||||
|
|
||||||
|
template <details::ProgressBarOption id>
|
||||||
|
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
|
||||||
|
return details::get_value<id>(settings_).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
template <details::ProgressBarOption id>
|
||||||
float _progress{0.0};
|
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
|
||||||
std::string _prefix_text{""};
|
return details::get_value<id>(settings_).value;
|
||||||
size_t _index{0};
|
}
|
||||||
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
|
||||||
std::string _postfix_text{""};
|
|
||||||
std::atomic<size_t> _max_postfix_text_length{0};
|
|
||||||
std::atomic<bool> _completed{false};
|
|
||||||
std::atomic<bool> _show_percentage{true};
|
|
||||||
std::atomic<bool> _show_elapsed_time{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::mutex _mutex;
|
|
||||||
Color _foreground_color;
|
|
||||||
|
|
||||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::chrono;
|
|
||||||
typedef duration<int, ratio<86400>> days;
|
|
||||||
char fill = os.fill();
|
|
||||||
os.fill('0');
|
|
||||||
auto d = duration_cast<days>(ns);
|
|
||||||
ns -= d;
|
|
||||||
auto h = duration_cast<hours>(ns);
|
|
||||||
ns -= h;
|
|
||||||
auto m = duration_cast<minutes>(ns);
|
|
||||||
ns -= m;
|
|
||||||
auto s = duration_cast<seconds>(ns);
|
|
||||||
if (d.count() > 0)
|
|
||||||
os << setw(2) << d.count() << "d:";
|
|
||||||
if (h.count() > 0)
|
|
||||||
os << setw(2) << h.count() << "h:";
|
|
||||||
os << setw(2) << m.count() << "m:" << setw(2) << s.count() << 's';
|
|
||||||
os.fill(fill);
|
|
||||||
return os;
|
|
||||||
};
|
|
||||||
|
|
||||||
void _save_start_time() {
|
void _save_start_time() {
|
||||||
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
|
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
|
||||||
|
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
|
||||||
|
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
||||||
|
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
|
||||||
_start_time_point = std::chrono::high_resolution_clock::now();
|
_start_time_point = std::chrono::high_resolution_clock::now();
|
||||||
_saved_start_time = true;
|
saved_start_time = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress() {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock{_mutex};
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||||
|
|
||||||
std::cout << termcolor::bold;
|
std::cout << termcolor::bold;
|
||||||
switch (_foreground_color) {
|
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
|
||||||
case Color::GREY:
|
std::cout << get_value<details::ProgressBarOption::prefix_text>();
|
||||||
std::cout << termcolor::grey;
|
if (get_value<details::ProgressBarOption::spinner_show>())
|
||||||
break;
|
std::cout << get_value<details::ProgressBarOption::spinner_states>()[_index % get_value<details::ProgressBarOption::spinner_states>().size()];
|
||||||
case Color::RED:
|
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||||
std::cout << termcolor::red;
|
|
||||||
break;
|
|
||||||
case Color::GREEN:
|
|
||||||
std::cout << termcolor::green;
|
|
||||||
break;
|
|
||||||
case Color::YELLOW:
|
|
||||||
std::cout << termcolor::yellow;
|
|
||||||
break;
|
|
||||||
case Color::BLUE:
|
|
||||||
std::cout << termcolor::blue;
|
|
||||||
break;
|
|
||||||
case Color::MAGENTA:
|
|
||||||
std::cout << termcolor::magenta;
|
|
||||||
break;
|
|
||||||
case Color::CYAN:
|
|
||||||
std::cout << termcolor::cyan;
|
|
||||||
break;
|
|
||||||
case Color::WHITE:
|
|
||||||
std::cout << termcolor::white;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::cout << _prefix_text;
|
|
||||||
if (_show_spinner)
|
|
||||||
std::cout << _states[_index % _states.size()];
|
|
||||||
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) {
|
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
_print_duration(std::cout, elapsed);
|
details::write_duration(std::cout, elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_show_remaining_time) {
|
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
||||||
if (_show_elapsed_time)
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
std::cout << "<";
|
std::cout << "<";
|
||||||
else
|
else
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
auto eta = std::chrono::nanoseconds(
|
auto eta = std::chrono::nanoseconds(
|
||||||
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
||||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||||
_print_duration(std::cout, remaining);
|
details::write_duration(std::cout, remaining);
|
||||||
std::cout << "]";
|
std::cout << "]";
|
||||||
|
} else {
|
||||||
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
||||||
|
std::cout << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_max_postfix_text_length == 0)
|
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
|
||||||
_max_postfix_text_length = 10;
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
|
||||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
_index += 1;
|
_index += 1;
|
||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (get_value<details::ProgressBarOption::completed>())
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
222
include/indicators/setting.hpp
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
/*
|
||||||
|
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 Dawid Pilarski <dawid.pilarski@panicsoftware.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 <cstddef>
|
||||||
|
#include <indicators/color.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
namespace indicators{
|
||||||
|
|
||||||
|
namespace details{
|
||||||
|
|
||||||
|
template <bool condition>
|
||||||
|
struct if_else;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct if_else<true>{
|
||||||
|
using type = std::true_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct if_else<false>{
|
||||||
|
using type = std::false_type ;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <bool condition, typename True, typename False>
|
||||||
|
struct if_else_type;
|
||||||
|
|
||||||
|
template <typename True, typename False>
|
||||||
|
struct if_else_type<true, True, False>{
|
||||||
|
using type = True;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename True, typename False>
|
||||||
|
struct if_else_type<false, True, False>{
|
||||||
|
using type = False;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Ops>
|
||||||
|
struct conjuction;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct conjuction<> : std::true_type {};
|
||||||
|
|
||||||
|
template <typename Op, typename... TailOps>
|
||||||
|
struct conjuction<Op, TailOps...> : if_else_type<!Op::value, std::false_type, conjuction<TailOps...>>::type {};
|
||||||
|
|
||||||
|
template <typename... Ops>
|
||||||
|
struct disjunction;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct disjunction<> : std::false_type {};
|
||||||
|
|
||||||
|
template <typename Op, typename... TailOps>
|
||||||
|
struct disjunction<Op, TailOps...> : if_else_type<Op::value, std::true_type, disjunction<TailOps...>>::type {};
|
||||||
|
|
||||||
|
enum class ProgressBarOption{
|
||||||
|
bar_width=0,
|
||||||
|
prefix_text,
|
||||||
|
postfix_text,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
fill,
|
||||||
|
lead,
|
||||||
|
remainder,
|
||||||
|
max_postfix_text_len,
|
||||||
|
completed,
|
||||||
|
show_percentage,
|
||||||
|
show_elapsed_time,
|
||||||
|
show_remaining_time,
|
||||||
|
saved_start_time,
|
||||||
|
foreground_color,
|
||||||
|
spinner_show,
|
||||||
|
spinner_states
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, ProgressBarOption Id>
|
||||||
|
struct Setting{
|
||||||
|
template <typename... Args, typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type>
|
||||||
|
explicit Setting(Args&&... args) : value(std::forward<Args>(args)...){}
|
||||||
|
Setting(const Setting&) = default;
|
||||||
|
Setting(Setting&&) = default;
|
||||||
|
|
||||||
|
static constexpr auto id = Id;
|
||||||
|
using type = T;
|
||||||
|
|
||||||
|
T value{};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_setting : std::false_type{};
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename T>
|
||||||
|
struct is_setting<Setting<T, Id>> : std::true_type{};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
struct are_settings : if_else<conjuction<is_setting<Args>...>::value>::type {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct are_settings<> : std::true_type{};
|
||||||
|
|
||||||
|
template<typename Setting, typename Tuple>
|
||||||
|
struct is_setting_from_tuple;
|
||||||
|
|
||||||
|
template<typename Setting>
|
||||||
|
struct is_setting_from_tuple<Setting, std::tuple<>> : std::true_type {};
|
||||||
|
|
||||||
|
template <typename Setting, typename... TupleTypes>
|
||||||
|
struct is_setting_from_tuple<Setting, std::tuple<TupleTypes...>> :
|
||||||
|
if_else<disjunction<std::is_same<Setting, TupleTypes>...>::value>::type {};
|
||||||
|
|
||||||
|
template <typename Tuple, typename... Settings>
|
||||||
|
struct are_settings_from_tuple : if_else<conjuction<is_setting_from_tuple<Settings, Tuple>...>::value>::type {};
|
||||||
|
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
struct always_true{
|
||||||
|
static constexpr auto value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<ProgressBarOption Id, typename Default>
|
||||||
|
Default&& get_impl(Default&& def){
|
||||||
|
return std::forward<Default>(def);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
|
||||||
|
auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
|
||||||
|
(std::decay<T>::type::id == Id),
|
||||||
|
decltype(std::forward<T>(first))>
|
||||||
|
::type{
|
||||||
|
return std::forward<T>(first);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
|
||||||
|
auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
|
||||||
|
(std::decay<T>::type::id != Id),
|
||||||
|
decltype(get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...))>::type{
|
||||||
|
return get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename Default, typename... Args, typename = typename std::enable_if<are_settings<Args...>::value, void>::type>
|
||||||
|
auto get(Default&& def, Args&&... args) -> decltype(details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...)){
|
||||||
|
return details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
using StringSetting = Setting<std::string, Id>;
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
using IntegerSetting = Setting<std::size_t, Id>;
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
using BooleanSetting = Setting<bool, Id>;
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename Tuple, std::size_t counter =0>
|
||||||
|
struct option_idx;
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename T, typename... Settings, std::size_t counter>
|
||||||
|
struct option_idx<Id, std::tuple<T, Settings...>, counter> : if_else_type<(Id == T::id),
|
||||||
|
std::integral_constant<std::size_t, counter>,
|
||||||
|
option_idx<Id, std::tuple<Settings...>, counter+1>>::type{};
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, std::size_t counter>
|
||||||
|
struct option_idx<Id, std::tuple<>, counter>{
|
||||||
|
static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found");
|
||||||
|
};
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename Settings>
|
||||||
|
auto get_value(Settings&& settings) -> decltype((std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::declval<Settings&&>()))){
|
||||||
|
return std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::forward<Settings>(settings));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace option{
|
||||||
|
using BarWidth = details::IntegerSetting<details::ProgressBarOption::bar_width>;
|
||||||
|
using PrefixText = details::StringSetting<details::ProgressBarOption::prefix_text>;
|
||||||
|
using PostfixText = details::StringSetting<details::ProgressBarOption::postfix_text>;
|
||||||
|
using Start = details::StringSetting<details::ProgressBarOption::start>;
|
||||||
|
using End = details::StringSetting<details::ProgressBarOption::end>;
|
||||||
|
using Fill = details::StringSetting<details::ProgressBarOption::fill>;
|
||||||
|
using Lead = details::StringSetting<details::ProgressBarOption::lead>;
|
||||||
|
using Remainder = details::StringSetting<details::ProgressBarOption::remainder>;
|
||||||
|
using MaxPostfixTextLen = details::IntegerSetting<details::ProgressBarOption::max_postfix_text_len>;
|
||||||
|
using Completed = details::BooleanSetting<details::ProgressBarOption::completed>;
|
||||||
|
using ShowPercentage = details::BooleanSetting<details::ProgressBarOption::show_percentage>;
|
||||||
|
using ShowElapsedTime = details::BooleanSetting<details::ProgressBarOption::show_elapsed_time>;
|
||||||
|
using ShowRemainingTime = details::BooleanSetting<details::ProgressBarOption::show_remaining_time>;
|
||||||
|
using SavedStartTime = details::BooleanSetting<details::ProgressBarOption::saved_start_time>;
|
||||||
|
using ForegroundColor = details::Setting<Color, details::ProgressBarOption::foreground_color>;
|
||||||
|
using ShowSpinner = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
|
||||||
|
using SpinnerStates = details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
@PACKAGE_INIT@
|
|
||||||
|
|
||||||
include(CMakeFindDependencyMacro)
|
|
||||||
|
|
||||||
find_dependency(Threads)
|
|
||||||
|
|
||||||
if (NOT TARGET indica::indica)
|
|
||||||
include(${CMAKE_CURRENT_LIST_DIR}/indicaTargets.cmake)
|
|
||||||
endif ()
|
|
||||||
7
indicators.pc.in
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
prefix=@CMAKE_INSTALL_PREFIX@
|
||||||
|
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
|
||||||
|
|
||||||
|
Name: indicators
|
||||||
|
Description: Activity Indicators for Modern C++
|
||||||
|
Version: @PROJECT_VERSION@
|
||||||
|
Cflags: -I${includedir}
|
||||||
9
indicatorsConfig.cmake.in
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
@PACKAGE_INIT@
|
||||||
|
|
||||||
|
include(CMakeFindDependencyMacro)
|
||||||
|
|
||||||
|
find_dependency(Threads)
|
||||||
|
|
||||||
|
if (NOT TARGET indicators::indicators)
|
||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/indicatorsTargets.cmake)
|
||||||
|
endif ()
|
||||||
@@ -1,18 +1,24 @@
|
|||||||
|
|
||||||
add_executable(block_progress_bar block_progress_bar.cpp)
|
add_executable(block_progress_bar block_progress_bar.cpp)
|
||||||
target_link_libraries(block_progress_bar PRIVATE indica::indica)
|
target_link_libraries(block_progress_bar PRIVATE indicators::indicators)
|
||||||
|
|
||||||
add_executable(multi_threaded_bar multi_threaded_bar.cpp)
|
add_executable(multi_threaded_bar multi_threaded_bar.cpp)
|
||||||
target_link_libraries(multi_threaded_bar PRIVATE indica::indica)
|
target_link_libraries(multi_threaded_bar PRIVATE indicators::indicators)
|
||||||
|
|
||||||
add_executable(progress_bar_set_progress progress_bar_set_progress.cpp)
|
add_executable(progress_bar_set_progress progress_bar_set_progress.cpp)
|
||||||
target_link_libraries(progress_bar_set_progress PRIVATE indica::indica)
|
target_link_libraries(progress_bar_set_progress PRIVATE indicators::indicators)
|
||||||
|
|
||||||
add_executable(progress_bar_tick progress_bar_tick.cpp)
|
add_executable(progress_bar_tick progress_bar_tick.cpp)
|
||||||
target_link_libraries(progress_bar_tick PRIVATE indica::indica)
|
target_link_libraries(progress_bar_tick PRIVATE indicators::indicators)
|
||||||
|
|
||||||
add_executable(progress_spinner progress_spinner.cpp)
|
add_executable(progress_spinner progress_spinner.cpp)
|
||||||
target_link_libraries(progress_spinner PRIVATE indica::indica)
|
target_link_libraries(progress_spinner PRIVATE indicators::indicators)
|
||||||
|
|
||||||
add_executable(time_meter time_meter.cpp)
|
add_executable(time_meter time_meter.cpp)
|
||||||
target_link_libraries(time_meter PRIVATE indica::indica)
|
target_link_libraries(time_meter PRIVATE indicators::indicators)
|
||||||
|
|
||||||
|
add_executable(multi_progress_bar multi_progress_bar.cpp)
|
||||||
|
target_link_libraries(multi_progress_bar PRIVATE indicators::indicators)
|
||||||
|
|
||||||
|
add_executable(multi_block_progress_bar multi_block_progress_bar.cpp)
|
||||||
|
target_link_libraries(multi_block_progress_bar PRIVATE indicators::indicators)
|
||||||
|
|||||||
@@ -7,13 +7,9 @@ int main() {
|
|||||||
// Hide cursor
|
// Hide cursor
|
||||||
std::cout << "\e[?25l";
|
std::cout << "\e[?25l";
|
||||||
|
|
||||||
indicators::BlockProgressBar bar;
|
indicators::BlockProgressBar bar{
|
||||||
|
indicators::option::BarWidth {80}
|
||||||
// Configure the bar
|
};
|
||||||
bar.set_bar_width(80);
|
|
||||||
bar.start_bar_with("[");
|
|
||||||
bar.end_bar_with("]");
|
|
||||||
bar.set_foreground_color(indicators::Color::WHITE);
|
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
auto progress = 0.0f;
|
auto progress = 0.0f;
|
||||||
|
|||||||
70
samples/multi_block_progress_bar.cpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include <indicators/block_progress_bar.hpp>
|
||||||
|
#include <indicators/multi_progress.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
using namespace indicators;
|
||||||
|
BlockProgressBar bar1{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::ForegroundColor{Color::YELLOW},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"Progress Bar #1 "}
|
||||||
|
};
|
||||||
|
|
||||||
|
BlockProgressBar bar2{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::ForegroundColor{Color::CYAN},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"Progress Bar #2 "}
|
||||||
|
};
|
||||||
|
|
||||||
|
BlockProgressBar bar3{
|
||||||
|
option::BarWidth{50},
|
||||||
|
option::ForegroundColor{Color::RED},
|
||||||
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"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;
|
||||||
|
}
|
||||||
85
samples/multi_progress_bar.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <indicators/multi_progress.hpp>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
indicators::ProgressBar bar1{
|
||||||
|
indicators::option::BarWidth{50},
|
||||||
|
indicators::option::Start{"["},
|
||||||
|
indicators::option::Fill{"■"},
|
||||||
|
indicators::option::Lead{"■"},
|
||||||
|
indicators::option::Remainder{" "},
|
||||||
|
indicators::option::End{" ]"},
|
||||||
|
indicators::option::ForegroundColor{indicators::Color::YELLOW},
|
||||||
|
indicators::option::ShowElapsedTime{true},
|
||||||
|
indicators::option::ShowRemainingTime{true},
|
||||||
|
indicators::option::PrefixText{"Progress Bar #1 "}
|
||||||
|
};
|
||||||
|
|
||||||
|
indicators::ProgressBar bar2{
|
||||||
|
indicators::option::BarWidth{50},
|
||||||
|
indicators::option::Start{"["},
|
||||||
|
indicators::option::Fill{"="},
|
||||||
|
indicators::option::Lead{">"},
|
||||||
|
indicators::option::Remainder{" "},
|
||||||
|
indicators::option::End{" ]"},
|
||||||
|
indicators::option::ForegroundColor{indicators::Color::CYAN},
|
||||||
|
indicators::option::ShowElapsedTime{true},
|
||||||
|
indicators::option::ShowRemainingTime{true},
|
||||||
|
indicators::option::PrefixText{"Progress Bar #2 "}
|
||||||
|
};
|
||||||
|
|
||||||
|
indicators::ProgressBar bar3{
|
||||||
|
indicators::option::BarWidth{50},
|
||||||
|
indicators::option::Start{"["},
|
||||||
|
indicators::option::Fill{"#"},
|
||||||
|
indicators::option::Lead{"#"},
|
||||||
|
indicators::option::Remainder{" "},
|
||||||
|
indicators::option::End{" ]"},
|
||||||
|
indicators::option::ForegroundColor{indicators::Color::CYAN},
|
||||||
|
indicators::option::ShowElapsedTime{true},
|
||||||
|
indicators::option::ShowRemainingTime{true},
|
||||||
|
indicators::option::PrefixText{"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,14 +3,15 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
indicators::ProgressBar bar;
|
indicators::ProgressBar bar{
|
||||||
bar.set_bar_width(50);
|
indicators::option::BarWidth{50},
|
||||||
bar.start_bar_with("[");
|
indicators::option::Start{"["},
|
||||||
bar.fill_bar_progress_with("■");
|
indicators::option::Fill{"■"},
|
||||||
bar.lead_bar_progress_with("■");
|
indicators::option::Lead{"■"},
|
||||||
bar.fill_bar_remainder_with("-");
|
indicators::option::Remainder{"-"},
|
||||||
bar.end_bar_with("]");
|
indicators::option::End{" ]"},
|
||||||
bar.set_foreground_color(indicators::Color::YELLOW);
|
indicators::option::ForegroundColor{indicators::Color::YELLOW},
|
||||||
|
};
|
||||||
|
|
||||||
// As configured, the bar will look like this:
|
// As configured, the bar will look like this:
|
||||||
//
|
//
|
||||||
@@ -39,7 +40,7 @@ int main() {
|
|||||||
if (bar.is_completed()) {
|
if (bar.is_completed()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bar.set_postfix_text(status_text[index % status_text.size()]);
|
bar.set_option(indicators::option::PostfixText{status_text[index % status_text.size()]});
|
||||||
bar.tick();
|
bar.tick();
|
||||||
index += 1;
|
index += 1;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
|||||||
@@ -3,36 +3,49 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
|
||||||
|
|
||||||
// Configure the bar
|
// Hide cursor
|
||||||
bar.set_bar_width(50);
|
std::cout << "\e[?25l";
|
||||||
bar.start_bar_with("[");
|
|
||||||
bar.fill_bar_progress_with("=");
|
indicators::ProgressBar bar{
|
||||||
bar.lead_bar_progress_with(">");
|
indicators::option::BarWidth{50},
|
||||||
bar.fill_bar_remainder_with(" ");
|
indicators::option::Start{"["},
|
||||||
bar.end_bar_with("]");
|
indicators::option::Fill{"■"},
|
||||||
bar.set_postfix_text("Getting started");
|
indicators::option::Lead{"■"},
|
||||||
bar.set_foreground_color(indicators::Color::GREEN);
|
indicators::option::Remainder{"-"},
|
||||||
|
indicators::option::End{" ]"},
|
||||||
|
indicators::option::PostfixText{"Loading dependency 1/4"},
|
||||||
|
indicators::option::ForegroundColor{indicators::Color::CYAN},
|
||||||
|
};
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
|
|
||||||
bar.set_progress(10); // 10% done
|
bar.set_progress(10); // 10% done
|
||||||
|
|
||||||
// do some work
|
// do some work
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
||||||
|
|
||||||
|
bar.set_option(indicators::option::PostfixText{"Loading dependency 2/4"});
|
||||||
|
|
||||||
bar.set_progress(30); // 30% done
|
bar.set_progress(30); // 30% done
|
||||||
|
|
||||||
// do some more work
|
// do some more work
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
std::this_thread::sleep_for(std::chrono::milliseconds(700));
|
||||||
|
|
||||||
|
bar.set_option(indicators::option::PostfixText{"Loading dependency 3/4"});
|
||||||
|
|
||||||
bar.set_progress(65); // 65% done
|
bar.set_progress(65); // 65% done
|
||||||
|
|
||||||
// do final bit of work
|
// 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_option(indicators::option::PostfixText{"Loaded dependencies!"});
|
||||||
|
|
||||||
bar.set_progress(100); // all done
|
bar.set_progress(100); // all done
|
||||||
|
|
||||||
|
bar.mark_as_completed();
|
||||||
|
|
||||||
|
// Show cursor
|
||||||
|
std::cout << "\e[?25h";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,16 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
indicators::ProgressBar bar{
|
||||||
|
indicators::option::BarWidth{50},
|
||||||
// Configure the bar
|
indicators::option::Start{"["},
|
||||||
bar.set_bar_width(50);
|
indicators::option::Fill{"="},
|
||||||
bar.start_bar_with("[");
|
indicators::option::Lead{">"},
|
||||||
bar.fill_bar_progress_with("=");
|
indicators::option::Remainder{" "},
|
||||||
bar.lead_bar_progress_with(">");
|
indicators::option::End{" ]"},
|
||||||
bar.fill_bar_remainder_with(" ");
|
indicators::option::PostfixText{"Getting started"},
|
||||||
bar.end_bar_with("]");
|
indicators::option::ForegroundColor{indicators::Color::GREEN},
|
||||||
bar.set_postfix_text("Getting started");
|
};
|
||||||
bar.set_foreground_color(indicators::Color::GREEN);
|
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
@@ -5,22 +5,21 @@ int main() {
|
|||||||
// Hide cursor
|
// Hide cursor
|
||||||
std::cout << "\e[?25l";
|
std::cout << "\e[?25l";
|
||||||
|
|
||||||
indicators::ProgressSpinner spinner;
|
indicators::ProgressSpinner spinner{
|
||||||
|
indicators::option::PostfixText{"Checking credentials"},
|
||||||
// Configure the spinner
|
indicators::option::ForegroundColor{indicators::Color::YELLOW},
|
||||||
spinner.set_postfix_text("Checking credentials");
|
indicators::option::SpinnerStates{std::vector<std::string>{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}},
|
||||||
spinner.set_foreground_color(indicators::Color::YELLOW);
|
};
|
||||||
spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
|
||||||
|
|
||||||
// Update spinner state
|
// Update spinner state
|
||||||
auto job = [&spinner]() {
|
auto job = [&spinner]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (spinner.is_completed()) {
|
if (spinner.is_completed()) {
|
||||||
spinner.set_foreground_color(indicators::Color::GREEN);
|
spinner.set_option(indicators::option::ForegroundColor{indicators::Color::GREEN});
|
||||||
spinner.set_prefix_text("✔");
|
spinner.set_option(indicators::option::PrefixText{"✔"});
|
||||||
spinner.hide_spinner();
|
spinner.set_option(indicators::option::ShowSpinner{false});
|
||||||
spinner.hide_percentage();
|
spinner.set_option(indicators::option::ShowPercentage{false});
|
||||||
spinner.set_postfix_text("Authenticated!");
|
spinner.set_option(indicators::option::PostfixText{"Authenticated!"});
|
||||||
spinner.mark_as_completed();
|
spinner.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
|
|||||||
@@ -3,21 +3,18 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
indicators::ProgressBar bar;
|
indicators::ProgressBar bar{
|
||||||
|
indicators::option::BarWidth{50},
|
||||||
// Configure the bar
|
indicators::option::Start{" ["},
|
||||||
bar.set_bar_width(50);
|
indicators::option::Fill{"█"},
|
||||||
bar.start_bar_with(" [");
|
indicators::option::Lead{"█"},
|
||||||
bar.fill_bar_progress_with("█");
|
indicators::option::Remainder{"-"},
|
||||||
bar.lead_bar_progress_with("█");
|
indicators::option::End{"]"},
|
||||||
bar.fill_bar_remainder_with("-");
|
indicators::option::PostfixText{"Training Gaze Network "},
|
||||||
bar.end_bar_with("]");
|
indicators::option::ForegroundColor{indicators::Color::YELLOW},
|
||||||
bar.set_prefix_text("Training Gaze Network 👀");
|
indicators::option::ShowElapsedTime{true},
|
||||||
bar.set_foreground_color(indicators::Color::YELLOW);
|
indicators::option::ShowRemainingTime{true},
|
||||||
|
};
|
||||||
// Show time elapsed and remaining
|
|
||||||
bar.show_elapsed_time();
|
|
||||||
bar.show_remaining_time();
|
|
||||||
|
|
||||||
// Update bar state
|
// Update bar state
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||