mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Updated smooth block progress bar update logic
This commit is contained in:
@@ -33,6 +33,7 @@ SOFTWARE.
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
@@ -148,18 +149,21 @@ private:
|
|||||||
}
|
}
|
||||||
std::cout << _prefix_text;
|
std::cout << _prefix_text;
|
||||||
std::cout << _start;
|
std::cout << _start;
|
||||||
auto pos = static_cast<size_t>(_progress * static_cast<float>(_bar_width) / 100.0);
|
|
||||||
auto remainder = _bar_width - pos;
|
|
||||||
std::vector<std::string> lead_characters{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
std::vector<std::string> lead_characters{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
||||||
_lead = lead_characters[remainder % lead_characters.size()];
|
auto progress = std::min(1.0f, std::max(0.0f, _progress / 100.0f));
|
||||||
for (size_t i = 0; i < _bar_width; ++i) {
|
auto whole_width = std::floor(progress * _bar_width);
|
||||||
if (i < pos)
|
auto remainder_width = fmod((progress * _bar_width), 1.0f);
|
||||||
std::cout << _fill;
|
auto part_width = std::floor(remainder_width * lead_characters.size());
|
||||||
else if (i == pos)
|
_lead = lead_characters[part_width];
|
||||||
std::cout << _lead;
|
if ((_bar_width - whole_width - 1) < 0)
|
||||||
else
|
_lead = "";
|
||||||
std::cout << _remainder;
|
for (size_t i = 0; i < whole_width; ++i)
|
||||||
}
|
std::cout << _fill;
|
||||||
|
std::cout << _lead;
|
||||||
|
for (size_t i = 0; i < (_bar_width - whole_width - 1); ++i)
|
||||||
|
std::cout << " ";
|
||||||
|
|
||||||
std::cout << _end;
|
std::cout << _end;
|
||||||
if (_show_percentage) {
|
if (_show_percentage) {
|
||||||
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
||||||
|
|||||||
Reference in New Issue
Block a user