sidyll
sidyll

Reputation: 59297

Updating Output

I need to create an utility that "updates" its output, much like curl which keeps changing its last line:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  8434  100  8434    0     0   4064      0  0:00:02  0:00:02 --:--:--  7695

I think using something like curses is not the way to go here. I don't want to manipulate the window, I want to simply change my last line of output.

The solution I have in mind is to print a number of backspaces enough to rewrite the line. But I haven't tested this yet. I'd like to know if this is a "correct" way of doing it, or if there is a better one.

Also, in my case I need to update the last line. So I don't need a so large number of backspaces (if that's the solution); however (to make it generic) if I need to update the -10 line, rewriting the same thing from -9th line on might not be that efficient (or maybe it is...).

Upvotes: 0

Views: 55

Answers (2)

Jonathan Wood
Jonathan Wood

Reputation: 67223

In DOS, you can simply print the carriage return without the line feed and overwrite the last line.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490178

You can backspace over the line, or (generally easier) print a carriage return, and just re-print the entire line. When you do, be sure to rewrite the whole line though -- if (for example) you have a number counting down to 0, when it drops from 100 to 99 (for example) it won't necessarily overwrite the '1' unless you assure that a space gets printed there.

Upvotes: 2

Related Questions