A public-domain Fortran(2003) interface to the ncurses(3c) library

The ncurses(3c) library lets you control your terminal screen on a character-cell by character-cell basis. It is often described as a "CRT screen handling and optimization package". The ncurses(3c) library is C-based and does not supply a Fortran interface. The following is a description of public-domain files that comprise such a Fortran/C interface for ncurses(3c).

This is a screen shot from a Fortran program running in an xterm(1) window that uses ncurses(3c):

Note that this interface does not yet support all the ncurses(3c) extensions to the curses(3c) interface, such as

System Requirements

The interface has been tested (20150113) using

Download Files:

All the files, including the examples are at

        https://github.com/urbanjost/M_ncurses

Documentation:

You can typically get a description of all the ncurses(3c) C routines on Linux and Unix platforms by entering

  man ncurses

or see the Wikipedia entry for "Ncurses" or the main web page for ncurses(3c). The Fortran routines are essentially a one-to-one mapping to the C routines except as noted below and in the example programs.

   http://invisible-island.net/ncurses/

Examples

The examples should help considerably to show the differences in the Fortran interface for those familiar with the C interface. To get started the most significant differences are:

To try the interface start with a simple program like the "Hello World!" example below and try the following commands (or their equivalent for your programming environment):

   # GNU compiler
   gcc -c macros.c  # build auxiliary C routines
   gfortran -J. -c ncurses.f90
   gfortran -I. -J. hello_world.f90 ncurses.o macros.o -lncurses -o hello_world
   ./hello_world
   # Intel compiler
   icc -c macros.c  # build auxiliary C routines
   ifort -I. -c ncurses.f90
   ifort -I. hello_world.f90 ncurses.o macros.o -lncurses -o hello_world
   ./hello_world

Fortran "Hello World" Example

