Loading [MathJax]/extensions/tex2jax.js
Tatooine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages Concepts
progress_bars.h
Go to the documentation of this file.
1#ifndef TATOOINE_PROGRESS_BARS_H
2#define TATOOINE_PROGRESS_BARS_H
3//==============================================================================
4#include <concepts>
5#include <indicators/block_progress_bar.hpp>
6#include <indicators/indeterminate_progress_bar.hpp>
7//==============================================================================
8namespace tatooine {
9//==============================================================================
10template <typename Indicator>
12 requires(Indicator indicator, double p) { indicator.set_progress(p); };
13
14template <typename Indicator> struct indicator_msg {
15 Indicator &indicator;
16 auto operator=(char const *msg) -> indicator_msg & {
17 indicator.set_option(indicators::option::PostfixText{msg});
18 return *this;
19 }
20 auto operator=(std::string const &msg) -> indicator_msg & {
21 indicator.set_option(indicators::option::PostfixText{msg});
22 return *this;
23 }
24 auto set_text(std::string const &text) -> void {
25 indicator.set_option(indicators::option::PostfixText{text});
26 }
27 auto mark_as_completed() -> void { indicator.mark_as_completed(); }
28};
29template <typename Indicator> struct progress_indicator_wrapper {
30 Indicator &indicator;
31 double &progress;
32 //----------------------------------------------------------------------------
33 auto operator=(char const *msg) -> progress_indicator_wrapper & {
34 indicator.set_option(indicators::option::PostfixText{msg});
35 return *this;
36 }
37 //----------------------------------------------------------------------------
38 auto operator=(std::string const &msg) -> progress_indicator_wrapper & {
39 indicator.set_option(indicators::option::PostfixText{msg});
40 return *this;
41 }
42 //----------------------------------------------------------------------------
43 auto set_text(std::string const &text) -> void {
44 indicator.set_option(indicators::option::PostfixText{text});
45 }
46};
47//==============================================================================
49 using namespace indicators;
50 return IndeterminateProgressBar{
51 option::BarWidth{20},
52 option::Start{"▶"},
53 option::Fill{" "},
54 option::Lead{"░▒▓▒░"},
55 option::End{"◀"},
56 option::ForegroundColor{Color::white},
57 option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}};
58}
59//------------------------------------------------------------------------------
61 return std::thread{[&indicator]() {
62 while (!indicator.is_completed()) {
63 indicator.tick();
64 std::this_thread::sleep_for(std::chrono::milliseconds(100));
65 }
66 indicator.tick();
67 }};
68}
69//------------------------------------------------------------------------------
70template <typename F, typename... Args>
71 requires is_invocable<F, Args...>
72auto indeterminate_progress_bar(F &&f, Args &&...args) -> decltype(auto) {
74 auto job_completion_thread = make_indeterminate_completion_thread(indicator);
75 using return_type = invoke_result<F, Args...>;
76 if constexpr (is_void<return_type>) {
77 f(std::forward<Args>(args)...);
78 indicator.mark_as_completed();
79 job_completion_thread.join();
80 } else {
81 decltype(auto) ret = f(std::forward<Args>(args)...);
82 indicator.mark_as_completed();
83 job_completion_thread.join();
84 return ret;
85 }
86}
87//------------------------------------------------------------------------------
88template <typename F, typename... Args>
89 requires is_invocable<F, indicator_msg<indicators::IndeterminateProgressBar>,
90 Args...> &&
92 F, indicator_msg<indicators::IndeterminateProgressBar>, Args...>>
93auto indeterminate_progress_bar(F &&f, Args &&...args) {
95 auto job_completion_thread = make_indeterminate_completion_thread(indicator);
96
98 std::forward<Args>(args)...);
99 indicator.mark_as_completed();
100 job_completion_thread.join();
101}
102//------------------------------------------------------------------------------
103template <typename F, typename... Args>
104 requires is_invocable<F, indicator_msg<indicators::IndeterminateProgressBar>,
105 Args...> &&
107 F, indicator_msg<indicators::IndeterminateProgressBar>,
108 Args...>>)
109 auto indeterminate_progress_bar(F &&f, Args &&...args)
110 -> decltype(auto) {
111 using namespace indicators;
112 using return_type =
114 Args...>;
116 auto job_completion_thread = make_indeterminate_completion_thread(indicator);
117
118 decltype(auto) ret =
120 std::forward<Args>(args)...);
121 indicator.mark_as_completed();
122 job_completion_thread.join();
123 return ret;
124}
125//==============================================================================
126template <typename F, typename... Args>
127 requires std::invocable<
128 F, progress_indicator_wrapper<indicators::BlockProgressBar>,
129 Args...>
130auto progress_bar(F &&f, Args &&...args) -> decltype(auto) {
131 indicators::BlockProgressBar progress_indicator{
132 indicators::option::BarWidth{20},
133 // indicators::option::Start{"▶"},
134 // indicators::option::End{"◀"},
135 indicators::option::ShowElapsedTime{true},
136 indicators::option::ShowRemainingTime{true},
137 indicators::option::ForegroundColor{indicators::Color::white},
138 indicators::option::FontStyles{
139 std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
140 auto progress = double{};
142 progress_indicator, progress};
143
144 auto job_completion_thread = std::thread{[&progress_indicator, &progress] {
145 while (progress < 1) {
146 progress_indicator.set_progress(100 * progress);
147 std::this_thread::sleep_for(std::chrono::milliseconds(100));
148 }
149 progress_indicator.set_progress(100);
150 }};
151
152 if constexpr (is_invocable<F, Args...>) {
153 using return_type = invoke_result<F, Args...>;
154 if constexpr (is_void<return_type>) {
155 f(std::forward<Args>(args)...);
156 wrapper.progress = 100;
157 job_completion_thread.join();
158 } else {
159 decltype(auto) ret = f(std::forward<Args>(args)...);
160 wrapper.progress = 100;
161 job_completion_thread.join();
162 return ret;
163 }
164 } else if constexpr (is_invocable<F,
166 indicators::BlockProgressBar>,
167 Args...>) {
168 using return_type = invoke_result<
170 if constexpr (is_void<return_type>) {
171 f(wrapper, std::forward<Args>(args)...);
172 wrapper.progress = 100;
173 job_completion_thread.join();
174 } else {
175 decltype(auto) ret = f(wrapper, std::forward<Args>(args)...);
176 wrapper.progress = 100;
177 job_completion_thread.join();
178 return ret;
179 }
180 }
181}
182//==============================================================================
183} // namespace tatooine
184//==============================================================================
185#endif
Definition: progress_bars.h:11
Definition: algorithm.h:6
auto indeterminate_progress_bar(F &&f, Args &&...args) -> decltype(auto)
Definition: progress_bars.h:72
static constexpr auto is_void
Definition: type_traits.h:45
std::invoke_result_t< F, Args... > invoke_result
Definition: concepts.h:127
auto make_default_indeterminate_progress_bar()
Definition: progress_bars.h:48
auto make_indeterminate_completion_thread(auto &indicator)
Definition: progress_bars.h:60
static constexpr auto is_invocable
Definition: type_traits.h:66
auto progress_bar(F &&f, Args &&...args) -> decltype(auto)
Definition: progress_bars.h:130
Definition: progress_bars.h:14
auto operator=(std::string const &msg) -> indicator_msg &
Definition: progress_bars.h:20
auto mark_as_completed() -> void
Definition: progress_bars.h:27
Indicator & indicator
Definition: progress_bars.h:15
auto operator=(char const *msg) -> indicator_msg &
Definition: progress_bars.h:16
auto set_text(std::string const &text) -> void
Definition: progress_bars.h:24
Definition: progress_bars.h:29
auto operator=(char const *msg) -> progress_indicator_wrapper &
Definition: progress_bars.h:33
auto operator=(std::string const &msg) -> progress_indicator_wrapper &
Definition: progress_bars.h:38
auto set_text(std::string const &text) -> void
Definition: progress_bars.h:43
double & progress
Definition: progress_bars.h:31
Indicator & indicator
Definition: progress_bars.h:30