Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
progress.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <chrono>
35#include <iostream>
36#include <iterator>
37#include <string>
38
39#include "../core/csexc.h"
40
41namespace console {
50 template <class CharT = char, class Traits = std::char_traits<CharT>>
52 using string_type = std::basic_string<CharT, Traits>;
53 using ostream_type = std::basic_ostream<CharT, Traits>;
54
56 int width;
62
74 int w = 50,
75 string_type fc = string_type{1, CharT('#')},
76 string_type ec = string_type{1, CharT('.')},
77 string_type pre = string_type{1, CharT('[')},
78 string_type suf = string_type{1, CharT(']')},
79 bool sp = true) :
80 os(o), width(w), fill_char(std::move(fc)),
81 empty_char(std::move(ec)), prefix(std::move(pre)),
82 suffix(std::move(suf)), show_percent(sp) {}
83 };
84
87
90
101 template <class CharT, class Traits, class Iter>
103 public:
105
110 class iterator {
112 size_t current_;
113 size_t total_;
114 Iter it_;
115 std::chrono::steady_clock::time_point last_draw_;
116
117 void draw() {
118 if (!config_) return;
119 auto now = std::chrono::steady_clock::now();
120 if (current_ < total_
121 && (now - last_draw_) < std::chrono::milliseconds{50}) {
122 return;
123 }
124 int percent = current_ * 100 / total_;
125 int filled = percent * config_->width / 100;
126 config_->os << CharT('\r') << config_->prefix;
127 for (int i = 0; i < filled; ++i)
129 for (int i = filled; i < config_->width; ++i)
132 if (config_->show_percent) {
133 config_->os.width(4);
134 config_->os << percent << CharT('%');
135 }
136 config_->os.flush();
137 last_draw_ = now;
138 }
139
140 public:
141 iterator(const config_type *config,
142 size_t current,
143 size_t total,
144 Iter it) :
145 config_(config), current_(current), total_(total), it_(it),
146 last_draw_(std::chrono::steady_clock::now()) {}
147
149 if (current_ < total_) {
150 ++current_;
151 ++it_;
152 draw();
153 }
154 return *this;
155 }
156
158 iterator tmp = *this;
159 ++(*this);
160 return tmp;
161 }
162
163 bool operator==(const iterator &other) {
164 return current_ == other.current_;
165 }
166
167 bool operator!=(const iterator &other) {
168 return current_ != other.current_;
169 }
170
171 decltype(*it_) operator*() { return *it_; }
172 };
173
174 template <typename Cont>
175 BasicProgress(const Cont &cont, const config_type &config) :
176 config_(config),
177 total_(std::distance(std::begin(cont), std::end(cont))),
178 begin_(std::begin(cont)), end_(std::end(cont)) {}
179
180 template <typename Cont>
181 BasicProgress(Cont &cont, const config_type &config) :
182 config_(config),
183 total_(std::distance(std::begin(cont), std::end(cont))),
184 begin_(std::begin(cont)), end_(std::end(cont)) {}
185
186 ~BasicProgress() { config_.os << std::endl; }
187
188 iterator begin() { return iterator(&config_, 0, total_, begin_); }
189 iterator end() { return iterator(&config_, total_, total_, end_); }
190
191 private:
193 size_t total_;
194 Iter begin_;
195 Iter end_;
196 };
197
199 template <class Iter>
201
203 template <class Iter>
205
206 namespace ProgressStyle {
208 static BasicProgressConfig<> cfg{std::cout};
209 return cfg;
210 }
211
213 static BasicProgressConfig<> cfg{//
214 std::cout,
215 50,
216 std::string(1, '='),
217 std::string(1, '-'),
218 std::string{},
219 std::string{},
220 false};
221 return cfg;
222 }
223
225 static BasicProgressConfig<> cfg{//
226 std::cout,
227 50,
228 std::string("\u2588"),
229 std::string("\u2591"),
230 std::string("\u2595"),
231 std::string("\u258F"),
232 true};
233 return cfg;
234 }
235 }
236
246 template <class CharT = char,
247 class Traits = std::char_traits<CharT>,
248 class Cont>
249 inline auto
251 -> BasicProgress<CharT, Traits, decltype(std::begin(cont))> {
252 return {std::forward<Cont>(cont), pc};
253 }
254}
Iter it_
Definition progress.h:114
iterator operator++(int)
Definition progress.h:157
size_t current_
Definition progress.h:112
void draw()
Definition progress.h:117
iterator & operator++()
Definition progress.h:148
size_t total_
Definition progress.h:113
bool operator==(const iterator &other)
Definition progress.h:163
std::chrono::steady_clock::time_point last_draw_
Definition progress.h:115
const config_type * config_
Definition progress.h:111
bool operator!=(const iterator &other)
Definition progress.h:167
decltype(*it_) operator*()
Definition progress.h:171
iterator(const config_type *config, size_t current, size_t total, Iter it)
Definition progress.h:141
进度条迭代器包装器,用于在遍历容器时显示进度。
Definition progress.h:102
BasicProgress(Cont &cont, const config_type &config)
Definition progress.h:181
~BasicProgress()
Definition progress.h:186
BasicProgressConfig< CharT, Traits > config_type
Definition progress.h:104
BasicProgress(const Cont &cont, const config_type &config)
Definition progress.h:175
定义 console 库使用的自定义异常类层次结构。
Definition progress.h:206
const BasicProgressConfig & normal()
Definition progress.h:207
const BasicProgressConfig & beautiful()
Definition progress.h:224
const BasicProgressConfig & simple()
Definition progress.h:212
本库所有组件所在的顶层命名空间。
BasicProgress< wchar_t, std::char_traits< wchar_t >, Iter > WProgress
BasicProgress<wchar_t, std::char_traits<wchar_t>, Iter> 的类型别名。
Definition progress.h:204
BasicProgressConfig< wchar_t > WProgressConfig
BasicProgressConfig<wchar_t> 的类型别名。
Definition progress.h:89
Time now()
获取当前时间点(自纪元以来的纳秒数)。
Definition time.h:204
BasicProgress< char, std::char_traits< char >, Iter > Progress
BasicProgress<char, std::char_traits<char>, Iter> 的类型别名。
Definition progress.h:200
BasicProgressConfig< char > ProgressConfig
BasicProgressConfig<char> 的类型别名。
Definition progress.h:86
auto progress(Cont &&cont, const BasicProgressConfig< CharT, Traits > &pc) -> BasicProgress< CharT, Traits, decltype(std::begin(cont))>
创建进度条对象的辅助函数。
Definition progress.h:250
进度条显示配置。
Definition progress.h:51
string_type fill_char
Definition progress.h:57
int width
Definition progress.h:56
bool show_percent
Definition progress.h:61
std::basic_ostream< CharT, Traits > ostream_type
Definition progress.h:53
string_type empty_char
Definition progress.h:58
string_type prefix
Definition progress.h:59
BasicProgressConfig(ostream_type &o, int w=50, string_type fc=string_type{1, CharT('#')}, string_type ec=string_type{1, CharT('.')}, string_type pre=string_type{1, CharT('[')}, string_type suf=string_type{1, CharT(']')}, bool sp=true)
构造进度条配置对象。
Definition progress.h:73
string_type suffix
Definition progress.h:60
ostream_type & os
Definition progress.h:55
std::basic_string< CharT, Traits > string_type
Definition progress.h:52