mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-13 02:28:53 +08:00
Added sample showing MultiProgress with BlockProgressBar
This commit is contained in:
@@ -131,7 +131,10 @@ private:
|
|||||||
std::atomic<bool> _saved_start_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;
|
Color _foreground_color{indicators::Color::WHITE};
|
||||||
|
|
||||||
|
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||||
|
std::atomic<bool> _multi_progress_mode{false};
|
||||||
|
|
||||||
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -162,7 +165,13 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress() {
|
void _print_progress(bool from_multi_progress = false) {
|
||||||
|
if (_multi_progress_mode && !from_multi_progress) {
|
||||||
|
if (_progress > 100.0) {
|
||||||
|
_completed = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<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);
|
||||||
@@ -243,7 +252,7 @@ private:
|
|||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
_completed = true;
|
||||||
}
|
}
|
||||||
if (_completed)
|
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ SOFTWARE.
|
|||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
template <typename Indicator, size_t count> class MultiProgress {
|
template <typename Indicator, size_t count> class MultiProgress {
|
||||||
public:
|
public:
|
||||||
void add_progress_bar(Indicator &bar) {
|
void add_progress_bar(Indicator &bar) {
|
||||||
_bars.push_back(bar);
|
_bars.push_back(bar);
|
||||||
|
|||||||
@@ -179,8 +179,8 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _print_progress(bool force = false) {
|
void _print_progress(bool from_multi_progress = false) {
|
||||||
if (_multi_progress_mode && !force) {
|
if (_multi_progress_mode && !from_multi_progress) {
|
||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
_completed = true;
|
||||||
}
|
}
|
||||||
@@ -260,7 +260,7 @@ private:
|
|||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
_completed = true;
|
||||||
}
|
}
|
||||||
if (_completed && !force)
|
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,5 +17,8 @@ target_link_libraries(progress_spinner PRIVATE indica::indica)
|
|||||||
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 indica::indica)
|
||||||
|
|
||||||
add_executable(multi_progress multi_progress.cpp)
|
add_executable(multi_progress_bar multi_progress_bar.cpp)
|
||||||
target_link_libraries(multi_progress PRIVATE indica::indica)
|
target_link_libraries(multi_progress_bar PRIVATE indica::indica)
|
||||||
|
|
||||||
|
add_executable(multi_block_progress_bar multi_block_progress_bar.cpp)
|
||||||
|
target_link_libraries(multi_block_progress_bar PRIVATE indica::indica)
|
||||||
|
|||||||
70
samples/multi_block_progress_bar.cpp
Normal file
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() {
|
||||||
|
|
||||||
|
indicators::BlockProgressBar bar1;
|
||||||
|
bar1.set_bar_width(50);
|
||||||
|
bar1.set_foreground_color(indicators::Color::YELLOW);
|
||||||
|
bar1.show_elapsed_time();
|
||||||
|
bar1.show_remaining_time();
|
||||||
|
bar1.set_prefix_text("Progress Bar #1 ");
|
||||||
|
|
||||||
|
indicators::BlockProgressBar bar2;
|
||||||
|
bar2.set_bar_width(50);
|
||||||
|
bar2.set_foreground_color(indicators::Color::CYAN);
|
||||||
|
bar2.show_elapsed_time();
|
||||||
|
bar2.show_remaining_time();
|
||||||
|
bar2.set_prefix_text("Progress Bar #2 ");
|
||||||
|
|
||||||
|
indicators::BlockProgressBar bar3;
|
||||||
|
bar3.set_bar_width(50);
|
||||||
|
bar3.set_foreground_color(indicators::Color::RED);
|
||||||
|
bar3.show_elapsed_time();
|
||||||
|
bar3.show_remaining_time();
|
||||||
|
bar3.set_prefix_text("Progress Bar #3 ");
|
||||||
|
|
||||||
|
indicators::MultiProgress<indicators::BlockProgressBar, 3> bars;
|
||||||
|
bars.add_progress_bar(bar1);
|
||||||
|
bars.add_progress_bar(bar2);
|
||||||
|
bars.add_progress_bar(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;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <indicators/progress_bar.hpp>
|
|
||||||
#include <indicators/multi_progress.hpp>
|
#include <indicators/multi_progress.hpp>
|
||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Reference in New Issue
Block a user