0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
progress_indicator.cc
Go to the documentation of this file.
1/**
2 * @file src/common/generators/progress_indicator.cc
3 *
4 * Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "progress_indicator.h"
20
21#include <util/util.h>
22
24
26 : progress_indicator(std::cout)
27{
28}
29
31 : ostream_(ostream)
32{
33 progress_bars_.set_option(indicators::option::HideBarWhenComplete{false});
34}
35
37 const std::string &name, size_t max, indicators::Color color)
38{
39 auto postfix_text = max > 0 ? fmt::format("{}/{}", 0, max) : std::string{};
40
41 const auto kBarWidth = 35U;
42 const auto kPrefixTextWidth = 25U;
43
44 auto bar = std::make_shared<indicators::ProgressBar>(
45 indicators::option::Stream{ostream_},
46 indicators::option::BarWidth{kBarWidth},
47 indicators::option::ForegroundColor{color},
48 indicators::option::ShowElapsedTime{true},
49#if _MSC_VER
50 indicators::option::Fill{"="}, indicators::option::Lead{">"},
51#else
52 indicators::option::Fill{"█"}, indicators::option::Lead{"█"},
53#endif
54 indicators::option::Remainder{"-"},
55 indicators::option::PrefixText{
56 fmt::format("{:<25}", util::abbreviate(name, kPrefixTextWidth))},
57 indicators::option::PostfixText{postfix_text});
58
60
61 progress_bars_.push_back(*bar);
62 bars_.push_back(bar);
63 auto bar_index = bars_.size() - 1;
64 progress_bar_index_.emplace(name, progress_state{bar_index, 0, max});
65
66 progress_bars_mutex_.unlock();
67}
68
69void progress_indicator::increment(const std::string &name)
70{
71 const auto kASTTraverseProgressPercent = 95U;
72
74
75 if (progress_bar_index_.count(name) == 0) {
76 progress_bars_mutex_.unlock();
77 return;
78 }
79
80 auto &p = progress_bar_index_.at(name);
81 auto &bar = progress_bars_[p.index];
82
83 p.progress++;
84
85 bar.set_progress((p.progress * kASTTraverseProgressPercent) / p.max);
86 bar.set_option(indicators::option::PostfixText{
87 fmt::format("{}/{}", p.progress, p.max)});
88
89 progress_bars_mutex_.unlock();
90}
91
93{
95
96 for (auto &[name, p] : progress_bar_index_) {
97 progress_bars_[p.index].mark_as_completed();
98 }
99
100 progress_bars_mutex_.unlock();
101}
102
103void progress_indicator::complete(const std::string &name)
104{
105 const auto kCompleteProgressPercent = 100U;
106
108
109 if (progress_bar_index_.count(name) == 0) {
110 progress_bars_mutex_.unlock();
111 return;
112 }
113
114 auto &p = progress_bar_index_.at(name);
115 auto &bar = progress_bars_[p.index];
116
117 p.progress = p.max;
118
119 bar.set_progress(kCompleteProgressPercent);
120
121#if _MSC_VER
122 const auto postfix_text = fmt::format("{}/{} OK", p.progress, p.max);
123#else
124 const auto postfix_text = fmt::format("{}/{} ✔", p.progress, p.max);
125#endif
126 bar.set_option(indicators::option::PostfixText{postfix_text});
127 bar.set_option(
128 indicators::option::ForegroundColor{indicators::Color::green});
129 bar.mark_as_completed();
130
131 progress_bars_mutex_.unlock();
132}
133
134void progress_indicator::fail(const std::string &name)
135{
137
138 if (progress_bar_index_.count(name) == 0) {
139 progress_bars_mutex_.unlock();
140 return;
141 }
142
143 auto &p = progress_bar_index_.at(name);
144 auto &bar = progress_bars_[p.index];
145
146#if _MSC_VER
147 const auto postfix_text = fmt::format("{}/{} FAILED", p.progress, p.max);
148#else
149 const auto postfix_text = fmt::format("{}/{} ✗", p.progress, p.max);
150#endif
151 bar.set_option(indicators::option::ForegroundColor{indicators::Color::red});
152 bar.set_option(indicators::option::PostfixText{postfix_text});
153 bar.mark_as_completed();
154
155 progress_bars_mutex_.unlock();
156}
157
158} // namespace clanguml::common::generators