mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-12 01:18:52 +08:00
Added dynamic postfix text sample based on #56
This commit is contained in:
@@ -8,8 +8,10 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <indicators/setting.hpp>
|
#include <indicators/setting.hpp>
|
||||||
|
#include <indicators/terminal_size.hpp>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -160,6 +162,12 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t get_prefix_length() {
|
||||||
|
std::stringstream os;
|
||||||
|
os << get_value<details::ProgressBarOption::prefix_text>();
|
||||||
|
return os.str().size();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void print_progress(bool from_multi_progress = false) {
|
void print_progress(bool from_multi_progress = false) {
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <indicators/color.hpp>
|
#include <indicators/color.hpp>
|
||||||
#include <indicators/setting.hpp>
|
#include <indicators/setting.hpp>
|
||||||
|
#include <indicators/terminal_size.hpp>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|||||||
@@ -44,3 +44,6 @@ target_link_libraries(max_progress PRIVATE indicators::indicators)
|
|||||||
add_executable(indeterminate_progress_bar indeterminate_progress_bar.cpp)
|
add_executable(indeterminate_progress_bar indeterminate_progress_bar.cpp)
|
||||||
target_link_libraries(indeterminate_progress_bar PRIVATE indicators::indicators)
|
target_link_libraries(indeterminate_progress_bar PRIVATE indicators::indicators)
|
||||||
|
|
||||||
|
add_executable(dynamic_postfix_text dynamic_postfix_text.cpp)
|
||||||
|
target_link_libraries(dynamic_postfix_text PRIVATE indicators::indicators)
|
||||||
|
|
||||||
|
|||||||
46
samples/dynamic_postfix_text.cpp
Normal file
46
samples/dynamic_postfix_text.cpp
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <indicators/multi_progress.hpp>
|
||||||
|
#include <indicators/terminal_size.hpp>
|
||||||
|
using namespace indicators;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::cout << "Terminal width: " << terminal_size().second << "\n";
|
||||||
|
|
||||||
|
// prepare progress bar
|
||||||
|
auto prepare_p = [](ProgressBar *p, const std::string &prefix){
|
||||||
|
p->set_option(option::PrefixText{prefix});
|
||||||
|
p->set_option(option::Start{""});
|
||||||
|
p->set_option(option::Fill{""});
|
||||||
|
p->set_option(option::Lead{""});
|
||||||
|
p->set_option(option::Remainder{""});
|
||||||
|
p->set_option(option::End{""});
|
||||||
|
p->set_option(option::BarWidth{0});
|
||||||
|
};
|
||||||
|
|
||||||
|
ProgressBar p1, p2;
|
||||||
|
|
||||||
|
prepare_p(&p1, "Progress #1");
|
||||||
|
prepare_p(&p2, "Progress #2");
|
||||||
|
|
||||||
|
MultiProgress<ProgressBar, 2> mp(p1, p2);
|
||||||
|
|
||||||
|
std::string some_text[] = {"foo", "bar", "independence", "beta", "alfa"};
|
||||||
|
std::string dynamic_text;
|
||||||
|
|
||||||
|
// first pb with static postfix text
|
||||||
|
p1.set_option(option::PostfixText{"Static text"});
|
||||||
|
|
||||||
|
// second pb with dynamic postfix text
|
||||||
|
for (auto &t: some_text) {
|
||||||
|
dynamic_text += t + " ";
|
||||||
|
p2.set_option(option::PostfixText{dynamic_text});
|
||||||
|
mp.set_progress<0>(size_t(0));
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// update postfix to little text for pb #2
|
||||||
|
p2.set_option(option::PostfixText{"abcd"});
|
||||||
|
mp.set_progress<0>(size_t(0));
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2516,6 +2516,14 @@ public:
|
|||||||
}
|
}
|
||||||
os.flush();
|
os.flush();
|
||||||
|
|
||||||
|
// std::cout << "\n"
|
||||||
|
// << prefix_length << " "
|
||||||
|
// << start_length << " "
|
||||||
|
// << bar_width << " "
|
||||||
|
// << end_length << " "
|
||||||
|
// << postfix_length << " "
|
||||||
|
// << " = " << terminal_width << "\n";
|
||||||
|
|
||||||
if ((type == ProgressType::incremental && progress_ >= max_progress) ||
|
if ((type == ProgressType::incremental && progress_ >= max_progress) ||
|
||||||
(type == ProgressType::decremental && progress_ <= min_progress)) {
|
(type == ProgressType::decremental && progress_ <= min_progress)) {
|
||||||
get_value<details::ProgressBarOption::completed>() = true;
|
get_value<details::ProgressBarOption::completed>() = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user