Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do ncurses et. al. Work?

Tags:

There are several libraries like ncurses that assist in making command-line GUIs.

Simply put, how do they work?

My first thought was that ncurses intercepts all keyboard input, and draws each "frame" by outputting it line-by-line normally. Closer inspection, however, reveals that each new frame overwrites the previous one. How does it modify lines that have already been outputted? Furthermore, how does it handle color?

EDIT: The same question applies to anything with a "fancy" interface, like vim and emacs.

like image 412
Maxpm Avatar asked Sep 26 '11 16:09

Maxpm


People also ask

What can you do with ncurses?

What we can do with NCURSES. NCURSES not only creates a wrapper over terminal capabilities, but also gives a robust framework to create nice looking UI (User Interface)s in text mode. It provides functions to create windows etc. Its sister libraries panel, menu and form provide an extension to the basic curses library.

What is a ncurses interface?

ncurses (new curses) is a programming library providing an application programming interface (API) that allows the programmer to write text-based user interfaces (TUI) in a terminal-independent manner. It is a toolkit for developing "GUI-like" application software that runs under a terminal emulator.

How do you end ncurses?

To leave ncurses mode, call endwin() as you would if you were intending to terminate the program. This will take the screen back to cooked mode; you can do your shell-out. When you want to return to ncurses mode, simply call refresh() or doupdate() . This will repaint the screen.

Does ncurses work on windows?

In ncurses, "windows" are a means to divide the screen into logical areas. Once you define a window, you don't need to track its location on the screen; you just draw to your window using a set of ncurses functions.


2 Answers

Text terminals have command sequences that do things like move the cursor to a particular position on the screen, insert characters, delete lines etc.

Each terminal type is different and has its own set of command sequences. ncurses has a databse (see terminfo for details)

Internally ncurses maintains 2 views of the screen: the current contents and what the screen should look like after the current pending changes are applied. Once the program requests a screen redraw, ncurses calculates an efficient way to update the screen to look like the desired view. The exact characters/command sequences output depend on what terminal type is in use.

like image 87
Craig Avatar answered Oct 14 '22 18:10

Craig


curses (and ncurses, too, I think) works by moving the cursor around on the screen. There are control sequences to do such things. Take a look at the code again and you'll see them. These sequences are not ASCII control characters, they are strings starting with (umm...) ESC, maybe. Have a look here for a higher-level explanation.

like image 27
Pete Wilson Avatar answered Oct 14 '22 19:10

Pete Wilson