Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cursor blinking removal in terminal, how to?

I use the following lines to output my simulation's progress info in my c++ program,

double N=0;
double percent=0;
double total = 1000000;
for (int i; i<total; ++i)
{
    percent = 100*i/total;
    printf("\r[%6.4f%%]",percent);
}

It works fine!

But the problem is I see the terminal cursor keeps blinking cyclically through the numbers, this is very annoying, anyone knows how to get rid of this?

I've seen some programs like wget or ubuntu apt, they use progress bar or percentages too, but they seems no blinking cursor issue, I am wondering how did they do that?

Thanks!

like image 238
Daniel Avatar asked Jun 11 '12 21:06

Daniel


2 Answers

You can hide and show the cursor using the DECTCEM (DEC text cursor enable mode) mode in DECSM and DECRM:

fputs("\e[?25l", stdout); /* hide the cursor */

fputs("\e[?25h", stdout); /* show the cursor */
like image 103
LeoNerd Avatar answered Sep 28 '22 10:09

LeoNerd


Just a guess: try to use a proper number of '\b' (backspace) characters instead of '\r'.

== EDIT ==

I'm not a Linux shell wizard, but this may work:

system("setterm -cursor off");
// ...display percentages...
system("setterm -cursor on");

Don't forget to #include <cstdlib> or <iostream>.

like image 23
kol Avatar answered Sep 28 '22 10:09

kol