I’ve been working with C for a while and am reasonably proficient at simple command-line interfaces. I’ve also had a play with the curses library, for terminal applications that do more than write text to stdout. However, I can’t figure out where the half-way point is – applications like wget or make have, for example, the ability to update the text they have output (like wget‘s bouncing download meter and progress bar), without taking over the entire screen.
Is this sort of interface something I should be using curses for, or is there a step in between? Preferably one that is cross-platform.
You can do some simple things by just printing backspace characters
'\b'and raw carriage returns'\r'(without newlines). Backspaces move the cursor backwards one character, allowing you to overwrite your output, and carriage returns move the cursor back to the start of the current line, allowing you to overwrite the current line.Here’s a simple example of a progress bar:
Note that you should only do this if you’re writing to an actual terminal (as opposed to being redirected to a file or pipe). You can test for that with
if(isatty(fileno(stdout)) { ... }. Of course, the same would be true if you were using any other library such as curses or ncurses.