Dimitri Danilov
Dimitri Danilov

Reputation: 1352

How can I refresh my terminal every second and get user input ?

I am trying to do an implementation of htop (system monotitoring) in c++.

So I am using ncurses to refresh my terminal.

I need to get new info every 5 seconds for example, im using a loop to do so.

 while (42)
  {
      key = std::cin.get();
      std::cout << key;
      this->gereEvent(key);
      std::cout << i<< std::endl;
      if (i == 500000000)
      {
          std::cout << "test"<< std::endl;
  //      fputs(tgetstr((char *)"cl", 0), stdout);
        this->refresh();
        i = 0;
      }
      i++;
  }

But problem is cin.get() stops the loop.. I cannot do a thread eitheir because std::thread needs c++11.

Have you an idea of how I can do that ?

Upvotes: 0

Views: 1124

Answers (1)

Evers
Evers

Reputation: 1908

You need to poll the keyboard events. This can be done in ncurses with getch.

#include<stdio.h>
#include<curses.h>
#include<unistd.h>

int main ()
{
    int i=0;

    initscr();     //in ncurses
    timeout(0);
    while(!i)
    {
        usleep(1);
        i=getch();
        printw("%d ",i);
        if(i>0)
            i=1;
        else
            i=0;
    }
    endwin();
    printf("\nhitkb end\n");
    return 0;
}

This example is from http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/comment-page-1/#comment-2100.

Upvotes: 3

Related Questions