Monday, 20 March 2017

How to write in bold using ncurses in C

To use ncurses in your program, you first need to install ncurses. Once installed, write the below code:

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  int ch;
  initscr();
  raw();
  keypad(stdscr, TRUE);
  noecho();
  printw("Type any character to see it in bold\n");
  ch = getch();
  if (ch == KEY_F(1))
    printw("F1 Key pressed");
  else {
    printw("The pressed key is ");
    attron(A_BOLD);
    printw("%c", ch);
    attroff(A_BOLD);
  }
  refresh();
  getch();
  endwin();
  return EXIT_SUCCESS;
}

To compile it, use the ncurses library using
$ gcc file.c -lncurses

Once compiled, it will generate a executable named a.out

Type any character to see it in bold
The pressed key is A

If you need any help installing ncurses, write in comment section.

No comments:

Post a Comment