It's not manipulating stdout -- it's overwriting the characters which have already been displayed by the terminal.
Try this:
#include <stdio.h>
#include <unistd.h>
static char bar[] = "======================================="
"======================================>";
int main() {
int i;
for (i = 77; i >= 0; i--) {
printf("[%s]
", &bar[i]);
fflush(stdout);
sleep(1);
}
printf("
");
return 0;
}
That's pretty close to wget
's output, right?
is a carriage-return, which the terminal interprets as "move the cursor back to the start of the current line".
Your shell, if it's bash
, uses the GNU Readline library, which provides much more general functionality, including detecting terminal types, history management, programmable key bindings, etc.
One more thing -- when in doubt, the source for your wget, your shell, etc. are all available.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…