Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

More Cool Features


Chapter 18: Define Your Own Color Palettes

  • init_color(short color_number, short r, short g, short b) uses values from 0 to 1000, not 255.
  • Custom colors are global: once you redefine COLOR_GREEN, that's what it means from now on.
#include <curses.h>
#include <ncurses.h>

int main() {
  initscr();

  // check for color support
  if (!has_colors() || !can_change_color()) {
    printw("Your terminal does not support colors.");
    getch();
    return 1;
  }
  start_color();

  // Query how many you get
  printw("COLORS      : %d\n", COLORS);
  printw("COLOR_PAIRS : %d\n", COLOR_PAIRS);

  // Redefine COLOR_CYAN
  init_color(COLOR_CYAN, 1000, 200, 800);
  init_pair(1, COLOR_CYAN, COLOR_BLACK);
  attron(COLOR_PAIR(1));
  printw("Custom color magenta-ish cyan?\n");
  attroff(COLOR_PAIR(1));

  getch();

  endwin();
  return 0;
}

Chapter 19: Character Management (delch, wdelch, mvdelch, mvwdelch)

// routines for character management

#include <ncurses.h>
int main() {
  initscr();
  noecho();

  printw("Hello from the underworld");
  getch();

  // Deletes a character, defined by move(y, x);
  // move(0, 9)
  // delch();
  // wdelch(WINDOW);

  // Achieves the same results as above
  mvdelch(0, 9);

  // performs within the window
  // WINDOW *win = newwin(10, 25, 5, 5);
  // box(win, 0, 0);
  // refresh();
  // wrefresh(win);
  //
  // mvwprintw(win, 1, 1, "hello this is more text");
  // wgetch(win);
  // mvwdelch(win, 1, 10);
  // wgetch(win);

  getch();

  endwin();

  return 0;
}

Chapter 20: Line Management (insertln, deleteln, insdelln)

// function to add / remove lines

#include <curses.h>

int main() {
  initscr();
  noecho();

  WINDOW *win = newwin(4, 25, 5, 5);
  refresh();
  wrefresh(win);

  mvwprintw(win, 0, 0, "1 Line of Text here");
  mvwprintw(win, 1, 0, "2 Line of Text here");
  mvwprintw(win, 2, 0, "3 Line of Text here");
  mvwprintw(win, 3, 0, "4 Line of Text here");

  wmove(win, 1, 0);
  wgetch(win);

  // inserts a line wherever the cursor it. needs a wmove().
  // Moves every other lines -1
  // winsertln(win);

  // deletes the line, again at the position determined by wmove()
  // also moves other lines
  wdeleteln(win);

  // deletes or inserts lines, determined by the second int argument
  // (positive int to add, negative int to remove).
  winsdelln(win, -2);

  // No more room for lines? the bottom ones gets deleted first.

  wgetch(win);
  endwin();
  return 0;
}