I have a program that loops over some files, doing various things to them. With each file, the program prints some output to the terminal. This output could be of various forms (over which I have no control).

I want to have a percentage progress bar displayed solely at the bottom of the terminal output, below all of the continuous output of the file processing activities. How could I do this?

Note that I do not have access to ncurses.

Using guidance from a previous question, I have a basic attempt here:

#include <unistd.h>
#include <iostream>
#include <string>

using namespace std;

void print_progress_bar(int percentage){
  string progress = "[" + string(percentage, '*') + string(100 - percentage, ' ') + "]";
  cout << progress << "\r\033[F\033[F\033[F" << flush;
}

void print_stuff(){
    int number_of_lines = (rand() % 9) + 1;
    for (int i=0; i <= number_of_lines; ++i){
        cout << "some junk output of " << number_of_lines << " lines\n";
    }
}

int main(){
  cout << endl;
  for (int i=0; i <= 100; ++i){
    std::cout << "processing file number " << i << "\n";
    print_stuff();
    print_progress_bar(i);
    usleep(10000);
  }
  cout << endl;
  cout << endl;
  cout << endl;
  cout << endl;
}
0

There are 0 best solutions below