Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
liveldy authored Jun 23, 2024
1 parent 7b8b6da commit cf7e073
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ProgressBar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "ProgressBar.h"
#include <iostream>
#include <algorithm>

ProgressBar::ProgressBar(int minValue, int maxValue, char placeholder)
: minValue(minValue), maxValue(maxValue), currentValue(minValue), placeholder(placeholder) {}

void ProgressBar::setMinValue(int minValue) {
this->minValue = minValue;
if (currentValue < minValue) {
currentValue = minValue;
}
}

void ProgressBar::setMaxValue(int maxValue) {
this->maxValue = maxValue;
if (currentValue > maxValue) {
currentValue = maxValue;
}
}

void ProgressBar::setCurrentValue(int currentValue) {
this->currentValue = std::clamp(currentValue, minValue, maxValue);
}

void ProgressBar::setPlaceholder(char placeholder) {
this->placeholder = placeholder;
}

void ProgressBar::display() const {
int progress = calculateProgress();
std::cout << "\033[?25l";
std::cout << "\r[";

for (int i = 0; i < progress; ++i) {
std::cout << placeholder;
}
for (int i = progress; i < 100; ++i) {
std::cout << " ";
}
std::cout << "] " << progress << "%" << std::flush;
std::cout << "\033[?25h";
}

int ProgressBar::calculateProgress() const {
if (maxValue == minValue) {
return 100;
}
return (static_cast<double>(currentValue - minValue) / (maxValue - minValue)) * 100;
}

0 comments on commit cf7e073

Please sign in to comment.