Skip to content

Instantly share code, notes, and snippets.

@christianparpart
Created May 2, 2022 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianparpart/9c5f06864828d8438478aaa01614c5c4 to your computer and use it in GitHub Desktop.
Save christianparpart/9c5f06864828d8438478aaa01614c5c4 to your computer and use it in GitHub Desktop.
C++ Progress bar, using new Fira Code inspired codepoints.
#include <array>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <thread>
using namespace std;
using namespace std::chrono;
void paintProgressBar(float progress, int columnWidth)
{
auto const filledColumns = static_cast<int>(floorf(progress * static_cast<float>(columnWidth)));
if (filledColumns == 0)
cout << "\uee00"; // left empty
else
cout << "\uee03"; // left filled
int i = 1;
while (i++ < min(filledColumns, columnWidth - 1))
cout << "\uee04"; // middle filled
while (i++ < columnWidth)
cout << "\uee01"; // middle empty
if (filledColumns >= columnWidth)
cout << "\uee05"; // Right filled
else
cout << "\uee02"; // Right empty
cout << " | " << int(progress * 100.0f) << '%';
cout.flush();
}
int main()
{
cout << "Progress bar demo: ";
for (float progress = 0.0f; progress <= 1.0f + std::numeric_limits<float>::epsilon(); progress += 0.1f)
{
cout << "\0337"; // DECSC - save cursor
paintProgressBar(progress, 10);
this_thread::sleep_for(chrono::milliseconds(250));
cout << "\0338"; // DECRS - restore cursor
}
cout << '\n';
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment