Skip to content

Instantly share code, notes, and snippets.

@pbabics
Last active April 5, 2017 16:44
Show Gist options
  • Save pbabics/e4a3d0c410a26d8485cb41c9791904dd to your computer and use it in GitHub Desktop.
Save pbabics/e4a3d0c410a26d8485cb41c9791904dd to your computer and use it in GitHub Desktop.
Simple Linux console application for printing progress of per line output items (i.e progress of printing sha sums of listed files via parallels), to compile use: g++ -O3 -o progress -Wall -Wno-long-long -pedantic -std=c++11 progress.cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
int main(int argc, const char* argv[])
{
if (argc != 2)
{
eprintf("Usage: %s <number of lines>\n", argv[0]);
return 1;
}
long total_lines = atol(argv[1]);
long current_line = 0;
fprintf(stderr, "Progress: %ld / %ld (%.02f%%)\r", current_line, total_lines, current_line / double(total_lines) * 100.f);
int BUFFER_SIZE = 1024;
char* buffer = new char[BUFFER_SIZE];
while (!feof(stdin))
{
if (!fgets(buffer, BUFFER_SIZE, stdin))
break;
int l = strlen(buffer);
if (l == BUFFER_SIZE - 1)
printf("%*s", l, buffer);
else
{
++current_line;
fprintf(stderr, "Progress: %ld / %ld (%.02f%%)\r", current_line, total_lines, current_line / double(total_lines) * 100.f);
printf("%*s", l, buffer);
}
}
fprintf(stderr, "Complete%*s\n", 200, " ");
delete [] buffer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment