Curses
Curses is a way to manipulate terminals mainly for text user interfaces in applications.
ncurses.h
The ncurses.h helper file in C includes a lot of other libraries, as well, which means you don't need to include them, as they may bloat your final compiled program[2]:
stdio.hunctrl.hstdarg.hstddef.h
Example
This is an example of ncurses, the most common curses library, from Dan Gookin's book[2] in C.
goodbye.c
#include <ncurses.h> // Use the ncurses library
int main(void)
{
initscr(); // allocates memory for present window which is called stdscr[4]
addstr("Goodbye, cruel C programming!"); // add string to window
refresh(); // flushes current window to the screen
getch(); // waits for user input to exit
endwin(); // call to end or exit our curses program[5]
return 0; // ended program without errors
}
Compiling
$ gcc -lncurses goodbye.c -o goodbye
# -lncurses : the curses linker
Running
$ ./goodbye
Functions
Cursor
move(row, column)- Move the cursor to the Y,X point.
Printing Text
printw(str[, arg1, arg2, ...])-printwisprintffor ncurses. Uses standard string formatting.addstr(str)- Print the string at the cursor.addch(char)- Print the current character at the cursor.
Text Formatting
Styles
attron(attr[ | attr | ...])/attroff(attr[ | attr | ...])- Set attributes on/off for the following text.attrset(attr[ | attr | ...])- Set attributes for the following text, resetting all previous attributes.
| Attribute | Effect |
|---|---|
A_BOLD |
Bright text, bold text, thick text (depending on terminal type) |
A_DIM |
Dimmed text (not as bright as regular text) |
A_NORMAL |
Normal text |
A_REVERSE |
Inverse text |
A_STANDOUT |
Same as standout() |
A_UNDERLINE |
Underline text |
Colors
int has_colors()- ReturnsTRUEif terminal can render colors, andFALSEotherwise, using builtin defs forTRUEandFALSE.int start_color()- Allows colors to be used. ReturnsOKif allowed. Usually used right afterinitscr.
User Input
int getch()- Waits for user input and returns the inputted character.getnstr(str, int n)- Get user input up toncharacters. On newline, stores input character buffer instr.
Display
clrtoeol()- Clears everything that is on the current line under the cursor[7].clear()- Clears the screen.curs_set(0)- Make cursor invisible on screen.
References
- https://en.wikipedia.org/wiki/Curses_%28programming_library%29
- https://www.goodreads.com/book/show/4460597-programmer-s-guide-to-ncurses
- https://www.linuxhowtos.org/manpages/3x/ncurses.htm
- https://www.sbarjatiya.com/notes_wiki/index.php/Using_ncurses_library_with_C
- https://linux.die.net/man/3/endwin
- https://manpages.debian.org/testing/ncurses-doc/start_color.3ncurses.en.html
- https://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses
Incoming Links
Last modified: 202401040446