diff --git a/include/indicators/multi_progress.hpp b/include/indicators/multi_progress.hpp new file mode 100644 index 0000000..0533f8e --- /dev/null +++ b/include/indicators/multi_progress.hpp @@ -0,0 +1,76 @@ +/* +Activity Indicators for Modern C++ +https://github.com/p-ranav/indicators + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2019 Pranav Srinivas Kumar . + +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 +#define NOMINMAX +#include +#include +#include + +namespace indicators { + +template +class MultiProgress { +public: + void add_progress_bar(ProgressBar& bar) { + _bars.push_back(bar); + bar._multi_progress_mode = true; + } + + template + typename std::enable_if<(index < count), void>::type + set_progress(float value) { + _bars[index].get().set_progress(value); + _print_progress(); + } + + template + typename std::enable_if<(index < count), void>::type + tick() { + _bars[index].get().tick(); + _print_progress(); + } + + template + typename std::enable_if<(index < count), bool>::type + is_completed() const { return _bars[index].get().is_completed(); } + +private: + std::vector> _bars; + + void _print_progress() { + for (size_t i = 0; i < count; ++i) + std::cout << "\x1b[A"; + for (auto& bar: _bars) { + bar.get()._print_progress(); + std::cout << "\n"; + } + } + +}; + +} + diff --git a/include/indicators/progress_bar.hpp b/include/indicators/progress_bar.hpp index 43eb2e5..81294fa 100644 --- a/include/indicators/progress_bar.hpp +++ b/include/indicators/progress_bar.hpp @@ -36,6 +36,7 @@ SOFTWARE. #include #include #include +#include namespace indicators { @@ -147,6 +148,10 @@ private: std::mutex _mutex; Color _foreground_color; + template + friend class MultiProgress; + std::atomic _multi_progress_mode{false}; + std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) { using namespace std; using namespace std::chrono; @@ -176,7 +181,7 @@ private: } } - void _print_progress() { + void _print_progress() { std::unique_lock lock{_mutex}; auto now = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast(now - _start_time_point);