Saturday, January 30, 2010

Discovering ncurses, the GUI for the Linux Console

The CLI versus GUI debate is well-known, but sometimes, drawing a hard line between text-only interfaces and graphical interfaces is too restrictive.

Most of the time, if you're using a terminal (whether over SSH or on the console), the CLI and text-only interface works great.

Sometimes, though, it can be a real advantage to be able to use at least some graphical logic, without having to fire up an X server.

Not all servers run X; you don't always want to forward X to your remote terminal; or you may be at a stage of system or hardware install that prevents you from running a regular graphical interface.


This is where ncurses comes in. The ncurses library allows you to write programs that work in a GUI-like way, but which will run within a normal terminal emulator. You've almost certainly already encountered programs that use ncurses, including:

  • screen, which allows you to run multiple screens in a single terminal window (this uses the terminfo information from ncurses but does its own screen-handling).
  • make menuconfig, a tool which gives you a menu-driven user interface when configuring the Linux kernel before building it. This is a massive improvement over the older question-and-answer style configuration tool, as you'll know if you've ever missed a question with the older tool and had to start all over again.
  • The email program mutt.
  • The terminal-based web browsers lynx and w3m.
  • aptitude, which provides a menu-driven interface to apt-get on Debian and Ubuntu systems.
  • The visual file manager GNU Midnight Commander.
  • The SuSE system management tool yast, when run in ncurses mode.
  • A variety of MP3-player and IRC interfaces available via Sourceforge and other sites.

Midnight Commander, the powerful ncurses-based file manager
Midnight Commander, the powerful ncurses-based file manager

ncurses is a C library which provides a variety of functions for handling screen drawing, cursor management, mouse management, and so on.

It isn't released under the GPL, but under a specific free software copyright notice which is discussed in the FAQ.

As a programmer, using ncurses means that you don't have to deal with the massive variety of slightly different terminal codes that exist. Instead, you can just deal with the more abstract concept of a display with multiple windows, and ncurses will handle the translation into control codes for whichever terminal emulator it detects that the program is running under. It can also handle mouse events, so if you have a mouse handler running (either on console or via X), you can use links and clickable menus within your terminal.

w3m, the ncurses Web browser. It take the same colors as your X 

w3m, the ncurses Web browser.

It take the same colors as your X terminal

There are ways of interacting with ncurses in numerous languages other than C itself (including Python, Perl, and C++), so anything you learn in one language will transfer with reasonable ease to another.

If you're interested in writing your own ncurses programs, there's an introduction here (technically obsolete, but still useful as a starting point), and a HOWTO.

It's entirely possible to run an ncurses-based program within a terminal running in an X environment.

The main thing to bear in mind when doing this is that the ncurses mouse-handling overrides the X mouse-handling while the mouse pointer is within the window that the ncurses application is running in.

So you can use the mouse to click on menus and links, but you can't (unless the program implements this itself) copy-and-paste using the normal X methods. 

In terms of a straight comparison between ncurses and X as ways of providing a graphical interface, X certainly provides much more scope for things like moving between terminals, and cutting-and-pasting between terminals.

Fundamentally, X provides a way of managing multiple windows within a desktop environment, and ncurses doesn't. screen, which uses part of the ncurses code, does provide multiple terminals within one session, which you can paste between, but you can't natively cut-and-paste between two separate instances of screen, or even use X to do this between ncurses programs in separate terminals.

ncurses applications are also less aesthetically pleasing, by and large, than X applications, due to the limitations imposed by operating within a terminal; they tend to look quite blocky and very text-based.

The extent to which ncurses applications are GUI-like is more about their logic and internal navigation than it is about their aesthetics. However, this 'graphical logic' can be incredibly useful. 

Being able to use the mouse for browsing within w3m is useful; and being able to use a menu-driven system for configuring a new Linux kernel (with make menuconfig) is incredibly useful and makes the task significantly easier.

(There is also an X kernel configuration tool available, but you don't always have X running when you're rebuilding a kernel!).

Even in a modern system, it's not necessarily the case that a full-on GUI will actually give you significant improvements.

Depending on what you're doing, the graphical login of ncurses may be enough of an improvement over a text-only CLI; and ncurses applications are way more lightweight than X applications.

Most importantly, they don't require you to be running an X server, which isn't always either desirable or practical.

If your focus is on aesthetic beauty, or you need a desktop-style environment, then by all means stick with X and regular GUIs.

If you want ease-of-use and low overheads, and you're either not bothered about graphical bells and whistles, or running in a terminal or console environment, ncurses-based programs can make that terminal environment far more usable, fast, and productive.

No comments:

Post a Comment