Jump to content

Creating changing text in a CMD shell without newlines

- - - - -

  • Please log in to reply
1 reply to this topic

#1
TheSourceOfX

TheSourceOfX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
This may seem like a silly question, but I'm just having a hard time phrasing Google inquiries. Whenever you use some sort of automated installer from a shell it displays some kind of updating text informing you of how much time is left and how much of the given file was downloaded. This updating text stays on the same line the whole time. Does anyone know how to make that happen?

-Eitan

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Choose your pick, you can do carriage returns '\r' to return the curser to the beginning of the line, and rewrite the progress bar/update, or work with a library such as ncurses (more common) to do the heavy lifting, I personally use VT escapes to clear the screen and start on a new screen, this is what applications such as 'top' does on unix distributions.

A more rudimentry of carriage returns:
#include <stdio.h>
int main() {
    //13 = '\r';
    printf("foo%cbar%cbaz", 13, 13);
}
And for ANSI escapes:
#include <stdio.h>
int main() {
    //27 = ESC
    printf("foo bar%c[2Jbaz", 27);
}
You will see the output as purely "baz", as the resulting carriage return or clearscreen will rewrite foo and bar.

For an existing implementation, you can look at some GNU applications, or a nice clean example source of a multipurpose progress bar is here:
Command Line Progress Bar

Edited by Alexander, 03 September 2010 - 10:47 PM.
Added examples.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users