| 199 | } |
| 200 | |
| 201 | void update(size_t current, size_t total) { |
| 202 | if (!is_output_a_tty()) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (!total) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | std::lock_guard<std::mutex> lock(mutex); |
| 211 | |
| 212 | if (lines.find(this) == lines.end()) { |
| 213 | lines[this] = max_line++; |
| 214 | std::cout << "\n"; |
| 215 | } |
| 216 | int lines_up = max_line - lines[this]; |
| 217 | |
| 218 | size_t width = 50; |
| 219 | size_t pct = (100 * current) / total; |
| 220 | size_t pos = (width * current) / total; |
| 221 | |
| 222 | std::cout << "\033[s"; |
| 223 | |
| 224 | if (lines_up > 0) { |
| 225 | std::cout << "\033[" << lines_up << "A"; |
| 226 | } |
| 227 | std::cout << "\033[2K\r[" |
| 228 | << std::string(pos, '=') |
| 229 | << (pos < width ? ">" : "") |
| 230 | << std::string(width - pos, ' ') |
| 231 | << "] " << std::setw(3) << pct << "% (" |
| 232 | << current / (1024 * 1024) << " MB / " |
| 233 | << total / (1024 * 1024) << " MB) " |
| 234 | << "\033[u"; |
| 235 | |
| 236 | std::cout.flush(); |
| 237 | |
| 238 | if (current == total) { |
| 239 | cleanup(this); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | ProgressBar(const ProgressBar &) = delete; |
| 244 | ProgressBar & operator=(const ProgressBar &) = delete; |