views:

371

answers:

5

I would like to know how to create console applications with better control of input and output using C. For example, how does the text-editor nano display the file contents in a certain portion of the screen and then keep a list of commands stationary on the bottom. I know you can use the win32 api to do this in Windows, but what is the Unix/Linux equivalent?

A: 

ncurses

Ignacio Vazquez-Abrams
Rather than posting only a link, it is more helpful to provide a sentence or two describing what it is a link to.
anon
A: 

I remember using curses (or the newer version ncurses).

http://en.wikipedia.org/wiki/Ncurses

Moron
+5  A: 

More than likely there's a ncurses interface that controls the screen drawing and placement of items on the screen in a certain position. The api is standard across the linux systems and on unix also, some may be implementation defined depending on the variant of curses used under the commercial variants of unix but otherwise some standard ncurses functionality are compatible.

Hope this helps, Best regards, Tom.

tommieb75
But what is it that ncurses uses for Linux (Ubuntu)?
Matthew
@Matthew: As per the link above, ncurses uses GNU Midnight Commander (File manager), YAST by Suse, and GNU Screen to name but a few...
tommieb75
@Matthew: also the ncurses communicates with the termcap (terminal capabilities) to determine what kind of console is used such as type of console ASCII, VTerm, such as how many lines and how many columns... Think of termcap as the middle layer, the bottom layer is the console graphics, the top layer as the ncurses api.
tommieb75
Matthew: ncurses is a library that you can use in your own applications to control the terminal.
caf
I understand that. I knew about ncurses before I asked my question. I really wanted to know what is the low-level stuff that ncurses uses. I am doing something quite basic and don't want to have to use a larger library like that.
Matthew
@Matthew: to iterate, the termcap is responsible for controlling the console, ncurses 'talk' to termcap, termcap delegates the task in drawing, positioning the cursor, unless you want to dig deeper and see how the consoles are set up low-level and how termcap interacts with it...
tommieb75
The low level stuff ncurses uses is different for different terminal types. The next level below ncurses is terminfo or termcap (it can use either IIRC). Below that are the raw bytes you need to send to each terminal type, the commands are different for different terminals, and they do not have the same capabilities (some of them can set the title string of the window, some can resize, some can scroll, some have more than 8 colors, but you cannot take any of that for granted). If you just use terminfo or termcap you need to handle the logic of how to deal with terminal differences on your own.
Justin Smith
+1  A: 

Besides ncurses and depending on the task at hands you may find newt, a library for color text mode, widget-based user interfaces, a suitable alternative also. Sometimes visual results are better with newt.

dtmilano
A: 

If you just want to do the low level stuff, you probably want to use the termcap or terminfo library.

If you want to do it the way nano and just about every other interactive terminal app does it, you probably want to use ncurses. That way you will spend less time implementing terminal control logic, and more time on the actual app you are developing.

Justin Smith