Search code examples
iocommand-line-interfaceoctave

Octave / CLI -- Keeping long outputs on the same console line


I'm using algorithms that require some time to compute and if I print an output to be sure that the correct section of code is executing even writing a single . per iteration I end up with entire screens full of dots that cover up all previous outputs that I might have been interest in.

Is there a way to "send the cursor back" when sending output to the console line? Alternatively, is there a way to output a "rotating bar" on the console output for the time required to execute a program from point A to point B?

What I'm looking for is to write the following, where each of these lines would appear on the same line in sequence:

Processing
Processing .
Processing ..
Pricessing ...
Processing
Processing .
Processing ..

Solution

  • You can print backspace characters (\b) which will move the cursor backwards. Seems like it may overwrite previous characters but on mine it doesn't. So something like this:

    printf ("processing ");
    for i = 1:3
      for i = 1:9
        printf (".");
        pause (0.1);
      endfor
      printf (repmat ("\b", 1, 9));
      printf (repmat (" ", 1, 9));
      printf (repmat ("\b", 1, 9));
    endfor
    printf ("\n");
    

    Alternatively, you can install the Octave Forge miscellaneous package, there's the text_waitbar function:

    pkg load miscellaneous;
    
    text_waitbar (0, 70); # set length of waitbar
    text_waitbar (0.0, "processing ");
    for i = 1:3
      for i = 1:9
        text_waitbar (i/9, "processing");
        pause (0.1);
      endfor
    endfor
    

    which will look like this:

    [############################################              ]   80%