mirror of https://github.com/mamba-org/mamba.git
102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
/*
|
|
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(size_t 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 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
|