Skip to content

Instantly share code, notes, and snippets.

@pbabics
Created April 5, 2017 16:44
Show Gist options
  • Save pbabics/3ee9a1e1c74347d35daf9b8b7e18b069 to your computer and use it in GitHub Desktop.
Save pbabics/3ee9a1e1c74347d35daf9b8b7e18b069 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)
#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