Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor Visible #1

Open
WillianBR opened this issue Sep 12, 2024 · 0 comments
Open

Cursor Visible #1

WillianBR opened this issue Sep 12, 2024 · 0 comments

Comments

@WillianBR
Copy link

Thank you for the buffering.

There's one thing I did with the code here!

Since it's intended to run on Linux only. I added extra functions.

void hide_caret() {
        printf("\e[?25l");
}

void show_caret() {
        printf("\e[?25h");
}

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

The funcitons hide_caret() and show_caret() area called before and after the while loop.

Also I added one function to detect keyboard pressing to exit the while loop. It's annoying to press "Ctrl+C" to exit the TUI Screen saver!

The kbhit() function requires the headers down below:

#include <termios.h>
#include <fcntl.h>

The kbhit() can be changed to return the keycode. Let's say we want to exit if it's ESC.

Just one more thing! This single line functions could be replaced by a DEFINE directive!

But I got what I wanted, so...

@WillianBR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant