From d543453c0cb50f967dc59a79bc1402ab5efae5b7 Mon Sep 17 00:00:00 2001 From: Dawid Pilarski Date: Mon, 10 Feb 2020 20:54:20 +0100 Subject: [PATCH] code changes done --- include/indicators/block_progress_bar.hpp | 157 ++++++++++++---------- samples/block_progress_bar.cpp | 10 +- 2 files changed, 92 insertions(+), 75 deletions(-) diff --git a/include/indicators/block_progress_bar.hpp b/include/indicators/block_progress_bar.hpp index 1f0a05d..5d8b78e 100644 --- a/include/indicators/block_progress_bar.hpp +++ b/include/indicators/block_progress_bar.hpp @@ -37,56 +37,74 @@ SOFTWARE. #include #include #include +#include #include +#include "setting.hpp" namespace indicators { 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: - void set_foreground_color(Color color) { - std::lock_guard lock{_mutex}; - _foreground_color = color; + template ::type...>::value, void*>::type = nullptr> + BlockProgressBar(Args&&... args) : settings_( + details::get(option::ForegroundColor{Color::WHITE}, std::forward(args)...), + details::get(option::BarWidth{100}, std::forward(args)...), + details::get(option::Start{"["}, std::forward(args)...), + details::get(option::End{"]"}, std::forward(args)...), + details::get(option::PrefixText{""}, std::forward(args)...), + details::get(option::PostfixText{""}, std::forward(args)...), + details::get(option::ShowPercentage{true}, std::forward(args)...), + details::get(option::ShowElapsedTime{false}, std::forward(args)...), + details::get(option::ShowRemainingTime{false}, std::forward(args)...), + details::get(option::Completed{false}, std::forward(args)...), + details::get(option::SavedStartTime{false}, std::forward(args)...), + details::get(option::MaxPostfixTextLen{0}, std::forward(args)...) + ) {} + + template + void set_option(details::Setting&& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; } - void set_bar_width(size_t bar_width) { - std::lock_guard lock{_mutex}; - _bar_width = bar_width; + template + void set_option(const details::Setting& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = setting.value; } - void start_bar_with(const std::string &start) { - std::lock_guard lock{_mutex}; - _start = start; + void set_option(const details::Setting& setting){ + std::lock_guard lock(_mutex); + get_value() = setting.value; + if(setting.value.length() > get_value()){ + get_value() = setting.value.length(); + } } - void end_bar_with(const std::string &end) { - std::lock_guard lock{_mutex}; - _end = end; + void set_option(details::Setting&& setting){ + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; + auto& new_value = get_value(); + if(new_value.length() > get_value()){ + get_value() = new_value.length(); + } } - void set_prefix_text(const std::string &text) { - std::lock_guard lock{_mutex}; - _prefix_text = text; - } - - void set_postfix_text(const std::string &text) { - std::lock_guard 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::lock_guard lock{_mutex}; @@ -110,44 +128,47 @@ public: return std::min(static_cast(_progress), size_t(100)); } - bool is_completed() const { return _completed; } + bool is_completed() const { return get_value(); } void mark_as_completed() { - _completed = true; + get_value() = true; _print_progress(); } private: + + template + auto get_value() -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + template + auto get_value() const -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + Settings settings_; float _progress{0.0}; - size_t _bar_width{100}; - std::string _prefix_text{""}; - std::string _start{"["}; - std::string _end{"]"}; - std::string _postfix_text{""}; - std::atomic _max_postfix_text_length{0}; - std::atomic _completed{false}; - std::atomic _show_percentage{true}; - std::atomic _show_elapsed_time{false}; - std::atomic _show_remaining_time{false}; - std::atomic _saved_start_time{false}; std::chrono::time_point _start_time_point; std::mutex _mutex; - Color _foreground_color{indicators::Color::WHITE}; template friend class MultiProgress; std::atomic _multi_progress_mode{false}; void _save_start_time() { - if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) { + auto& show_elapsed_time = get_value(); + auto& saved_start_time = get_value(); + auto& show_remaining_time = get_value(); + if ((show_elapsed_time || show_remaining_time) && !saved_start_time) { _start_time_point = std::chrono::high_resolution_clock::now(); - _saved_start_time = true; + saved_start_time = true; } } void _print_progress(bool from_multi_progress = false) { if (_multi_progress_mode && !from_multi_progress) { if (_progress > 100.0) { - _completed = true; + get_value() = true; } return; } @@ -156,25 +177,25 @@ private: auto elapsed = std::chrono::duration_cast(now - _start_time_point); std::cout << termcolor::bold; - details::set_stream_color(std::cout, _foreground_color); - std::cout << _prefix_text; - std::cout << _start; + details::set_stream_color(std::cout, get_value()); + std::cout << get_value(); + std::cout << get_value(); - details::BlockProgressScaleWriter writer{std::cout, _bar_width}; + details::BlockProgressScaleWriter writer{std::cout, get_value()}; writer.write(_progress); - std::cout << _end; - if (_show_percentage) { + std::cout << get_value(); + if (get_value()) { std::cout << " " << std::min(static_cast(_progress), size_t(100)) << "%"; } - if (_show_elapsed_time) { + if (get_value()) { std::cout << " ["; details::write_duration(std::cout, elapsed); } - if (_show_remaining_time) { - if (_show_elapsed_time) + if (get_value()) { + if (get_value()) std::cout << "<"; else std::cout << " ["; @@ -184,18 +205,18 @@ private: details::write_duration(std::cout, remaining); std::cout << "]"; } else { - if (_show_elapsed_time) + if (get_value()) std::cout << "]"; } - if (_max_postfix_text_length == 0) - _max_postfix_text_length = 10; - std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; + if (get_value() == 0) + get_value() = 10; + std::cout << " " << get_value() << std::string(get_value(), ' ') << "\r"; std::cout.flush(); if (_progress > 100.0) { - _completed = true; + get_value() = true; } - if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress + if (get_value() && !from_multi_progress) // Don't std::endl if calling from MultiProgress std::cout << termcolor::reset << std::endl; } }; diff --git a/samples/block_progress_bar.cpp b/samples/block_progress_bar.cpp index d0cbcfd..2d6b1fd 100644 --- a/samples/block_progress_bar.cpp +++ b/samples/block_progress_bar.cpp @@ -7,13 +7,9 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - indicators::BlockProgressBar bar; - - // Configure the bar - bar.set_bar_width(80); - bar.start_bar_with("["); - bar.end_bar_with("]"); - bar.set_foreground_color(indicators::Color::WHITE); + indicators::BlockProgressBar bar{ + indicators::option::BarWidth {80} + }; // Update bar state auto progress = 0.0f;