Update Command-Line Output, I.E. for Progress

Update Command-line Output, i.e. for Progress

This can be done using ANSI Escape Sequences -- see here for a list.

In PHP, you'll use "\033" when it's indicated ESC on that page.


In your case, you could use something like this :

echo "Progress :      ";  // 5 characters of padding at the end
for ($i=0 ; $i<=100 ; $i++) {
echo "\033[5D"; // Move 5 characters backward
echo str_pad($i, 3, ' ', STR_PAD_LEFT) . " %"; // Output is always 5 characters long
sleep(1); // wait for a while, so we see the animation
}


I simplified a bit, making sure I always have 5 extra characters, and always displaying the same amount of data, to always move backwards by the same number of chars...

But, of course, you should be able to do much more complicated, if needed ;-)

And there are many other interesting escape sequences : colors, for instance, can enhance your output quite a bit ;-)

Update command line output

Use autoflush with STDOUT:

local $| = 1; # Or use IO::Handle; STDOUT->autoflush;

print 'Progress: ';
my $progressString;
while ...
{
# remove prev progress
print "\b" x length($progressString) if defined $progressString;
# do lots of processing, update $counter
$progressString = "$counter / $total"; # No more newline
print $progressString; # Will print, because auto-flush is on
# end of processing
}
print "\n"; # Don't forget the trailing newline

java display changing progress on command line

It depends on your console, but the simplest way is to use the backspace character to move the cursor backwards, in front of the recently printed characters:

int[] step = {100,200,300,400,500};
System.out.print("$> processed < ");
for (int i : step) {
System.out.print(i + " > records.\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
Thread.sleep(500);
}

This works on the windows command line, but for example it does not work on the Eclipse console.

You should add some additional logic to count the numbers of characters printed, and not hard-code all these "\b" characters, but calculate the right number depending on the recent output.

As @Reza suggests, using "\r" is even easier:

int[] step = {100,200,300,400,500};
for (int i : step) {
System.out.print("$> processed < " + i + " > records.\r");
Thread.sleep(500);
}

It does still not work on the Eclipse console (but is more readable than the backspace approach), but avoids the handling of all the backspace characters. If printing a shorter line than before, it might be necessary to print some additional spaces to clear trailing characters.

How to update multiple line in console with Golang

I was looking to implement a similar functionality and came across this question. Not sure if you're still looking for a solution, but I found one :)

It turns out uilive can very well be used to update multiple lines, by making use of the Newline() function on a *uilive.Writer. Editing the example on their repository to write the download progress on multiple lines, we get:

writer := uilive.New()       // writer for the first line
writer2 := writer.Newline() // writer for the second line
// start listening for updates and render
writer.Start()

for i := 0; i <= 100; i++ {
fmt.Fprintf(writer, "Downloading File 1.. %d %%\n", i)
fmt.Fprintf(writer2, "Downloading File 2.. %d %%\n", i)
time.Sleep(time.Millisecond * 5)
}

fmt.Fprintln(writer, "Finished downloading both files :)")
writer.Stop() // flush and stop rendering

For implementing something to track the progress of multiple concurrently running tasks, as @blami's answer also states, a good approach is to have a separate goroutine that handles all terminal output.

If you're interested, I've implemented this exact functionality in mllint, my tool for linting machine learning projects. It runs all its underlying linters (i.e. tasks) in parallel and prints their progress (running / done) in the manner that you describe in your question. See this file for the implementation of that. Specifically, the printTasks function reads from the list of tasks in the system and prints each task's display name and status on a new line.

Note that especially with multi-line printing, it becomes important to have control over flushing the print buffer, as you do not want your writer to automatically flush halfway through writing an update to the buffer. Therefore, I set the writer's RefreshDuration to something large (e.g. time.Hour) and call Flush() on the writer after printing all lines, i.e. at the end of printTasks.

Output progress of function to console inside of executing Promise

Since I can't comment, I'm leaving this here: check out this answer https://stackoverflow.com/a/10232330/4662777

Replace console output in Python

An easy solution is just writing "\r" before the string and not adding a newline; if the string never gets shorter this is sufficient...

sys.stdout.write("\rDoing thing %i" % i)
sys.stdout.flush()

Slightly more sophisticated is a progress bar... this is something I am using:

def start_progress(title):
global progress_x
sys.stdout.write(title + ": [" + "-"*40 + "]" + chr(8)*41)
sys.stdout.flush()
progress_x = 0

def progress(x):
global progress_x
x = int(x * 40 // 100)
sys.stdout.write("#" * (x - progress_x))
sys.stdout.flush()
progress_x = x

def end_progress():
sys.stdout.write("#" * (40 - progress_x) + "]\n")
sys.stdout.flush()

You call start_progress passing the description of the operation, then progress(x) where x is the percentage and finally end_progress()

Overwrite current output in the R console

This program seems to work:

while (1) {
cat('\b\b\b\b\b\b',format(Sys.time(),'%H:%M'))
flush.console()
}

Are there any reasons this might be a bad idea?

/edit: even better (thanks @Greg Snow):

while (1) {
cat('\r',format(Sys.time(),'%H:%M:%S'))
flush.console()
}

Android SDK sdkmanager progress update

With the latest SDK tools (26.1.1 as of Sept 27th, 2017), we now have a download progress bar:

$ sdkmanager "build-tools;26.0.2"
[===================== ] 50% Downloading build-tools_r26.0.2-ma

It seems there is no option to show this progress when using older versions. Solution: Update SDK tools.



Related Topics



Leave a reply



Submit