!=============================================================================== program hello_world ! @(#) HELLO WORLD program using ncurses(3c) from Fortran use M_ncurses ! load interface to ncurses(3c) C library !mplicit none integer :: ierr, ikey stdscr=initscr() ! Start curses mode ierr=addstr("Hello World!!!"//C_NULL_CHAR) ! Print Hello World ierr=refresh() ! update the real screen ikey=getch() ! Wait for a user keystroke ierr=endwin() ! End curses mode end program hello_world

C "Hello World" Example

#include <ncurses.h> int main() { initscr(); /* Start curses mode */ printw("Hello World !!!"); /* Print Hello World */ refresh(); /* Print it on to the real screen */ getch(); /* Wait for user input */ endwin(); /* End curses mode */ return 0; }

The following example programs show basic usage:

Print your ncurses(3c) pads and windows as HTML

A simple cut and paste of a plain terminal window can suffice for monochrome output not using box characters. but I find it far easier (especially if you want to print a "pad" window, which can be larger than your display screen) to use the following routines:

The ncp.f90 sample program reads a file generated by ncurses(3c) programs with the putwin(3c) procedure and converts them to HTML. Note this allows windows dumped from programs written in other languages to be dumped.

As an example, nc_printhtml(3f) was used to generate the following output from selected test programs:


ACS_ULCORNER:Upper left corner    ┌    ACS_GEQUAL  :Greater/Equal sign   ≥
ACS_LLCORNER:Lower left corner      └  ACS_PI      :Pi                     π
ACS_LRCORNER:Lower right corner   ┘    ACS_NEQUAL  :Not equal            ≠
ACS_URCORNER:Upper right corner     ┐  ACS_STERLING:UK pound sign          £
ACS_LTEE    :Tee pointing right   ├
ACS_RTEE    :Tee pointing left      ┤
ACS_BTEE    :Tee pointing up      ┴
ACS_TTEE    :Tee pointing down      ┬
ACS_HLINE   :Horizontal line      ─
ACS_VLINE   :Vertical line          │
ACS_PLUS    :Crossover            ┼
ACS_S1      :Scan Line 1            ⎺
ACS_S3      :Scan Line 3          ⎻
ACS_S7      :Scan Line 7            ⎼
ACS_S9      :Scan Line 9          ⎽
ACS_DIAMOND :Diamond                ♦
ACS_CKBOARD :Stipple              ▒
ACS_DEGREE  :Degree Symbol          °
ACS_PLMINUS :Plus/Minus Symbol    ±
ACS_BULLET  :Bullet                 •
ACS_LARROW  :Arrow Pointing Left  ←
ACS_RARROW  :Arrow Pointing Right   →
ACS_DARROW  :Arrow Pointing Down  ↓
ACS_UARROW  :Arrow Pointing Up      ↑
ACS_BOARD   :Board of squares     ▚
ACS_LANTERN :Lantern Symbol         ␋
ACS_BLOCK   :Solid Square Block   █
ACS_LEQUAL  :Less/Equal sign        ≤


 Color      FD  FB  FW  BG  BD    BFD BFB BFW BBG BBN

 default    FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 BLACK      FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 RED        FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 GREEN      FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 YELLOW     FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 BLUE       FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 MAGENTA    FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 CYAN       FD  FB  FW  BG  BD    FD  FB  FW  BG  BD
 WHITE      FD  FB  FW  BG  BD    FD  FB  FW  BG  BD

  FD  = Front color on default background
  FB  = Front color on black background
  FW  = Front color on white background
  BG  = Front and background color
  BD  = Background color with default front color
  B?? = As above, with A_BOLD enabled

Hit any key to exit.



                                       ──
                                      ────
                                     ──────
                                    ────────
                                   ──────────
                                  ────────────
                                 ──────────────
                                ────────────────
                               ──────────────────
                              ────────────────────
                             ──────────────────────
                            ────────────────────────
                           ──────────────────────────
                          ────────────────────────────
                         ──────────────────────────────
                        ────────────────────────────────
                       ──────────────────────────────────
                      ────────────────────────────────────
                     ──────────────────────────────────────
                    ────────────────────────────────────────
                   ──────────────────────────────────────────
                  ────────────────────────────────────────────
                 ──────────────────────────────────────────────
                ────────────────────────────────────────────────
               ──────────────────────────────────────────────────
              ────────────────────────────────────────────────────
             ──────────────────────────────────────────────────────
            ────────────────────────────────────────────────────────


Type any character to see it in bold ('q' to quit)
Note on some keyboards you hit [FN][Function Key] to press a 'function key'
The pressed key value is 330, named function  dc: delete character
The pressed key value is 262, named function  home: home key
The pressed key value is 339, named function  ppage: previous page
The pressed key value is 338, named function  npage: next page
The pressed key value is 360, named function  end: end key
The pressed key value is 360, named function  end: end key
The pressed key value is 261, named function  right: Right arrow key
The key value is 19, a normal 'non-printable' called  DC3 which prints as^S
The pressed key value is 97, the regular character a
The pressed key value is 98, the regular character b
The pressed key value is 99, the regular character c
The pressed key value is 100, the regular character d
The pressed key value is 65, the regular character A
The pressed key value is 66, the regular character B
The pressed key value is 67, the regular character C
The key value is 32, a normal 'non-printable' called  SPACE which prints as 
The key value is 27, a normal 'non-printable' called  ESC which prints as^[
The pressed key value is 96, the regular character `
The pressed key value is 113, the regular character q



This terminal is capable of the following attributes:
 AltCharSet: Yes ▒bcde°±▚␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥█123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      Blink: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
       Bold: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
        Dim: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
 Invis(ible):Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
     Normal: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Reverse: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
   Standout: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
  Underline: Yes abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Protect: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
 Horizontal: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
       Left: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
        Low: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      Right: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
        Top: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
   Vertical: No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
   Italic:   No  abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Window size is 28 rows, 80 columns.
This terminal has insert/delete character abilities
This terminal has insert/delete line abilities
This terminal can do colors.
This terminal can change the standard colors.
This terminal's baud rate is 38400.



Click the left mouse around the screen. Asterisks should appear where you click
unless you click in the green bar, which should cause a beep instead.
The ENTER key exits.
                                   *   *
                                                  *
                           *                        *
                                    *         *
                                          *      *

                     *        *           
                                *             *      *
                                          
                     *     *                     *
                                          
                                           *
                                       *        *   *

                      *        *
                                     *    *       *

                                               *
                               *         *