Skip to content

Cursor Visible #1

@WillianBR

Description

@WillianBR

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions