68 std::string fc =
"#", std::string ec =
".",
69 std::string pre =
"[", std::string suf =
"]",
100 std::chrono::steady_clock::time_point last_draw_;
111 auto now = std::chrono::steady_clock::now();
112 if (current_ < total_ &&
113 (
now - last_draw_) < std::chrono::milliseconds{50})
117 int percent = current_ * 100 / total_;
118 int filled = percent * config_->width / 100;
119 config_->os <<
'\r' << config_->prefix;
120 for (
int i = 0; i < filled; ++i)
121 config_->os << config_->fill_char;
122 for (
int i = filled; i < config_->width; ++i)
123 config_->os << config_->empty_char;
124 config_->os << config_->suffix;
125 if (config_->show_percent)
127 config_->os.width(4);
128 config_->os << percent <<
'%';
143 size_t total, Iter it)
144 : config_(config), current_(current),
145 total_(total), it_(it),
146 last_draw_(std::chrono::steady_clock::
now()) {}
151 if (current_ < total_)
171 return current_ == other.current_;
177 return current_ != other.current_;
193 template <
typename Cont>
196 total_(std::distance(std::
begin(cont), std::
end(cont))),
197 begin_(std::
begin(cont)),
198 end_(std::
end(cont)) {}
206 template <
typename Cont>
209 total_(std::distance(std::
begin(cont), std::
end(cont))),
210 begin_(std::
begin(cont)),
211 end_(std::
end(cont)) {}
219 config_.os << std::endl;
225 return iterator(&config_, 0, total_, begin_);
231 return iterator(&config_, total_, total_, end_);
264 static ProgressConfig cfg{std::cout, 50,
"=",
"-",
"",
"",
false};
276 "\u2595",
"\u258F",
true};
295 template <
class Cont>
297 -> Progress<
decltype(std::begin(cont))>
299 return {std::forward<Cont>(cont), pc};
进度条的迭代器,负责绘制进度条。
Definition progress.h:95
decltype(*it_) operator*() const
解引用:返回底层迭代器所指向的元素。
Definition progress.h:181
bool operator==(const iterator &other) const
比较迭代器是否相等(通过当前进度索引)。
Definition progress.h:169
iterator(const ProgressConfig *config, size_t current, size_t total, Iter it)
构造进度条迭代器。
Definition progress.h:142
iterator operator++(int)
后置递增(调用前置递增)。
Definition progress.h:161
iterator & operator++()
前置递增:移动到下一个元素,并更新进度条。
Definition progress.h:149
bool operator!=(const iterator &other) const
比较迭代器是否不等。
Definition progress.h:175
Progress(const Cont &cont, const ProgressConfig &config={})
从 const 容器构造进度条对象。
Definition progress.h:194
Progress(Cont &cont, const ProgressConfig &config={})
从非 const 容器构造进度条对象。
Definition progress.h:207
~Progress()
析构函数,确保最后输出换行。
Definition progress.h:217
iterator begin()
返回指向第一个元素的迭代器(进度条起始)。
Definition progress.h:223
iterator end()
返回指向末尾的迭代器(进度条结束)。
Definition progress.h:229
定义 console 库使用的自定义异常类层次结构。
const ProgressConfig & beautiful()
美观样式(使用 Unicode 块字符):宽度 50,填充 '█',空白 '░',边框 '▒' 和 '▏',显示百分比。
Definition progress.h:273
const ProgressConfig & simple()
简洁样式:宽度 50,填充 '=',空白 '-',无前后缀,不显示百分比。
Definition progress.h:262
const ProgressConfig & normal()
普通样式:宽度 50,填充 '#',空白 '.',前后缀 "[]",显示百分比。
Definition progress.h:252
auto progress(Cont &&cont, const ProgressConfig &pc={}) -> Progress< decltype(std::begin(cont))>
创建进度条对象的辅助函数(自动推导容器类型)。
Definition progress.h:296
Time now()
获取当前时间点(自纪元以来的纳秒数)。
Definition time.h:125
进度条显示配置。
Definition progress.h:48
std::string empty_char
未填充部分使用的字符(如 ".")。
Definition progress.h:52
std::string fill_char
已填充部分使用的字符(如 "#")。
Definition progress.h:51
std::string suffix
进度条后缀字符串(如 "]"]。
Definition progress.h:54
std::ostream & os
输出目标流,默认为 std::cout。
Definition progress.h:49
std::string prefix
进度条前缀字符串(如 "[")。
Definition progress.h:53
ProgressConfig(std::ostream &o=std::cout, int w=50, std::string fc="#", std::string ec=".", std::string pre="[", std::string suf="]", bool sp=true)
构造一个进度条配置对象。
Definition progress.h:67
int width
进度条的宽度(字符数)。
Definition progress.h:50
bool show_percent
是否在进度条后显示百分比数字。
Definition progress.h:55